詳解Matplotlib繪圖之屬性設置
關于Python數(shù)據(jù)分析在數(shù)學建模中的更多相關應用:Python數(shù)據(jù)分析在數(shù)學建模中的應用匯總(持續(xù)更新中?。?br />
(1)、導入庫
import matplotlib.pyplot as plt import numpy
(2)、figure對象和subplot簡單運用
#figure對象 fig = plt.figure() #figure是圖象對象 ax1 = fig.add_subplot(2,2,1) #創(chuàng)建一個2*2的子圖,放在第一個位置 ax2 = fig.add_subplot(2,2,2) #創(chuàng)建一個2*2的子圖,放在第二個位置 ax3 = fig.add_subplot(2,2,3) #創(chuàng)建一個2*2的子圖,放在第三個位置 from numpy.random import randn plt.plot(randn(50).cumsum(),'k--') #'k--'告訴python要畫出黑色的虛線 ax1.hist(randn(100),bins=20,color='k',alpha=0.3)

(3)、調(diào)整subplot周圍的間距
#調(diào)整subplot周圍的間距
fig,axes = plt.subplots(2,2,sharex=True,sharey=True) #直接生成fiure對象和Axes實例(數(shù)組)
for i in range(2):
for j in range(2):
axes[i, j ].hist(numpy.random.randn(500),bins = 50,color='k',alpha=0.5) #遍歷創(chuàng)建圖
plt.subplots_adjust(left=None,bottom=None,right=None,top=None,wspace=0,hspace=0) #用于調(diào)整subplot周圍的間距

(4)、顏色、標記和線型
#顏色、標記和線型 plt.figure() ##linestyle設置線型,color設置顏色,marker設置設置連接點 plt.plot(numpy.random.randn(30).cumsum(),linestyle='--',color='g',marker='o')

(5)、設置標題、軸標簽,刻度以及刻度標簽
#設置標題、軸標簽,刻度以及刻度標簽
fig = plt.figure() #創(chuàng)建figure對象
ax = fig.add_subplot(1,1,1) #獲得Axes對象
ax.plot(numpy.random.randn(1000).cumsum()) #生成隨機數(shù)
ax.set_xticks([0,250,500,750,1000]) #設置x軸刻度
ax.set_yticks([-20,-10,0,10,20]) #設置y軸刻度
ax.set_title('My first matplotlib plot') #設置標題
ax.set_xlabel('Xtages') #設置x軸標簽
ax.set_ylabel('Ytages') #設置y軸標簽

(6)、添加圖例
#添加圖例 fig = plt.figure() ax = fig.add_subplot(1,1,1) ax.plot(numpy.random.randn(1000).cumsum(),'k',label='one') #label標簽用于添加圖例 ax.plot(numpy.random.randn(1000).cumsum(),'k',label='two') ax.plot(numpy.random.randn(1000).cumsum(),'k',label='three') ax.legend(loc='best') #loc選項可以選擇圖例的位置

(7)、添加注釋
#添加注釋
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(numpy.random.randn(1000).cumsum(),'k',label='one') #label標簽用于添加圖例
plt.annotate("Important value", (55,20), xycoords='data', #添加注釋的方法
xytext=(5, 38),
arrowprops=dict(arrowstyle='->'))

(8)、繪制常用圖形
#繪制常用圖形 fig = plt.figure() ax = fig.add_subplot(1,1,1) rect = plt.Rectangle((0.2,0.75),0.4,0.15,color='k',alpha=0.3) #長方形 circ = plt.Circle((0.7,0.2),0.15,color='b',alpha=0.3) #橢圓形 ax.add_patch(rect) #添加到圖版中 ax.add_patch(circ)

(9)、圖表的導出
#圖表的保存
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
rect = plt.Rectangle((0.2,0.75),0.4,0.15,color='k',alpha=0.3) #長方形
ax.add_patch(rect) #添加到圖版中
fig.savefig('figpath.png',dpi = 400,bbox_inches='tight') #dpi可以控制圖象的分辨率,bbox_inches可以剪除圖表的空白部分
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python使用win32com模塊實現(xiàn)數(shù)據(jù)庫表結構自動生成word表格的方法
這篇文章主要介紹了Python使用win32com模塊實現(xiàn)數(shù)據(jù)庫表結構自動生成word表格的方法,結合實例形式分析了win32com模塊下載、連接mysql、查詢獲取表結構以及使用win32com生成word表格的相關操作技巧,需要的朋友可以參考下2018-07-07
numpy中np.nanmax和np.max的區(qū)別及坑
這篇文章主要介紹了numpy中np.nanmax和np.max的區(qū)別及坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
python基于gevent實現(xiàn)并發(fā)下載器代碼實例
這篇文章主要介紹了python基于gevent實現(xiàn)并發(fā)下載器代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-11-11

