使用matplotlib绘制常用图表(3)-其他图表绘制

一、绘制三点图

 1 """
 2 三月份最高气温
 3 a = 
 4 [12,15,18,6,7,5,6,8,9,10,15,10,4,5,11,10,5,6,12,15,10,5,14,10,10,12,16,5,3,5,5,5,6]
 5 """
 6 
 7 
 8 from matplotlib import pyplot as plt
 9 from matplotlib import font_manager
10 
11 y = [12,15,18,6,7,5,6,8,9,10,15,10,4,5,11,10,5,6,12,15,10,5,14,10,10,12,16,5,3,5,6]
12 
13 x = range(1,32)
14 
15 #设置图像大小
16 plt.figure(figsize=(20,8),dpi = 80)
17 
18 plt.scatter(x,y,label='3月份')
19 
20 
21 #定义字体
22 my_font = font_manager.FontProperties(fname='C:\Windows\Fonts\FZSTK.TTF')
23 #x轴刻度列表
24 xticks_label = ['3月{}日'.format(i) for i in x]
25 
26 #将设置的格式写入x轴
27 plt.xticks(x[::3],xticks_label[::3],fontproperties = my_font,rotation = 45)
28 
29 #设置x轴y轴标题
30 plt.xlabel('日期',fontproperties = my_font)
31 plt.ylabel('温度',fontproperties = my_font)
32 
33 #图例
34 plt.legend(prop=my_font)
35 plt.show()
View Code

二、绘制柱形图

 1 '''
 2 a = ['流浪地球','疯狂的外星人','飞驰人生','大黄蜂','熊出没·原始时代','新喜剧之王']
 3 b = ['38.13','19.85','14.89','11.36','6.47','5.93']
 4 
 5 '''
 6 from matplotlib import pyplot as plt
 7 from matplotlib import font_manager
 8 
 9 a = ['流浪地球','疯狂的外星人','飞驰人生','大黄蜂','熊出没·原始时代','新喜剧之王']
10 b = ['38.13','19.85','14.89','11.36','6.47','5.93']
11 
12 my_font = font_manager.FontProperties(fname='C:\Windows\Fonts\FZSTK.TTF',size = 22)
13 
14 plt.figure(figsize=(20,8),dpi = 80)
15 
16 rects = plt.bar(range(len(a)),[float(i) for i in b],0.3,color = 'red')
17 
18 plt.xticks(range(len(a)),a,fontproperties = my_font)
19 
20 
21 #增加标注
22 for rect in rects:
23     height = rect.get_height()
24     plt.text(rect.get_x() + rect.get_width()/2,height+0.3,str(height),ha='center')
25 
26 plt.show()
View Code

三、横向柱状图

 1 #横向柱状图
 2 from matplotlib import pyplot as plt 
 3 from matplotlib import font_manager
 4 
 5 my_font= font_manager.FontProperties(fname='C:\Windows\Fonts\FZSTK.TTF',size = 18)
 6 
 7 a = ['流浪地球','疯狂的外星人','飞驰人生','大黄蜂','熊出没·原始时代','新喜剧之王']
 8 b = ['38.13','19.85','14.89','11.36','6.47','5.93']
 9 
10 plt.figure(figsize=(20,8),dpi = 80)
11 
12 rects = plt.barh(range(len(a)),[float(i) for i in b],height = 0.5, color = 'red')
13 
14 plt.yticks(range(len(a)),a,fontproperties = my_font)
15 
16 for rect in rects:
17     width = rect.get_width()
18     plt.text(width,rect.get_y()+0.5/2,str(width),va = 'center')
19 plt.show()
View Code

四、并列和罗列柱状图

 1 from matplotlib import pyplot
 2 from matplotlib import font_manager
 3 import numpy as np
 4 index = np.arange(4)
 5 BJ = [50,55,53,60]
 6 SH = [44,66,55,41]
 7 
 8 #并列
 9 plt.bar(index,BJ,width=0.3)
10 #plt.bar(index+0.3,SH,width=0.3,color = 'green')
11 #plt.xticks(index+0.3/2,index)
12 
13 #罗列
14 plt.bar(index,SH,bottom = BJ,width = 0.3,color='green')
15 plt.show()
View Code

猜你喜欢

转载自www.cnblogs.com/luweilehei/p/11416193.html