Python-使用matplotlib组织3个子图 [英] Python - Organisation of 3 subplots with matplotlib

查看:31
本文介绍了Python-使用matplotlib组织3个子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试为我的图生成 3 个子图.它似乎有效,但并不完全一致.下图和下图的宽度如何相同?

  plt.close()无花果= plt.figure()ax1 = plt.subplot(211)ax1.plot(cycles,np.asarray(df_int),'wo',label ='')ax1.plot(cycles,np.asarray(df_proj),'k+',label='')ax1.legend(loc=2)#ax1.subplots_adjust(left=0.15)ax1.set_ylim(0.001,0.004)ax2 = plt.subplot(223)i2 = ax2.imshow(c,cmap = plt.cm.hot,vmin = -5,vmax = 5);#plt.colorbar(); plt.clim(-5,5)cb2 = plt.colorbar(i2,ax = ax2,ticks = [-5,0,5],分数= 0.046,pad = 0.04,format ='%.2f')cb2.ax.tick_params(labelsize = 12)ax3 = plt.subplot(224)i3 = ax3.imshow(residue_proj,cmap = plt.cm.hot,vmin = -5,vmax = 5);#plt.colorbar(); plt.clim(-5,5);cb3 = plt.colorbar(i3,ax = ax3,ticks = [-5,0,5],fraction = 0.046,pad = 0.04,format ='%.2f')cb3.ax.tick_params(labelsize=12)plt.savefig('test.png',dpi = 500,bbox_inches ='tight',pad_inches = 0.1)#plt.show()

解决方案

如果您使用 GridSpec

将 numpy 导入为 np导入matplotlib.pylab作为pl导入 matplotlib.gridspec 作为 gridspec# 创建 2x2 子图gs = gridspec.GridSpec(2,2)pl.figure()ax = pl.subplot(gs [0,0])#第0行,第0行pl.plot([0,1])ax = pl.subplot(gs [0,1])#第0行,第1行pl.plot([0,1])ax = pl.subplot(gs [1,:])#第1行,跨所有列pl.plot([0,1])

请参阅:

I try to generate 3 subplots for my figure. It seems work but it is not perfectly aligned. How to have the same width between the figure at the bottom et the figures at below?

plt.close()
fig = plt.figure()

ax1 = plt.subplot(211)
ax1.plot(cycles,np.asarray(df_int),'wo',label='')
ax1.plot(cycles,np.asarray(df_proj),'k+',label='')
ax1.legend(loc=2)
#ax1.subplots_adjust(left=0.15)
ax1.set_ylim(0.001,0.004)

ax2 = plt.subplot(223)
i2=ax2.imshow(c,cmap=plt.cm.hot, vmin=-5, vmax=5);#plt.colorbar();plt.clim(-5,5)
cb2=plt.colorbar(i2,ax=ax2,ticks=[-5,0,5],fraction=0.046, pad=0.04,format='%.2f')
cb2.ax.tick_params(labelsize=12)



ax3 = plt.subplot(224)
i3=ax3.imshow(residue_proj,cmap=plt.cm.hot, vmin=-5, vmax=5);#plt.colorbar();plt.clim(-5,5);
cb3=plt.colorbar(i3,ax=ax3,ticks=[-5,0,5],fraction=0.046, pad=0.04,format='%.2f')
cb3.ax.tick_params(labelsize=12)

plt.savefig('test.png', dpi=500,  bbox_inches='tight', pad_inches=0.1)
#plt.show()

解决方案

This is probably a lot easier if you use GridSpec:

import numpy as np
import matplotlib.pylab as pl
import matplotlib.gridspec as gridspec

# Create 2x2 sub plots
gs = gridspec.GridSpec(2, 2)

pl.figure()
ax = pl.subplot(gs[0, 0]) # row 0, col 0
pl.plot([0,1])

ax = pl.subplot(gs[0, 1]) # row 0, col 1
pl.plot([0,1])

ax = pl.subplot(gs[1, :]) # row 1, span all columns
pl.plot([0,1])

See: http://matplotlib.org/users/gridspec.html

这篇关于Python-使用matplotlib组织3个子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