Matplotlib commonly used to draw a graph (3) - Other graphing

First, draw the three-point chart

1  "" " 
2  March maximum temperature
 . 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 is  
13 is X = Range (1,32 )
 14  
15  # set the image size 
16 plt.figure (figsize = (20,8), dpi = 80 )
 . 17 
18 is plt.scatter (X, Y, label = ' . 3 the month ' )
 . 19  
20 is  
21 is  # custom font 
22 is my_font = font_manager.FontProperties (fname = " C: \ the Windows \ Fonts \ FZSTK.TTF ' )
 23 is  # X-axis scale listing 
24 xticks_label = [ ' March day {} ' .format (I) for I in x]
 25  
26 is  # format written to the x-axis set 
27 plt.xticks (x [::. 3], xticks_label [::. 3] , fontproperties = my_font, rotation = 45 )
 28  
29  # is set to x axis y axis title 
30 plt.xlabel (' Date ' , fontproperties = my_font)
 31 is plt.ylabel ( ' temperature ' , fontproperties = my_font)
 32  
33 is  # legend 
34 is plt.legend (prop = my_font)
 35 plt.show ()
View Code

Second, draw bar chart

1  '' ' 
2  A = [' wandering the Earth ',' crazy aliens', 'speeding Life', 'Hornet', 'bear-infested primitive times,' 'the new King of Comedy']
 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 = [ ' wandering Earth ' , ' crazy aliens ' , ' speeding life ' , ' Hornet ' , 'Bear haunt · primitive times ' , ' New King of Comedy ' ]
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

Third, the lateral bar graph

1  # transverse bar graph 
2  from matplotlib Import pyplot AS PLT 
 . 3  from matplotlib Import font_manager
 . 4  
. 5 my_font = font_manager.FontProperties (fname = ' C: \ the Windows \ Fonts \ FZSTK.TTF ' , size = 18 is )
 . 6  
. 7 A = [ ' wandering the Earth ' , ' crazy aliens ' , ' speeding life ' , ' Hornet ' , ' bear-infested primitive times ' , ' new King of Comedy ' ]
 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

Fourth, the listed side by side and histograms

 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

 

Guess you like

Origin www.cnblogs.com/luweilehei/p/11416193.html