Error message here!

Hide Error message here!

Error message here!

Hide Error message here!

Error message here!

Close

# python实战学习之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()``````

### 散点图

``````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()``````

### 条形图

``````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()``````

``````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.2
x_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()``````

### 直方图

``````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()``````

https://www.cnblogs.com/zh20130424/p/10427958.html