Histogram of Python drawing

Use the bar function in matplotlib.pyplot to draw a histogram

The syntax format is as follows:

bar(x,height,width,bottom,align,tick_label)

Note: x represents the x coordinate value of the column

height: Indicates the height of the column

width: Indicates the width of the column

bottom: Indicates the y coordinate value of the bottom of the column

align: Indicates the alignment of the column, "center" indicates that the column scale line is centered, and "edge" indicates that the left side of the column is aligned with the scale line

tick_label indicates the label corresponding to the column

Example:

import matplotlib.pyplot as plt 
import numpy as np
#导入我们需要的包
x = np.arange(5)
y1 = np.array([10,8,7,11,13])
bar_width=0.3 #柱形的宽度
plt.bar(x,y1,tick_label=['a','b','c','d','e'],width= bar_width)
plt.show()

Draw multiple histograms

import matplotlib.pyplot as plt 
import numpy as np
#导入我们需要的包
x = np.arange(5)
y1 = np.array([10,8,7,11,13])
y2 = np.array([5,4,8,12,14])
bar_width=0.3 #柱形的宽度
plt.bar(x,y1,tick_label=['a','b','c','d','e'],width= bar_width)
plt.bar(x+bar_width,y2,width=bar_width) #另一组柱形的x的坐标值加了bar_width宽度
plt.show()

 

 Draw a stacked column chart

import matplotlib.pyplot as plt 
import numpy as np
#导入我们需要的包
x = np.arange(5)
y1 = np.array([10,8,7,11,13])
y2 = np.array([5,4,8,12,14])
bar_width=0.3 #柱形的宽度
plt.bar(x,y1,tick_label=['a','b','c','d','e'],width= bar_width)
plt.bar(x,y2,bottom=y1,width=bar_width) #利用bottom使第二个柱形图底部y参数值为y1
plt.show()

 

Guess you like

Origin blog.csdn.net/qq_52351946/article/details/130537321