Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Matplotlib.pyplot.hist2d() in Python
Next article icon

The matplotlib.pyplot.hist() function in Python is used to create histograms, which are graphical representations of data distribution. It divides the data into bins (non-overlapping intervals) and counts the frequency of values in each bin, plotting them as bars. Lets consider the data values and visualise histogram with help of an example:

Python
importmatplotlib.pyplotaspltdata=[32,96,45,67,76,28,79,62,43,81,70,61,95,44,60,69,71,23,69,54,76,67,82,97,26,34,18,16,59,88,29,30,66,23,65,72,20,78,49,73,62,87,37,68,81,80,77,92,81,52,43,68,71,86]plt.hist(data)plt.show()

Output:

Screenshot-2024-12-04-130555
Histogram with hist() with default parameters

Understanding the syntax and parameters

Syntax: matplotlib.pyplot.hist(x, bins=None, range=None, density=False, histtype='bar', color=None, label=None)

  • x: The data to be represented in the histogram.
  • bins: Specifies the number of bins or the bin edges for the histogram.
  • range: The lower and upper range of the bins.
  • density: If True, the histogram is normalized to form a probability density.
  • histtype: Defines the type of histogram (e.g., 'bar' for a traditional bar histogram).
  • color: Sets the color of the bars.
  • label: Label for the histogram, used in legends.

Create a Histogram in Matplotlib

Using the Matplotlib library in python, we can create many types of histograms. Let us see a few examples to better understand thefunctionality of hist() function. In this example, we will create a histogram and pass the necessary parameters such as bins, color, density, etc.

Python
importmatplotlib.pyplotaspltimportnumpyasnpmu,sigma=121,21x=np.random.normal(mu,sigma,1000)num_bins=100n,bins,_=plt.hist(x,num_bins,density=True,color='green',alpha=0.7)plt.xlabel('X-Axis')plt.ylabel('Y-Axis')plt.title('matplotlib.pyplot.hist() Example',fontweight='bold')plt.show()

Output:

Screenshot-2024-12-05-180917
Creating the histogram

Example 2: Visualize the Specific Bars of Histogram

In this example, we will create a histogram with different attributes using matplotlib.pyplot.hist() function. We define a specific set of colors for the bars of the histogram bars.

Python
importnumpyasnpimportmatplotlib.pyplotaspltx=np.random.randn(10000,3)colors=['green','blue','lime']plt.hist(x,bins=20,density=True,histtype='bar',color=colors,label=colors)plt.legend(fontsize=10)plt.title('matplotlib.pyplot.hist() Example',fontweight='bold')plt.show()

Output:

Screenshot-2024-12-04-151553
different color bars in matplot.pyplot.hist()

Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp