這里會整理些matplotlib繪圖的小知識點(diǎn),可能比較雜糕篇。
1. subplot
前面,我們有簡單的說過subplot酌心,通過他拌消,我們可以在一個figure上,分割多個區(qū)域分別去繪圖安券,
這里說一個最常用的方法
matplotlib.pyplot.subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)
Create a figure and a set of subplots
This utility wrapper makes it convenient to create common layouts of subplots, including the enclosing figure object, in a single call
import numpy as np
import matplotlib.pyplot as plt
#初始化2行2列的figure
f,axes = plt.subplots(2,2)
#這樣通過下標(biāo)可以快速的獲取axes
axes[0,1].hist(np.random.randn(100),bins=20)
plt.show()
我們還可以設(shè)置墩崩,是否共享x軸y軸
調(diào)整subplot周圍的間距
matplotlib.pyplot.subplots_adjust(*args, **kwargs)
subplots_adjust(left=None, bottom=None, right=None, top=None,
wspace=None, hspace=None)
left = 0.125 # the left side of the subplots of the figure
right = 0.9 # the right side of the subplots of the figure
bottom = 0.1 # the bottom of the subplots of the figure
top = 0.9 # the top of the subplots of the figure
wspace = 0.2 # the amount of width reserved for blank space between subplots,
# expressed as a fraction of the average axis width
hspace = 0.2 # the amount of height reserved for white space between subplots,
# expressed as a fraction of the average axis height
import numpy as np
import matplotlib.pyplot as plt
#初始化2行2列的figure
f,axes = plt.subplots(2,2)
data=np.random.randn(10).cumsum()
axes[0,0].plot(data,marker='o')
axes[0,0].set_title('default')
#drawstyle
[‘default’ | ‘steps’ | ‘steps-pre’ | ‘steps-mid’ | ‘steps-post’]
axes[0,1].plot(data,drawstyle='steps-pre',marker='o')
axes[0,1].set_title('steps-pre')
axes[1,0].plot(data,drawstyle='steps-mid',marker='o')
axes[1,0].set_title('steps-mid')
axes[1,1].plot(data,drawstyle='steps-post',marker='o')
axes[1,1].set_title('steps-post')
#plt.subplots_adjust(hspace=0)
plt.show()
我們這里,可以設(shè)置軸要顯示的刻度
比如侯勉,上面顯示的是0鹦筹,2,4址貌,6铐拐,8
#設(shè)置顯示的x軸的值
axes[0,0].set_xticks([0,5,10])
#將上面的值徘键,替換為標(biāo)簽
axes[0,0].set_xticklabels(['one','two','three'])
好了,這里先補(bǔ)充這些遍蟋,后面有的話繼續(xù)吹害,下一篇整理下pandas中繪圖的方法