Python-74-matplotlib套件-畫圖模塊

matplotlib是python中強大的畫圖模塊

  • 折線圖
  • 圓餅圖

Anaconda 預設已有matplotlib套件

如果未安裝時,首先確保已經安裝python,然後用pip來安裝matplotlib模塊。

進入到cmd(右鍵系統管理員身分執行),建議執行python -m pip install -U pip setuptools進行升級。

接著鍵入python -m pip install matplotlib進行自動的安裝,系統會自動下載安裝包。

安裝完成後,可以用python -m pip list查看本機的安裝的所有模塊,確保matplotlib已經安裝成功。


 

編輯器記得重新啟動,如果還無法顯示圖時

Tools > Preferences > iPython console > Graphics > Graphics backend > Automatic

編輯器記得重新啟動。


折線圖

 

import matplotlib.pyplot as mpt
mpt.xlim(1,6) #x軸 (1~6)
mpt.ylim(5000,10000)#y軸(5000~10000)

datax=[1,2,3,4,5,6] #x軸點的位置
datay=[7800,6500,7300,8120,6800,7650]#y軸點的位置
mpt.plot(datax,datay) #畫折線圖(x軸點的位置,y軸點的位置)

 

 

 

 

import matplotlib.pyplot as mpt
mpt.xlim(1,6)
mpt.ylim(5000,10000)

datax=[1,2,3,4,5,6]
datay=[7800,6500,7300,8120,6800,7650]
mpt.plot(datax,datay,label="Taipei")

infox=[1,2,3,4,5,6]
infoy=[5600,6730,6320,5650,7750,8680]
mpt.plot(infox,infoy,label="Elan")

mpt.legend()#圖例
mpt.show

設定屬性 顏色、線寬、線條樣式

文字屬性:字體、大小

import matplotlib.pyplot as mpt

from matplotlib.font_manager import FontProperties

mpt.xlim(1,6)
mpt.ylim(5000,10000)

datax =[1,2,3,4,5,6]
datay =[7800,6500,7300,8130,6800,7500]
mpt.plot(datax,datay,label="Taipei",color="green")

infox =[1,2,3,4,5,6]
infoy =[5670,6780,6324,7680,5600,7800]
mpt.plot(infox,infoy,label="elan",linewidth=8,linestyle="--")

mpt.legend()
font_set = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=12)
mpt.title("各分店銷售圖",fontproperties=font_set)

mpt.show

圓餅圖

 

import matplotlib.pyplot as mpt

labels=["food","clothes","live","play"]
sizes=[20,10,20,15]
explode=(0,0.2,0,0)
color=["green","blue","yellow","pink"]

mpt.pie(sizes,explode=explode,autopct="%2.2f%%",startangle=90,colors=color,shadow="True",labels=labels)

mpt.legend() #圖例
mpt.show

 

Yiru@Studio - 關於我 - 意如