In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, the editor will share with you the relevant knowledge points about how to achieve histogram drawing based on Python+Matplotlib. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.
1. About histogram
A histogram is also called a mass distribution map, although it looks like a histogram
Actually, there's a big difference. The histogram usually represents the data type on the horizontal axis and the distribution of each data type on the vertical axis.
Histogram can be divided into frequency distribution histogram and frequency distribution histogram. The drawing method is not much different, except that the events described are different. The frequency distribution histogram describes the number of events, while the frequency distribution describes the frequency at which it occurs.
On the other hand, the frequency distribution histogram can be understood as a kind of "density diagram". Both the frequency distribution histogram and the density graph can be used to describe the probability distribution of events, in which the frequency distribution histogram describes the probability distribution of discrete random variables, while the density graph describes the probability distribution of continuous random variables.
2.plt.hist ()
The histogram is drawn by the plt.hist () method, and the commonly used parameters are:
X data set
Bins-interval distribution of statistics. It can be a list of numeric elements or a numeric value. It can be used with the range parameter when it is a numeric value.
Range-tuple type, the interval displayed.
When setting the interval distribution using numeric bins and range parameters: range determines a range, the input form is a tuple (note that it is not a range range object), and both left and right endpoint values are acceptable. The numeric bins represents the number of shares (equal parts) that divide the range.
Density-Boolean type, showing the frequency statistics. Default is None (equivalent to False), set to False to not display frequency statistics results, and set to True to display frequency statistics results, that is, the image drawn changes from frequency distribution histogram to frequency distribution histogram.
Histtype-optional parameter, that is, the type of histogram. The default is bar, that is, the drawn "cylindrical" bar. It can also be set to barstacked, step, stepfilled.
Align-optional parameter to control the horizontal distribution of the bar chart, set to left, mid, or right, the default value is mid, and mid is also recommended. Left and right create some blank areas.
Log-Boolean type, which defaults to False, that is, whether the y-axis selects an exponential scale.
Stacked-Boolean type, which defaults to False, indicating whether it is a stacked histogram.
Edgecolor- sets the edge color
Facecolor- can be used to set the color of the histogram, or it can be abbreviated to color.
The direction of the orientation- histogram defaults to the vertical direction ('vertical'), while the setting of' horizontal' indicates the horizontal direction.
Cumulative- defaults to False, which means no accumulation. Sets the cumulative indexing histogram if True.
3. Draw a simple frequency distribution histogram import matplotlib.pyplot as pltimport numpy as npplt.rcParams ['font.sans-serif'] = [' SimHei'] plt.rcParams ['axes.unicode_minus'] = Falseplt.rcParams [' axes.facecolor'] ='# cc00ff'# set random numbers as 30np.random.seed (30) data = np.random.randint (0,100,100) plt.hist (data, bins= [0,25,50,75,100], facecolor='#ffff00' Edgecolor='#FF0000') plt.xlabel ('X index') plt.ylabel ('number of samples') plt.title ('X index frequency distribution histogram') plt.show ()
4. Draw a frequency distribution histogram
Just set density to True.
In addition, we modify a few more details to set the histtype parameter to stepfilled (ladder and fill).
And change the label of the y-axis from "number of samples" to "sample frequency".
Import matplotlib.pyplot as pltimport numpy as npplt.rcParams ['font.sans-serif'] = [' SimHei'] plt.rcParams ['axes.unicode_minus'] = Falseplt.rcParams [' axes.facecolor'] ='# cc00ff'np.random.seed (30) data = np.random.randint (0100,100) plt.hist (data, bins= [0,25,50,75,100], facecolor='#ffff00', edgecolor='#FF0000', density=True Histtype='stepfilled') plt.xlabel ('X index') plt.ylabel ('sample frequency') plt.title ('X index frequency distribution histogram') plt.show ()
The image effect is as follows:
5. Cumulative distribution histogram (horizontal)
Add parameters cumulative=True, orientation='horizontal'.
In addition, because the image becomes horizontal, you also need to interchange the x-axis and y-axis labels and adjust them slightly.
Here, the interval distribution is set by numerical bins and range parameters, which range from 0 to 100, and the interval is divided into 10 equal parts.
Import matplotlib.pyplot as pltimport numpy as npplt.rcParams ['font.sans-serif'] = [' SimHei'] plt.rcParams ['axes.unicode_minus'] = Falseplt.rcParams [' axes.facecolor'] ='# cc00ff'np.random.seed (30) data = np.random.randint (0100,100) plt.hist (data, bins=10, range= (0,100), facecolor='#ffff00', edgecolor='#FF0000', density=True, cumulative=True Orientation='horizontal') plt.xlabel ('sample cumulative frequency') plt.ylabel ('X index') plt.title ('X index frequency distribution histogram') plt.show ()
The image effect is as follows:
These are all the contents of the article "how to draw a histogram based on Python+Matplotlib". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.