If you want a histogram, you don't need to attach any 'names' to x-values because:

  • on x-axis you will have data bins
  • on y-axis counts (by default) or frequencies (density=True)
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");

If you're willing to explore other opportunities, there is a shortcut with seaborn:

# !pip install seaborn
import seaborn as sns
sns.displot(x, bins=82, kde=True);

Now back to the OP.

If you have limited number of data points, 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']);

Answer from Sergey Bushmanov on Stack Overflow
🌐
Matplotlib
matplotlib.org › stable › api › _as_gen › matplotlib.pyplot.hist.html
matplotlib.pyplot.hist — Matplotlib 3.11.2 documentation
This method uses numpy.histogram to bin the data in x and count the number of values in each bin, then draws the distribution either as a BarContainer or Polygon.
🌐
W3Schools
w3schools.com › python › matplotlib_histograms.asp
Matplotlib Histograms
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ... A histogram is a graph showing frequency distributions.
🌐
GeeksforGeeks
geeksforgeeks.org › data visualization › plotting-histogram-in-python-using-matplotlib
Plotting Histogram in Python using Matplotlib - GeeksforGeeks
Explanation: hist() function divides the data into 30 bins and plots the frequency of values in each bin and color parameter sets the bar color to sky blue, while edgecolor adds black borders around the bars.
Published: July 16, 2026
Top answer
1 of 7
301

If you want a histogram, you don't need to attach any 'names' to x-values because:

  • on x-axis you will have data bins
  • on y-axis counts (by default) or frequencies (density=True)
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");

If you're willing to explore other opportunities, there is a shortcut with seaborn:

# !pip install seaborn
import seaborn as sns
sns.displot(x, bins=82, kde=True);

Now back to the OP.

If you have limited number of data points, 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']);

2 of 7
27

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 :

🌐
GeeksforGeeks
geeksforgeeks.org › python › matplotlib-pyplot-hist-in-python
Matplotlib.pyplot.hist() in Python - GeeksforGeeks
March 18, 2026 - DSA Python · Data Science · NumPy · Pandas · Practice · Django · Flask · Last Updated : 18 Mar, 2026 · matplotlib.pyplot.hist() function is used to create histograms, which are graphical representations of data distribution.
🌐
YouTube
youtube.com › stikpet
Python - Histogram (Simple) - YouTube
Instructional video on creating a simple histogram with Python.Companion website: https://PeterStatistics.comJupyter Notebook used in video: https://bit.ly/3...
Published: July 6, 2020
Views: 1K
Find elsewhere
🌐
Plotly
plotly.com › python › histograms
Histograms in Python
Over 29 examples of Histograms including changing color, size, log axes, and more in Python.
🌐
Matplotlib
matplotlib.org › stable › gallery › statistics › hist.html
Histograms — Matplotlib 3.11.2 documentation
The histogram method returns (among other things) a patches object. This gives us access to the properties of the objects drawn. Using this, we can edit the histogram to our liking.
🌐
Medium
medium.com › @okanyenigun › how-to-plot-histogram-in-python-matplotlib-seaborn-plotly-9a3a1449a6d4
How to plot Histogram in Python? (Matplotlib, Seaborn, Plotly) | by Okan Yenigün | Towards Dev
October 31, 2022 - import numpy as np import matplotlib.pyplot as plt#generate dummy data vals = np.random.normal(225, 15, 500) #plot fig = plt.figure(figsize = (8, 8)) plt.hist(vals) plt.title('Histogram') plt.show()
🌐
StrataScratch
stratascratch.com › blog › how-to-create-a-matplotlib-histogram
How to Create a Matplotlib Histogram? - StrataScratch
March 10, 2025 - In Matplotlib, you can easily create a histogram using the hist() function. Matplotlib is one of the most widely used Python libraries for data visualization.
🌐
YouTube
youtube.com › watch
Histogram in Python - Matplotlib Tutorial - Pandas Tutorial - Define bins, add style, log scale - YouTube
Learn how to create histograms in Python, using matplotlib and Pandas. Histograms allow you to easily visualize the distribution of a variable by creating bi...
Published: August 5, 2020
🌐
CodingNomads
codingnomads.com › histograms-with-pandas-seaborn-and-matplotlib
Create Histograms with Pandas, Seaborn & Matplotlib
Below is the code syntax to create histograms in three different ways. # pandas Dataframe.hist() # seaborn sns.displot(X['MedInc'], bins=30, kde=True, aspect=2) # matplotlib fig, ax = plt.subplots(figsize=(14,8)) ax.hist(X['MedInc'], bins=30) Want to get into Data Science, Machine Learning or AI? Data Science and Machine Learning are two of the most useful skills you can develop to future-proof your career · If AI is the future, then Python, Data Science, and Machine Learning are the path
🌐
Matplotlib
matplotlib.org › stable › gallery › statistics › histogram_normalization.html
Histogram bins, density, and weight — Matplotlib 3.11.2 documentation
The Matplotlib hist method calls numpy.histogram and plots the results, therefore users should consult the numpy documentation for a definitive guide.
🌐
Data36
data36.com › home › how to plot a histogram in python (using pandas)
How to plot a histogram in Python using pandas (tutorial)
September 1, 2021 - There are many Python libraries that can do so: ... But I’ll go with the simplest solution: I’ll use the .hist() function that’s built into pandas. As I said in the introduction: you don’t have to do anything fancy here… You rather need a histogram that’s useful and informative for you — and for your data science tasks.
🌐
ImageMagick
imagemagick.org
ImageMagick | Mastering Digital Image Alchemy
ImageMagick is a powerful open-source software suite for creating, editing, converting, and manipulating images in over 200 formats. Ideal for developers, designers, and researchers.
🌐
Recharts
recharts.org
recharts.org is registered at Namecheap
has been recently registered with namecheap.com · Discover domains on auction now
🌐
Altcademy
altcademy.com › blog › how-to-plot-histogram-in-python
How to plot histogram in Python
September 6, 2023 - To plot a histogram in Python, we're going to use two important libraries: matplotlib and numpy.
🌐
NBShare
nbshare.io › notebook › 204214467 › How-to-Plot-a-Histogram-in-Python
How to Plot a Histogram in Python
In the above tutorial, I have shown you how to plot histograms in Python using two libraries Matplotlib and Seaborn .