matplotlib例項筆記

2022-09-03 18:36:10 字數 4019 閱讀 1508

下面的圖型是在一幅畫布上建立的四個球員相關資料的極座標圖

關於這個圖的**如下:

1

#_*_coding:utf-8_*_

2import

numpy as np

3import

matplotlib.pyplot as plt

4from matplotlib.font_manager import

fontproperties

5 plt.style.use('

ggplot')

67 font=fontproperties(fname=r'

c:\windows\fonts\simsun.ttc

',size=12)8#

本行是為了有效顯示中文的字型與大小

910 ability_size=6

11 ability_label=['

進攻','

防守','

盤帶','

速度','

體力','射術'

]1213#

建立每個極座標的位置

14 ax1=plt.subplot(221,projection='

polar')

15 ax2=plt.subplot(222,projection='

polar')

16 ax3=plt.subplot(223,projection='

polar')

17 ax4=plt.subplot(224,projection='

polar')

1819 player=

2526 theta=np.linspace(0,2*np.pi,6,endpoint=false)

2829

#下面分別畫四個球員的能力極座標圖

30 player['mm

'],player['m'

][0])

31 ax1.plot(theta,player['

m'],'r'

)32 ax1.fill(theta,player['

m'],'

r',alpha=0.3)

33ax1.set_xticks(theta)

34 ax1.set_xticklabels(ability_label,fontproperties=font)

35 ax1.set_title('

梅西',fontproperties=font,color='

r',size=20)

36 ax1.set_yticks([20,40,60,80,100])

3738 player['hh

'],player['h'

][0])

39 ax2.plot(theta,player['

h'],'g'

)40 ax2.fill(theta,player['

h'],'

g',alpha=0.3)

41ax2.set_xticks(theta)

42 ax2.set_xticklabels(ability_label,fontproperties=font)

43 ax2.set_title('

哈維',fontproperties=font,color='

g',size=20)

44 ax2.set_yticks([20,40,60,80,100])

4546 player['pp

'],player['p'

][0])

47 ax3.plot(theta,player['

p'],'b'

)48 ax3.fill(theta,player['

p'],'

b',alpha=0.3)

49ax3.set_xticks(theta)

50 ax3.set_xticklabels(ability_label,fontproperties=font)

51 ax3.set_title('

皮克',fontproperties=font,color='

b',size=20)

52 ax3.set_yticks([20,40,60,80,100])

5354 player['qq

'],player['q'

][0])

55 ax4.plot(theta,player['

q'],'y'

)56 ax4.fill(theta,player['

q'],'

y',alpha=0.3)

57ax4.set_xticks(theta)

58 ax4.set_xticklabels(ability_label,fontproperties=font)

59 ax4.set_title('

切赫',fontproperties=font,color='

y',size=20)

60 ax4.set_yticks([20,40,60,80,100])

6162 plt.show()

上述**也可以利用for迴圈簡化一下

1

#_*_coding:utf-8_*_

2import

numpy as np

3import

matplotlib.pyplot as plt

4from matplotlib.font_manager import

fontproperties

5 plt.style.use('

ggplot')

67 font=fontproperties(fname=r'

c:\windows\fonts\simsun.ttc

',size=12)

89 ability_size=6

10 ability_label=['

進攻','

防守','

盤帶','

速度','

體力','射術'

]1112 ax1=plt.subplot(221,projection='

polar')

13 ax2=plt.subplot(222,projection='

polar')

14 ax3=plt.subplot(223,projection='

polar')

15 ax4=plt.subplot(224,projection='

polar')

1617 player=

2324 theta=np.linspace(0,2*np.pi,6,endpoint=false)

2627 color4=['

r','

b','

g','y'

]28 player4=['

m','

h','

p','q'

]29 ax=[ax1,ax2,ax3,ax4]

30 name=['

梅西','

哈維','

皮克','切赫'

]31for i in range(4):

33ax[i].plot(theta,player[player4[i]],color4[i])

34 ax[i].fill(theta,player[player4[i]],color4[i],alpha=0.3)

35ax[i].set_xticks(theta)

36 ax[i].set_xticklabels(ability_label,fontproperties=font)

37 ax[i].set_title(name[i],fontproperties=font,color=color4[i],size=20)

38 ax[i].set_yticks([20,40,60,80,100])

3940 plt.show()

Matplotlib學習筆記

在最開始接觸python科學計算的時候,就知道了matplotlib這個繪相簿。個人是比較喜歡這種視覺化的工具,照我看,gui這種理念幾乎是劃時代的。如果說numpy是用來處理資料,那麼matplotlib就是用來展示資料的,抽象的資料,以圖表的形式展示出來,無論是對自己,還是對看到的人,接受起來都...

Matplotlib學習筆記(三)

import matplotlib.pyplot as plt import numpy as np x np.linspace 3,3,50 y 2 x 1 plt.figure num 1,figsize 8,5 plt.plot x,y ax plt.gca ax.spines right s...

Matplotlib學習筆記(四)

import matplotlib.pyplot as plt import numpy as np n 1024 x np.random.normal 0,1,n y np.random.normal 0,1,n t np.arctan2 y,x for color value plt.scatt...