一 子图
- 使用
plt.subplots
绘制均匀状态下的子图 - 使用
GridSpec
绘制非均匀子图
二 子图上的方法
- 常用直线的画法为:
axhline, axvline, axline
(水平、垂直、任意方向) - 使用
grid
可以加灰色网格 - 使用
set_xscale
可以设置坐标轴的规度(指对数坐标等)
三 思考题
-
墨尔本1981年至1990年的每月温度情况
data = pd.read_csv('../六月matplotlib-main/data/layout_ex1.csv')
fig, axs = plt.subplots(2, 5, figsize=(10, 4), sharex=True, sharey=True)
fig.suptitle('样例1', size=40)
k = 0
for i in range(2):
for j in range(5):
axs[i][j].plot(np.arange(1,13),data['Temperature'][k*12:(k+1)*12])
axs[i][j].scatter(np.arange(1,13),data['Temperature'][k*12:(k+1)*12],marker='*')
axs[i][j].set_xlim(1,12)
axs[i][j].set_title('第%d年'%(1880+k+1))
k=k+1
fig.tight_layout()
- 用
np.random.randn(2, 150)
生成一组二维数据,使用两种非均匀子图的分割方法,做出该数据对应的散点图和边际分布图
data = np.random.randn(2, 150)
x = data[0]
y = data[1]
fig = plt.figure(figsize=(10, 10))
spec = fig.add_gridspec(nrows=2, ncols=2, width_ratios=[4,1], height_ratios=[1,4])
#sub1
ax1 = fig.add_subplot(spec[1,0])
ax1.scatter(x,y)
ax1.grid(True)
#sub2
ax2 = fig.add_subplot(spec[0,0],sharex=ax1)
ax2.hist(x,rwidth=5)
ax2.axis('off')
ax2.spines['top'].set_visible(False)
ax2.spines['right'].set_visible(False)
ax2.spines['bottom'].set_visible(False)
ax2.spines['left'].set_visible(False)
#sub3
ax3 = fig.add_subplot(spec[1,1],sharey=ax1)
ax3.hist(y,rwidth=5,orientation='horizontal')
ax3.axis('off')
ax3.spines['top'].set_visible(False)
ax3.spines['right'].set_visible(False)
ax3.spines['bottom'].set_visible(False)
ax3.spines['left'].set_visible(False)
fig.tight_layout()
plt.show( )
-
请思考如何用极坐标系画出类似的玫瑰图
N=7
r = [0.2,0.4,0.6,0.8,1,1.2,1.4]
width = 2*np.pi/len(r)
rad = np.cumsum([width]* N) - width/2
plt.subplot(projection='polar')
plt.bar(rad,r,alpha=0.75)
fig.tight_layout()