python matplotlib中的subplot函數(shù)使用詳解
python里面的matplotlib.pylot是大家比較常用的,功能也還不錯(cuò)的一個(gè)包。基本框架比較簡(jiǎn)單,但是做一個(gè)功能完善且比較好看整潔的圖,免不了要網(wǎng)上查找一些函數(shù)。于是,為了節(jié)省時(shí)間,可以一勞永逸。我把常用函數(shù)作了一個(gè)總結(jié),最后寫(xiě)了一個(gè)例子,以后基本不用怎么改了。
一、作圖流程:
1.準(zhǔn)備數(shù)據(jù), , 3作圖, 4定制, 5保存, 6顯示
1.數(shù)據(jù)可以是numpy數(shù)組,也可以是list
2創(chuàng)建畫(huà)布:
import matplotlib.pyplot as plt #figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True) #num:圖像編號(hào)或名稱,數(shù)字為編號(hào) ,字符串為名稱 #figsize:指定figure的寬和高,單位為英寸; #dpi參數(shù)指定繪圖對(duì)象的分辨率,即每英寸多少個(gè)像素,缺省值為80 ,1英寸等于2.5cm,A4紙是 21*30cm的紙張 #facecolor:背景顏色 #edgecolor:邊框顏色 #frameon:是否顯示邊 fig = plt.figure() fig = plt.figure(figsize=(8,6), dpi=80) fig.add_axes() fig, axes = plt.subplos(nrows = 2, ncols = 2) #axes是長(zhǎng)度為4的列表
3、作圖路線
一維數(shù)據(jù):
axes[0, 0].plot(x, y) axes[0,1].bar([1,2,3], [2,4,8]) axes[0,2].barh([1,2,3], [2,4,8]) axes[1,0].axhline(0.45) axes[1, 1].scatter(x, y) axes[1,2].axvline(0.65) axes[2,0].fill(x,y, color = 'blue') axes[2,1].fill_between(x,y, color = 'blue') axes[2,2].violinplot(y) axes[0,3].arrow(0,0,0.5,0.5) axes[1,3].quiver(x,y)
4, 定制
plt.plot(x,y, alpha=0.4, c = 'blue', maker = 'o')
#顏色,標(biāo)記,透明度
# 顯示數(shù)學(xué)文本
t = np.arange(0.0, 2.0, 0.01)
s = np.sin(2*np.pi*t)
plt.plot(t,s)
plt.title(r'$\alpha_i > \beta_i$', fontsize=20)
plt.text(1, -0.6, r'$\sum_{i=0}^\infty x_i$', fontsize=20)
plt.text(0.6, 0.6, r'$\mathcal{A}\mathrm{sin}(2 \omega t)$',
fontsize=20)
plt.xlabel('time (s)')
plt.ylabel('volts (mV)')
fig = plt.figure()
fig.suptitle('bold figure suptitle', fontsize=14, fontweight='bold')
ax = fig.add_subplot(111)
fig.subplots_adjust(top=0.85)
ax.set_title('axes title')
ax.set_xlabel('xlabel')
ax.set_ylabel('ylabel')
ax.text(3, 8, 'boxed italics text in data coords', style='italic',
bbox={'facecolor':'red', 'alpha':0.5, 'pad':10})
ax.text(2, 6, r'an equation: $E=mc^2$', fontsize=15)
ax.text(3, 2, u'unicode: Institut f\374r Festk\366rperphysik')
ax.text(0.95, 0.01, 'colored text in axes coords',
verticalalignment='bottom', horizontalalignment='right',
transform=ax.transAxes,
color='green', fontsize=15)
ax.plot([2], [1], 'o')
# 注釋
ax.annotate('我是注釋啦', xy=(2, 1), xytext=(3, 4),color='r',size=15,
arrowprops=dict(facecolor='g', shrink=0.05))
ax.axis([0, 10, 0, 10])

