If you want a histogram, you don’t need to attach any ‘names’ to x-values, as on x-axis you would have data bins:
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
np.random.seed(42)
x = np.random.normal(size=1000)
plt.hist(x, density=True, bins=30) # density=False would make counts
plt.ylabel(‘Probability’)
plt.xlabel(‘Data’);
Note, the number of bins=30 was chosen arbitrarily, and there is Freedman–Diaconis rule to be more scientific in choosing the “right” bin width:
, where IQR is Interquartile range and n is total number of datapoints to plot
So, according to this rule one may calculate number of bins as:
q25, q75 = np.percentile(x,[.25,.75])
bin_width = 2*(q75 – q25)*len(x)**(-1/3)
bins = round((x.max() – x.min())/bin_width)
print(“Freedman–Diaconis number of bins:”, bins)
plt.hist(x, bins = bins);
Freedman–Diaconis number of bins: 82
And finally you can make your histogram a bit fancier with PDF line, titles, and legend:
import scipy.stats as st
plt.hist(x, density=True, bins=82, label=”Data”)
mn, mx = plt.xlim()
plt.xlim(mn, mx)
kde_xs = np.linspace(mn, mx, 300)
kde = st.gaussian_kde(x)
plt.plot(kde_xs, kde.pdf(kde_xs), label=”PDF”)
plt.legend(loc=”upper left”)
plt.ylabel(‘Probability’)
plt.xlabel(‘Data’)
plt.title(“Histogram”);
However, if you have limited number of data points, like in OP, a bar plot would make more sense to represent your data. Then you may attach labels to x-axis:
x = np.arange(3)
plt.bar(x, height=[1,2,3])
plt.xticks(x, [‘a’,’b’,’c’])
If you haven’t installed matplotlib yet just try the command.
> pip install matplotlib
Library import
import matplotlib.pyplot as plot
The histogram data:
plot.hist(weightList,density=1, bins=20)
plot.axis([50, 110, 0, 0.06])
#axis([xmin,xmax,ymin,ymax])
plot.xlabel(‘Weight’)
plot.ylabel(‘Probability’)
Display histogram
plot.show()
And the output is like :