【Python數據可視化】Matplotlib學習筆記之pyplot(5)
簡介:Matplotlib 是一個Python的 2D繪圖庫,它以各種硬拷貝格式和跨平臺的交互式環境生成出版質量級別的圖形 。通過 Matplotlib,開發者可以僅需要幾行代碼,便可以生成繪圖,直方圖,功率譜,條形圖,錯誤圖,散點圖等。
編程環境:Python 3.8
pycharm 2017
需要安裝的庫:numpy,matplotlib
實例:
'''24.函數積分圖實例'''
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
def func(x):
return -(x-2)*(x-8)+40
x=np.linspace(0,10)
y=func(x)
fig, ax=plt.subplots()
plt.plot(x,y,'r',linewidth=2)
a=2
b=9
'''取消刻度顯示'''
ax.set_xticks([a,b])
ax.set_yticks([])
ax.set_xticklabels(['$a$','$b$'])#顯示爲數學公式的a,b
'''設置座標軸名稱'''
plt.figtext(0.9,0.06,'$x$',size=14)
plt.figtext(0.1,0.9,'$y$',size=14)
'''生產多邊形的點'''
ix=np.linspace(a,b)
iy=func(ix)
ixy=zip(ix,iy) #把xy拼接起來
'''繪製函數下陰影'''
verts=[(a,0)]+list(ixy)+[(b,0)]
poly=Polygon(verts,facecolor='0.7',edgecolor='0.5')
ax.add_patch(poly)
'''添加函數表達式'''
x_math=(a+b)*0.5
y_math=35
plt.text(x_math,y_math,r"$ \int_a^b (-(x-2)*(x-8)+40)dx $",
size=12,horizontalalignment='center')
'''調小y軸'''
plt.ylim(ymin=25)
plt.show()
'''25.散點條形圖實例'''
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot')
x=np.random.randn(200)
y=x+np.random.randn(200)*0.5
'''邊界構圖值'''
margin_border=0.1
width=0.6
margin_between=0.02
height=0.2
'''主散點圖畫布參數'''
left_s=margin_border
bottom_s=margin_border
width_s=width
height_s=width
'''上方直方圖畫布參數'''
left_x=margin_border
bottom_x=margin_border+width+margin_between
width_x=width
height_x=height
'''右方直方圖畫布參數'''
left_y=margin_border+width+margin_between
bottom_y=margin_border
width_y=height
height_y=width
'''繪製底圖'''
plt.figure(1,figsize=(8,8))
rect_s=[left_s,bottom_s,width_s,height_s]
rect_x=[left_x,bottom_x,width_x,height_x]
rect_y=[left_y,bottom_y,width_y,height_y]
axScatter=plt.axes(rect_s)
axHisX=plt.axes(rect_x)
axHisY=plt.axes(rect_y)
'''去掉附圖標籤'''
axHisX.set_xticks([])
axHisY.set_yticks([])
'''繪圖'''
axScatter.scatter(x,y,s=10)
bin_width=0.25
xymax=np.max([np.max(np.fabs(x)),np.max(np.fabs(y))])
lim=int(xymax/bin_width+2)*bin_width
axScatter.set_xlim(-lim,lim)
axScatter.set_ylim(-lim,lim)
bins=np.arange(-lim,lim+bin_width,bin_width)
axHisX.hist(x,bins=bins,color='green',edgecolor='white',alpha=0.7)
axHisY.hist(y,bins=bins,color='orange',
edgecolor='white',orientation='horizontal')
axHisX.set_xlim(axScatter.get_xlim())
axHisY.set_ylim(axScatter.get_xlim())
plt.title('Scatter and Hist')
plt.show()
'''26.球員能力雷達圖'''
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot')
'''顯示中文和顯示負號'''
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
ax1=plt.subplot(221,projection='polar')
ax2=plt.subplot(222,projection='polar')
ax3=plt.subplot(223,projection='polar')
ax4=plt.subplot(224,projection='polar')
'''能力標籤'''
ability_label=['進攻','防守','盤帶','速度','體力','射術']
ability_size=6
player={
'M': np.random.randint(size=ability_size, low=60, high=99),
'H': np.random.randint(size=ability_size, low=60, high=99),
'P': np.random.randint(size=ability_size, low=60, high=99),
'Q': np.random.randint(size=ability_size, low=60, high=99)
}
theta=np.linspace(0,2*np.pi,6,endpoint=False)
theta=np.append(theta,theta[0])
player['M']=np.append(player['M'],player['M'][0])
player['H']=np.append(player['H'],player['H'][0])
player['P']=np.append(player['P'],player['P'][0])
player['Q']=np.append(player['Q'],player['Q'][0])
'''繪製梅西能力圖'''
ax1.plot(theta,player['M'],'r',linewidth=1)
ax1.fill(theta,player['M'],'r',alpha=0.5)
ax1.set_xticks(theta)
ax1.set_xticklabels(ability_label)
ax1.set_title('梅西',color='r',position=(0.5,0.99))
ax1.set_yticks([20,40,60,80,100])
'''繪製哈維能力圖'''
ax2.plot(theta,player['H'],'g',linewidth=1)
ax2.fill(theta,player['H'],'g',alpha=0.5)
ax2.set_xticks(theta)
ax2.set_xticklabels(ability_label)
ax2.set_title('哈維',color='g',position=(0.5,0.99))
ax2.set_yticks([20,40,60,80,100])
'''繪製皮克能力圖'''
ax3.plot(theta,player['P'],'b',linewidth=1)
ax3.fill(theta,player['P'],'b',alpha=0.5)
ax3.set_xticks(theta)
ax3.set_xticklabels(ability_label)
ax3.set_title('皮克',color='b',position=(0.5,0.99))
ax3.set_yticks([20,40,60,80,100])
'''繪製切赫能力圖'''
ax4.plot(theta,player['Q'],'y',linewidth=1)
ax4.fill(theta,player['Q'],'y',alpha=0.5)
ax4.set_xticks(theta)
ax4.set_xticklabels(ability_label)
ax4.set_title('切赫',color='y',position=(0.5,0.99))
ax4.set_yticks([20,40,60,80,100])
plt.show()
------------------------------------END---------------------------------------