5, 保存顯示
plt.savefig("1.png")
plt.savefig("1.png", trainsparent =True)
plt.show()
二、部分函數(shù)使用詳解:
1, fig.add_subplot(numrows, numcols, fignum) ####三個(gè)參數(shù),分別代表子圖的行數(shù),列數(shù),圖索引號(hào)。 eg: ax = fig.add_subplot(2, 3, 1) (or ,ax = fig.add_subplot(231))
2, plt.subplots()使用
x = np.linspace(0, 2*np.pi,400)
y = np.sin(x**2)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('Simple plot')
# Creates two subplots and unpacks the output array immediately
#fig = plt.figure(figsize=(6,6))
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(x, y)
ax1.set_title('Sharing Y axis')
ax2.scatter(x, y)
# Creates four polar axes, and accesses them through the returned array
fig, axes = plt.subplots(2, 2, subplot_kw=dict(polar=True))
axes[0, 0].plot(x, y)
axes[1, 1].scatter(x, y)
# Share a X axis with each column of subplots
plt.subplots(2, 2, sharex='col')
# Share a Y axis with each row of subplots
plt.subplots(2, 2, sharey='row')
# Share both X and Y axes with all subplots
plt.subplots(2, 2, sharex='all', sharey='all')
# Note that this is the same as
plt.subplots(2, 2, sharex=True, sharey=True)
# Creates figure number 10 with a single subplot
# and clears it if it already exists.
fig, ax=plt.subplots(num=10, clear=True)
3.plt.legend()
plt.legend(loc='String or Number', bbox_to_anchor=(num1, num2)) plt.legend(loc='upper center', bbox_to_anchor (0.6,0.95),ncol=3,fancybox=True,shadow=True) #bbox_to_anchor被賦予的二元組中,第一個(gè)數(shù)值用于控制legend的左右移動(dòng),值越大越向右邊移動(dòng),第二個(gè)數(shù)值用于控制legend的上下移動(dòng),值越大,越向上移動(dòng)

以上這篇python matplotlib中的subplot函數(shù)使用詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python使用pylab庫(kù)實(shí)現(xiàn)繪制直方圖功能示例
這篇文章主要介紹了Python使用pylab庫(kù)實(shí)現(xiàn)繪制直方圖功能,結(jié)合實(shí)例形式分析了Python數(shù)據(jù)讀取、遍歷以及基于pylab庫(kù)繪制直方圖的相關(guān)操作技巧,需要的朋友可以參考下2018-06-06
使用TensorFlow直接獲取處理MNIST數(shù)據(jù)方式
今天小編就為大家分享一篇使用TensorFlow直接獲取處理MNIST數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
Python基于更相減損術(shù)實(shí)現(xiàn)求解最大公約數(shù)的方法
這篇文章主要介紹了Python基于更相減損術(shù)實(shí)現(xiàn)求解最大公約數(shù)的方法,簡(jiǎn)單說(shuō)明了更相減損術(shù)的概念、原理并結(jié)合Python實(shí)例形式分析了基于更相減損術(shù)實(shí)現(xiàn)求解最大公約數(shù)的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-04-04
使用python在校內(nèi)發(fā)人人網(wǎng)狀態(tài)(人人網(wǎng)看狀態(tài))
人人網(wǎng)怎么發(fā)狀態(tài)?下面使用python實(shí)現(xiàn)這個(gè)功能,大家參考使用吧2014-02-02
Python中卷積神經(jīng)網(wǎng)絡(luò)(CNN)入門(mén)教程分分享
卷積神經(jīng)網(wǎng)絡(luò)(Convolutional Neural Networks, CNN)是一類(lèi)特別適用于處理圖像數(shù)據(jù)的深度學(xué)習(xí)模型,本文介紹了如何使用Keras創(chuàng)建一個(gè)簡(jiǎn)單的CNN模型,并用它對(duì)手寫(xiě)數(shù)字進(jìn)行分類(lèi),需要的可以參考一下2023-05-05

