詳解Matplotlib繪圖之屬性設(shè)置
關(guān)于Python數(shù)據(jù)分析在數(shù)學(xué)建模中的更多相關(guān)應(yīng)用:Python數(shù)據(jù)分析在數(shù)學(xué)建模中的應(yīng)用匯總(持續(xù)更新中!)
(1)、導(dǎo)入庫
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)、顏色、標(biāo)記和線型
#顏色、標(biāo)記和線型 plt.figure() ##linestyle設(shè)置線型,color設(shè)置顏色,marker設(shè)置設(shè)置連接點 plt.plot(numpy.random.randn(30).cumsum(),linestyle='--',color='g',marker='o')
(5)、設(shè)置標(biāo)題、軸標(biāo)簽,刻度以及刻度標(biāo)簽
#設(shè)置標(biāo)題、軸標(biāo)簽,刻度以及刻度標(biāo)簽 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]) #設(shè)置x軸刻度 ax.set_yticks([-20,-10,0,10,20]) #設(shè)置y軸刻度 ax.set_title('My first matplotlib plot') #設(shè)置標(biāo)題 ax.set_xlabel('Xtages') #設(shè)置x軸標(biāo)簽 ax.set_ylabel('Ytages') #設(shè)置y軸標(biāo)簽
(6)、添加圖例
#添加圖例 fig = plt.figure() ax = fig.add_subplot(1,1,1) ax.plot(numpy.random.randn(1000).cumsum(),'k',label='one') #label標(biāo)簽用于添加圖例 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標(biāo)簽用于添加圖例 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)、圖表的導(dǎo)出
#圖表的保存 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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
pandas中關(guān)于apply+lambda的應(yīng)用
本文主要介紹了pandas中關(guān)于apply+lambda的應(yīng)用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02Python使用win32com模塊實現(xiàn)數(shù)據(jù)庫表結(jié)構(gòu)自動生成word表格的方法
這篇文章主要介紹了Python使用win32com模塊實現(xiàn)數(shù)據(jù)庫表結(jié)構(gòu)自動生成word表格的方法,結(jié)合實例形式分析了win32com模塊下載、連接mysql、查詢獲取表結(jié)構(gòu)以及使用win32com生成word表格的相關(guān)操作技巧,需要的朋友可以參考下2018-07-07numpy中np.nanmax和np.max的區(qū)別及坑
這篇文章主要介紹了numpy中np.nanmax和np.max的區(qū)別及坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02Django中模版的子目錄與include標(biāo)簽的使用方法
這篇文章主要介紹了Django中模版的子目錄與include標(biāo)簽的使用方法,有利于Python的Django框架的模版布局,需要的朋友可以參考下2015-07-07python基于gevent實現(xiàn)并發(fā)下載器代碼實例
這篇文章主要介紹了python基于gevent實現(xiàn)并發(fā)下載器代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11