What is the chi-square distribution graph?
The test statistic in the chi-square goodness-of-fit test or the test of independence follows the chi-square distribution. The chi-square distribution graph is asymmetrical in shape. It is skewed to the right, and its shape depends on the degrees of freedom.
How to generate the chi-square distribution graph in Python?
We can use the following Python code to generate the chi-square distribution graph for specific degrees of freedom.
import numpy
from scipy.stats import chi2
from matplotlib import pyplot
X = numpy.linspace(0, 30, 300)
Y1 = chi2.pdf(X, df=5)
Y2 = chi2.pdf(X, df=10)
Y3 = chi2.pdf(X, df=15)
pyplot.plot(X, Y1, label="df = 5")
pyplot.plot(X, Y2, label="df = 10")
pyplot.plot(X, Y3, label="df = 15")
pyplot.legend()
pyplot.savefig("chi2-dist.png")
pyplot.close()
Here, we are using the linspace() function from the numpy Python module to generate 300 equally spaced numbers within the range 0 to 30. After that, we are using the chi.pdf() function from the scipy.stats module to generate the points on the y-axis …








































0 Comments