How to plot pcolor colorbar in a different subplot - matplotlib

安稳与你 提交于 2019-11-30 08:48:56

colorbar() accepts a cax keyword argument that allows you to specify the axes object that the colorbar will be drawn on.

In your case, you would change your colorbar call to the following:

# colorbar
axes = plt.subplot2grid((4, 2), (0, 1), rowspan=3)
plt.colorbar(pc, cax=axes)

This will take up the whole space given by subplot2grid; you can adjust this to be more reasonable either by having the main axes take up many more columns than the colorbar axes, or by setting up the gridspec explicitly. For example, your figure may be easier to tweak with the following:

from matplotlib import gridspec
gs = gridspec.GridSpec(2, 2, height_ratios=(3, 1), width_ratios=(9, 1))

# first graph
axes = plt.subplot(gs[0,0])
pc = plt.pcolor(df1, cmap='jet')

# second graph
axes = plt.subplot(gs[1,0])
plt.pcolor(df2, cmap='Greys')

# colorbar
axes = plt.subplot(gs[0,1])
plt.colorbar(pc, cax=axes)

Then you can just change height_ratios and width_ratios to your liking.

I believe you should use ax instead of cax.

# colorbar
axes = plt.subplot2grid((4, 2), (0, 1), rowspan=3)
plt.colorbar(pc, ax=axes)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!