Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use matplotlib to draw histogram in Python

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to draw histogram with matplotlib in Python". In daily operation, I believe that many people have doubts about how to draw histogram with matplotlib in Python. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how to draw histogram with matplotlib in Python". Next, please follow the editor to study!

1. Overview of histogram

1.1 what is a histogram?

A histogram is a visual representation of the distribution of data at consecutive intervals or over a specific period of time.

Histogram, also known as mass distribution map, is a kind of bar graph.

The x-axis of the histogram represents the data type, and the vertical axis represents the distribution, and the width of each data can be changed at will.

1.2 histogram usage scenarios

A histogram is used as a probability distribution to show the probability of occurrence of a set of data within a specified range.

It can be used to show the frequency of data distribution.

Used for the position of the number, the median

Pay attention to data gaps or outliers

1.3 histogram drawing steps

Import matplotlib.pyplot Modul

To prepare the data, you can use numpy/pandas to organize the data

Call pyplot.hist () to draw a histogram

1.4 case presentation

In this case, let's analyze the height distribution of the company's employees.

Case data preparation, using numpy to randomly generate 200 elevated data

Import numpy as npx_value = np.random.randint (140180200)

Draw a histogram:

Import matplotlib.pyplot as pltplt.hist (data analyze value) plt.title ("height") plt.xlabel ("height") plt.ylabel ("rate") plt.show ()

two。 Histogram attribute

2.1 set Color

Set bar color keyword: facecolor

Set the color keyword for the border: edgecolor

Color selection value:

English words that use colors such as red "red" and yellow "yellow"

Use color abbreviation: red "r", blue "b"

Use rgb: format (rmae gonomeb), value range: 0room1

2.2 set the number of bars

Keyword: bins

Optional, default is 10

2.3 set transparency

Keyword: alpha

The default is 0, and the range of values is 0: 1.

2.4 set the style

Keyword: histtype

Value theory

Attribute values indicate that 'bar' cylindrical data side by side, default' barstacked' cylindrical data overlap side by side 'step' cylindrical color does not fill' linear stepfilled' filling

We add a column to the first section of the histogram without filling it, and the border color is red.

Plt.hist (histtype= valuebins10) edgecolor = "r", "step")

The border is set to red and the transparency is 0.5

Plt.hist (histtype= valuebins10) edgecolor = "r", "bar", alpha=0.5)

3. Add a broken line histogram

In the histogram, we can also add a line chart to help us see the changes in the data.

First create an Axes object through pyplot.subplot ()

Call the hist () method through the Axes object to draw the histogram and return the lower x _ line data needed for the line chart.

Then the Axes object calls plot () to draw a line chart

Let's modify the first section of the code.

Fig,ax = plt.subplots () n binsbinsnumMagol pat = ax.hist (bins_num [: 10], n quotient = "yellowgreen", line)

4. Stack histogram

We sometimes compare the data collected by two groups of different object groups under the same data range.

Prepare two sets of data:

Import numpy as npx_value = np.random.randint (140180200) x2_value = np.random.randint (140180200)

Histogram property data: pass in two sets of data as a list

Set histogram stacked: True, allowing data override

Plt.hist ([x values, x2 values], bins=10,stacked=True)

5. Unequal distance histogram

The histograms we have drawn above are all equidistant, and we can specify a set of data to be passed into the bins attribute.

Bins keyword: specifies the number of columns in the histogram

After changing the above code, let's see the effect.

Bin_num = [140155160170175180] plt.hist (bins=bin_num,alpha=0.75,stacked=True)

6. Multi-class histogram

When we use histograms to check the frequency of data, we sometimes look at the frequency of multiple types of data.

At this point, we can pass in a variety of data to the x data of the hist () method in the form of a list.

X_value = [np.random.randint (140, 180) for i in [100200300]] plt.hist (Xerovaluebins10) edgecolor = "r", histtype= "bar", alpha=0.5,label= ["A Company", "B Company", "C Company"])

At this point, the study of "how to draw a histogram with matplotlib in Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report