学习完matplotlib绘图可以设置的属性,还需要学习一下除了折线图以外其他类型的图如直方图,条形图,散点图等,matplotlib还支持更多的图,:
折线图
折线图主要是以折线的上升或者下降表示数据的增减
plt.plot() 调用多次可以在同一张图上绘制多条折线
x = range(11,31,1)a = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1]b = [1,0,3,1,2,2,3,3,2,1,2,1,1,1,1,1,1,1,1,1]plt.xticks(x)plt.plot(x,a,label="数据A",linestyle="-",color="red",alpha=0.5)plt.plot(x,b,label="数据B",linestyle="--",color="blue",alpha=0.5)plt.legend(prop=my_font,loc="best")plt.show()
散点图
散点图主要是表示x和y之间的关系
与折线图唯一不同的就是绘制散点图调用的是 scatter方法
a = [11,17,16,11,12,11,12,6,6,7,8,9,12,15,14,17,18,21,16,17,20,14,15,15,15,19,21,22,22,22]b = [26,26,28,19,21,17,16,19,18,20,20,19,22,23,17,20,21,20,22,15,11,15,5,13,17,10,11,13,12,13]x_a = range(1,31)x_b = range(70,100)#设置图形大小plt.figure(figsize=(15,8),dpi=80)#使用scatter方法绘制散点图plt.scatter(x_a,a,label="数据A")plt.scatter(x_b,b,label="数据B")#添加图例plt.legend(prop=my_font)#展示plt.show()
条形图
条形图主要是统计离散数据
竖着的条形图
与折线图唯一不同的就是绘条形图调用的是 bar方法,需要设置条形的宽度a = ["流浪地球","疯狂的外星人","飞驰人生","大黄蜂","熊出没·原始时代","新喜剧之王","白蛇:缘起","阿丽塔:战斗天使","死侍2:我爱我家","密室逃生","一吻定情","神探蒲松龄","小猪佩奇过大年 ","廉政风云 ","掠食城市","钢铁飞龙之奥特曼崛起","一条狗的回家路","家和万事惊","命运之夜——天之杯:恶兆之花","我想吃掉你的胰脏"]b=[43.14,21.35,16.35,11.37,6.9,6.11,4.42,4.22,2.83,2.29,1.64,1.5,1.22,1.11,0.4997,0.3746,0.36,0.3233,0.3136,0.2361]#设置图形大小plt.figure(figsize=(15,8),dpi=80)#绘制条形图plt.bar(range(len(a)),b,width=0.3)#设置字符串到x轴plt.xticks(range(len(a)),a,fontproperties=my_font,rotation=90)plt.show()
横着的条形图
与竖着的条形图不同的是需要设置y轴的坐标,绘制图形调用的是 barh方法,需要设置条形的高度a = ["流浪地球","疯狂的外星人","飞驰人生","大黄蜂","熊出没·原始时代","新喜剧之王","白蛇:缘起","阿丽塔:战斗天使","死侍2:我爱我家","密室逃生","一吻定情","神探蒲松龄","小猪佩奇过大年 ","廉政风云 ","掠食城市","钢铁飞龙之奥特曼崛起","一条狗的回家路","家和万事惊","命运之夜——天之杯:恶兆之花","我想吃掉你的胰脏"]b=[43.14,21.35,16.35,11.37,6.9,6.11,4.42,4.22,2.83,2.29,1.64,1.5,1.22,1.11,0.4997,0.3746,0.36,0.3233,0.3136,0.2361]#设置图形大小plt.figure(figsize=(18,6),dpi=80)#绘制条形图plt.barh(range(len(a)),b,height=0.3,color="orange")#设置字符串到y轴plt.yticks(range(len(a)),a,fontproperties=my_font)plt.grid(alpha=0.3)plt.show()
多组数据绘制在一个图上
a = ["数据A","数据B","数据C","数据D"]b_1 = [15746,290,4497,339]b_2 = [12357,176,2045,168]b_3 = [2358,369,2358,392]bar_width = 0.2x_1 = list(range(len(a)))x_2 = [i+bar_width for i in x_1]x_3 = [i+bar_width*2 for i in x_1]#设置图形大小plt.figure(figsize=(20,8),dpi=80)plt.bar(range(len(a)),b_1,width=bar_width,label="1日")plt.bar(x_2,b_2,width=bar_width,label="2日")plt.bar(x_3,b_3,width=bar_width,label="3日")#设置图例plt.legend(prop=my_font)#设置x轴的刻度plt.xticks(x_2,a,fontproperties=my_font)plt.show()
直方图
直方图主要是 统计连续的数据
需要把所有的数据分成N组,组数需要适当,太少会有较大的统误差,太多规律不明显 组数 = 极差(最大值-最小值)/组距 组距:指每个小组的两个端点的距离与折线图唯一不同的就是绘制直方图调用的是 hist方法 需要传入数据和组数
a=[131, 98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114, 119, 128, 121, 142, 127, 130, 124, 101, 110, 116, 117, 110, 128, 128, 115, 99, 136, 126, 134, 95, 138, 117, 111,78, 132, 124, 113, 150, 110, 117, 86, 95, 144, 105, 126, 130,126, 130, 126, 116, 123, 106, 112, 138, 123, 86, 101, 99, 136,123, 117, 119, 105, 137, 123, 128, 125, 104, 109, 134, 125, 127,105, 120, 107, 129, 116, 108, 132, 103, 136, 118, 102, 120, 114,105, 115, 132, 145, 119, 121, 112, 139, 125, 138, 109, 132, 134,156, 106, 117, 127, 144, 139, 139, 119, 140, 83, 110, 102,123,107, 143, 115, 136, 118, 139, 123, 112, 118, 125, 109, 119, 133,112, 114, 122, 109, 106, 123, 116, 131, 127, 115, 118, 112, 135,115, 146, 137, 116, 103, 144, 83, 123, 111, 110, 111, 100, 154,136, 100, 118, 119, 133, 134, 106, 129, 126, 110, 111, 109, 141,120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126,114, 140, 103, 130, 141, 117, 106, 114, 121, 114, 133, 137, 92,121, 112, 146, 97, 137, 105, 98, 117, 112, 81, 97, 139, 113,134, 106, 144, 110, 137, 137, 111, 104, 117, 100, 111, 101, 110,105, 129, 137, 112, 120, 113, 133, 112, 83, 94, 146, 133, 101,131, 116, 111, 84, 137, 115, 122, 106, 144, 109, 123, 116, 111,111, 133, 150]#计算组数d = 3 #组距num_bins = (max(a)-min(a))//d#设置图形的大小plt.figure(figsize=(20,8),dpi=80)plt.hist(a,num_bins)#设置x轴的刻度plt.xticks(range(min(a),max(a)+d,d))plt.grid()plt.show()