Do you really know Python? You can't draw histograms in python, can you? In detail, teach you how to draw with python!

As the classic and most commonly used data visualization library of Python, the matplotlib library has powerful charting capabilities. Today, then, we will discuss a commonly used graphics, it is all very familiar with the histogram .

What is a histogram? In fact, the histogram is a statistical graph used to show the distribution characteristics of continuous data. By drawing the histogram, you can intuitively observe the characteristics, trends, and fluctuations of the constituent data in the data set.

In Python data visualization, the histogram is actually drawn and generated by calling the hist() function !

In order to let everyone have a more intuitive impression and a deeper understanding of the histogram and hist() function, let's give a "chestnut"!

The above is the program that we wrote to draw the histogram, let's focus on the hist() function in the red box above!

plt.hist(x_axis,bins=y_axis,color='red',histtype='bar',rwidth=0.97)

First look at the first parameter x_axis, which represents the continuous data input value, which is the value we assigned to the x-axis in line 10 above.

The second parameter bins is used to determine the number of cylinders, of course, it may also be the range of the cylinder edge.

The third parameter color represents the color of the cylinder.

The fourth parameter histtype represents the type of cylinder.

The fifth parameter rwidth represents the relative width of the cylinder. Its value range is 0~1. Of course, the histogram drawn with different relative widths for rwidth will be different!

Now let's call the Python interpreter to run the program we wrote above!

Execute the above command to output the histogram drawn as follows:

Now let's adjust the value of the fifth parameter rwidth in the hist() function of drawing the histogram to see what the different effects are!

First of all, let's assign the value of rwidth to 1.0 to see the effect:

Running our modified program will output the following results:

Do you see the difference? At this time, when rwidth=1.0, the histogram drawn will fill the chart, and the boundary between the columns is no longer obvious!

Next, let's reduce the value of the parameter rwidth. For example, let's assign a value of 0.1 to the variable rwidth to see the effect:

Run the modified program above, and the following chart will be generated:

At this time, the histogram we draw will become thin bars!

So when using the hist() function to draw a histogram, you need to assign a proper value to the parameter rwidth!

The above is all that we have introduced about drawing histograms in Python. I hope you learn the calling and syntax format of the hist() function, and then apply it to your own work! Okay, let's stop here today! See you next time! [Goodbye]

Python learning exchange group , welcome all friends to exchange and learn.

Guess you like

Origin blog.csdn.net/Python_xiaobang/article/details/112314015