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. The bins, range, density, and weights parameters are forwarded to numpy.histogram. If the data has already been binned and counted, use bar or stairs to plot the distribution:
🌐
GeeksforGeeks
geeksforgeeks.org › data visualization › plotting-histogram-in-python-using-matplotlib
Plotting Histogram in Python using Matplotlib - GeeksforGeeks
Titles and axis labels are added to both plots, while tight_layout() automatically adjusts the spacing to prevent overlapping elements. A stacked histogram displays multiple datasets in the same histogram by stacking their frequencies on top of each other. This helps compare the overall distribution and contribution of each dataset. Python ·
Published: July 16, 2026
🌐
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.
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 :

🌐
Matplotlib
matplotlib.org › stable › gallery › statistics › hist.html
Histograms — Matplotlib 3.11.2 documentation
To plot a 2D histogram, one only needs two vectors of the same length, corresponding to each axis of the histogram.
🌐
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 - But when you plot a histogram, there’s one more initial step: these unique values will be grouped into ranges. These ranges are called bins or buckets — and in Python, the default number of bins is 10.
🌐
Seaborn
seaborn.pydata.org › generated › seaborn.histplot.html
seaborn.histplot — seaborn 0.13.2 documentation
Passed to numpy.histogram_bin_edges(). ... Width of each bin, overrides bins but can be used with binrange. ... Lowest and highest value for bin edges; can be used either with bins or binwidth. Defaults to data extremes. ... If True, default to binwidth=1 and draw the bars so that they are centered on their corresponding data points. This avoids “gaps” that may otherwise appear when using discrete (integer) data. ... If True, plot the cumulative counts as bins increase.
Find elsewhere
🌐
Mode
mode.com › example-gallery › python_histogram
Plot Histograms Using Pandas: hist() Example | Charts | Charts - Mode
March 13, 2018 - This recipe will show you how to go about creating a histogram using Python.
🌐
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 - Histogram charts visualize the distribution of a continuous variable. It consists of bars and each bar indicates the frequency in the range its bin specifies. import numpy as np import matplotlib.pyplot as plt#generate dummy data vals = np.random.normal(225, 15, 500) #plot fig = plt.figure(figsize ...
🌐
Stack Overflow
stackoverflow.com › questions › 27872723 › is-there-a-clean-way-to-generate-a-line-histogram-chart
python - Is there a clean way to generate a line histogram chart? - Stack Overflow
I need to create a histogram that plots a line and not a step or bar chart. I am using python 2.7 The plt.hist function below plots a stepped line and the bins don't line up in the plt.plot functi...
🌐
Medium
medium.com › data-science › histograms-and-density-plots-in-python-f6bda88f5ac0
Histograms and Density Plots in Python | by Will Koehrsen | TDS Archive | Medium
March 23, 2018 - To make a basic histogram in Python, we can use either matplotlib or seaborn. The code below shows function calls in both libraries that create equivalent figures. For the plot calls, we specify the binwidth by the number of bins.
🌐
CodingNomads
codingnomads.com › histograms-with-pandas-seaborn-and-matplotlib
Create Histograms with Pandas, Seaborn & Matplotlib
In this way you can use the simple methods to create your plot and then use matplotlib.pyplot to add further customizations · A histogram is a graphical representation of the distribution of numerical data · A histogram estimates the probability distribution of a continuous variable · A histogram is a bar graph that shows the frequency of each value in a dataset · Python histograms are commonly created with Pandas, Seaborn or Matplotlib
🌐
Plotly
plotly.com › python › histograms
Histograms in Python
Over 29 examples of Histograms including changing color, size, log axes, and more in Python.
🌐
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. Somewhat of a synonym for creating plots in Python.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.plot.hist.html
pandas.DataFrame.plot.hist — pandas 3.0.6 documentation
But when we roll two dice and sum the result, the distribution is going to be quite different. A histogram illustrates those distributions. >>> df = pd.DataFrame(np.random.randint(1, 7, 6000), columns=["one"]) >>> df["two"] = df["one"] + np.random.randint(1, 7, 6000) >>> ax = df.plot.hist(bins=12, alpha=0.5)
🌐
R Graph Gallery
r-graph-gallery.com
The R Graph Gallery – Help and inspiration for R charts
Histogram · Boxplot · Ridgeline · Beeswarm · Correlation · Scatter · Heatmap · Correlogram · Bubble · Connected scatter · Density 2d · Ranking · Barplot · Spider / Radar · Wordcloud · Parallel · Lollipop · Circular Barplot · Table · Part of a whole · Grouped and Stacked barplot · Treemap · Doughnut · Pie chart · Dendrogram · Circular packing · Waffle · Evolution · Line plot ·
🌐
ApexCharts
apexcharts.com
ApexCharts.js - JavaScript Charts for your website
ApexCharts.js is a modern JavaScript charting library to build interactive charts and visualizations with simple API.
🌐
Onestopdataanalysis
onestopdataanalysis.com › home › the easiest way to plot a histogram in python – step-by-step
The Easiest way to Plot a Histogram in Python - Step-by-Step
February 13, 2023 - Here you will learn what is the easiest way to plot a histogram in Python. We make use of the seaborn library to create the distribution.
🌐
CodeSignal
codesignal.com › learn › courses › introduction-to-basic-plots-with-matplotlib › lessons › creating-histograms-with-matplotlib
Creating Histograms with Matplotlib | CodeSignal Learn
Python · # Histogram of bill depth plt.hist(penguins['bill_depth_mm']) The plt.hist() function automatically divides the penguins['bill_depth_mm'] data into bins and calculates the count of data points within each bin. Here is the complete code to create a histogram that visually portrays the distribution of penguin bill depths. It incorporates essential plotting elements such as setting the size, labeling, and titling the chart: Python ·