Python - 在tripcolor案例中的matplotlib子图

时间:2016-08-09 12:39:27

标签: python matplotlib delaunay

我试图组织并调整我使用tripcolor od Delaunay三角测量获得的三个子图。问题是我无法使用函数:plt.tight_layout(pad = 0.5,w_pad = 2.5,h_pad = 2.0)来设置窗口大小,在这种情况下它不起作用。 结果对应于: enter image description here

我希望窗口有方形...我的代码是:

import matplotlib.tri as tr
triang = tr.Triangulation(Xini, Yini)   

xmid = Xini[triang.triangles].mean(axis=1)
ymid = Yini[triang.triangles].mean(axis=1)

plt.figure()

ax1 = plt.subplot(131) # creates first axis
i1 =ax1.tripcolor(triang, Epst_eq2, shading='flat', cmap=plt.cm.hot)
ax1.set_xlim([-2.5,2.5])
ax1.set_ylim([-2.5,2.5])
# plt.title('tripcolor of Delaunay triangulation, flat shading')
plt.colorbar(i1,ax=ax1,ticks=np.linspace(0,0.005,3))

ax2 = plt.subplot(132) # creates first axis
ax2.tripcolor(triang, Epst_eq3, shading='flat', cmap=plt.cm.hot)
ax2.set_xlim([-2.5,2.5])
ax2.set_ylim([-2.5,2.5])

ax3 = plt.subplot(133) # creates first axis

ax3.tripcolor(triang, Epst_eq4, shading='flat', cmap=plt.cm.hot)

ax3.set_xlim([-2.5,2.5])
ax3.set_ylim([-2.5,2.5])

plt.savefig('test2.png',dpi=100)
plt.show()

1 个答案:

答案 0 :(得分:0)

最后我使用了gridspec,我可以轻松修改大小!

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

fig = plt.figure(figsize=(16, 6)) 

gs = gridspec.GridSpec(1, 3,width_ratios=[1.2,1,1])

ax1 = plt.subplot(gs[0])
i1 =ax1.tripcolor(triang, Epst_eq2, shading='flat', cmap=plt.cm.hot)
ax1.set_xlim([-2.5,2.5])
ax1.set_ylim([-2.5,2.5])
# plt.title('tripcolor of Delaunay triangulation, flat shading')
plt.colorbar(i1,ax=ax1,ticks=np.linspace(0,0.005,3))

ax2 = plt.subplot(gs[1])
ax2.tripcolor(triang, Epst_eq3, shading='flat', cmap=plt.cm.hot)
ax2.set_xlim([-2.5,2.5])
ax2.set_ylim([-2.5,2.5])

ax3 = plt.subplot(gs[2])
ax3.tripcolor(triang, Epst_eq4, shading='flat', cmap=plt.cm.hot)
ax3.set_xlim([-2.5,2.5])
ax3.set_ylim([-2.5,2.5])

plt.tight_layout()
plt.savefig('test2.png',dpi=100)
plt.show()