这篇教程Python 图形绘制详细代码(二)写得很实用,希望能帮到您。
4、条形图下面介绍条形图的画法。
4.1 代码import matplotlib.pyplot as plt# x-coordinates of left sides of barsleft = [1, 2, 3, 4, 5]# heights of barsheight = [10, 24, 36, 40, 5]# labels for barstick_label = ['one', 'two', 'three', 'four', 'five']# plotting a bar chartplt.bar(left, height, tick_label = tick_label, width = 0.8, color = ['red', 'green'])# naming the x-axisplt.xlabel('x - axis')# naming the y-axisplt.ylabel('y - axis')# plot titleplt.title('My bar chart!')# function to show the plotplt.show()
4.2 输出
4.3 代码的部分解释 - 1)使用
plt.bar() 函数来绘制条形图。 - 2)x轴与
height 两个参数必须有。 - 3)可以通过定义
tick_labels 为 x 轴坐标指定另外的名称。
5、直方图
5.1 代码import matplotlib.pyplot as plt# frequenciesages = [2,5,70,40,30,45,50,45,43,40,44, 60,7,13,57,18,90,77,32,21,20,40]# setting the ranges and no. of intervalsrange = (0, 100)bins = 10 # plotting a histogramplt.hist(ages, bins, range, color = 'green', histtype = 'bar', rwidth = 0.8)# x-axis labelplt.xlabel('age')# frequency labelplt.ylabel('No. of people')# plot titleplt.title('My histogram')# function to show the plotplt.show()
5.2 输出
5.3 代码的部分解释 |