【问题标题】:Set colorbar resized beside each subplots在每个子图旁边设置颜色条调整大小
【发布时间】:2016-06-01 12:30:12
【问题描述】:

我正在写我的科学文章,但我的子图有问题。 不幸的是,我得到的颜色条比图表高太多了。

我读了这篇文章:Matplotlib: same height for colorbar as for plot

但我找不到覆盖脚本以调整颜色条大小的方法。

这是我尝试调整大小的脚本:

fig3, (ax1, ax2, ax3) = plt.subplots(1,3)

fig = plt.gcf()
fig.set_size_inches(16, 9)

convolution_locale = convolve(RotatePlot, Gaussian2DKernel(stddev=4)) # AFFICHAGE DE LA CARTE DE DENSITE CONVOLUEE POUR 2'
fig_smoothed_heatmap_locale = ax1.imshow(convolution_locale, interpolation='nearest')
ax1.set_title("Carte de densite convoluee 2'")
ax1.set_xlabel("X (arcmin)")
ax1.set_ylabel("Y (arcmin)")
divider = make_axes_locatable(ax1)
cax1 = divider.append_axes("right", size="5%", pad=0.05)
fig3.colorbar(fig_smoothed_heatmap_locale,cax=cax1)
ax1.invert_yaxis()


convolution_grande = convolve(RotatePlot, Gaussian2DKernel(stddev=32)) # AFFICHAGE DE LA CARTE DE DENSITE CONVOLUEE POUR 8'
fig_smoothed_heatmap_grande = ax2.imshow(convolution_grande, interpolation='nearest')
ax2.set_title("Carte de densite convoluee 16'")
ax2.set_xlabel("X (arcmin)")
ax2.set_ylabel("Y (arcmin)")
divider = make_axes_locatable(ax1)
cax2 = divider.append_axes("right", size="5%", pad=0.05)
fig3.colorbar(fig_smoothed_heatmap_grande,cax=cax2)
ax2.invert_yaxis()

convolution_diff = convolution_locale - convolution_grande # AFFICHAGE DE LA CARTE DE DENSITE CONVOLUEE 2' - 8'
fig_smoothed_tab_diff = ax3.imshow(convolution_diff, interpolation='nearest')
ax3.set_title("Carte 2' - Carte 16'")
ax3.set_xlabel("X (arcmin)")
ax3.set_ylabel("Y (arcmin)")
divider = make_axes_locatable(ax1)
cax3 = divider.append_axes("right", size="5%", pad=0.05)
fig3.colorbar(fig_smoothed_tab_diff,cax=cax3)
ax3.invert_yaxis()

# Create space for labels between subplots
fig3.tight_layout()

fig3.savefig(outname3)

这就是我得到的:

如果您有解决方案,谢谢! :)

【问题讨论】:

  • 我看到一个颜色条与每个子图完全一样高......到底什么不适合你?
  • @gboffi 是的,尺寸变好了,但现在我将 3 个颜色条放在同一个地方,而不是在每个子图旁边:/
  • 好的,因此,如果您有不同的问题,请关闭此问题并发布一个新问题(我建议采取此措施)或编辑您的问题以反映新问题...
  • 在看到 Tom 的回答后三思:如果 Tom 的回答适用于 NEW 问题,也许最好更新您的问题,以便 Q&A 顺利进行。
  • @gboffi 好的,我会用汤姆的回答更新我的问题;)谢谢!

标签: python matplotlib colorbar


【解决方案1】:

问题是当您创建 3 个 divider 实例时,您总是使用 ax1

将第二个和第三个dividers更改为使用ax2ax3,例如:

divider = make_axes_locatable(ax2)

这是你的整个脚本,带有固定的行:

fig3, (ax1, ax2, ax3) = plt.subplots(1,3)

fig = plt.gcf()
fig.set_size_inches(16, 9)

convolution_locale = convolve(RotatePlot, Gaussian2DKernel(stddev=4)) # AFFICHAGE DE LA CARTE DE DENSITE CONVOLUEE POUR 2'
fig_smoothed_heatmap_locale = ax1.imshow(convolution_locale, interpolation='nearest')
ax1.set_title("Carte de densite convoluee 2'")
ax1.set_xlabel("X (arcmin)")
ax1.set_ylabel("Y (arcmin)")
divider = make_axes_locatable(ax1)
cax1 = divider.append_axes("right", size="5%", pad=0.05)
fig3.colorbar(fig_smoothed_heatmap_locale,cax=cax1)
ax1.invert_yaxis()


convolution_grande = convolve(RotatePlot, Gaussian2DKernel(stddev=32)) # AFFICHAGE DE LA CARTE DE DENSITE CONVOLUEE POUR 8'
fig_smoothed_heatmap_grande = ax2.imshow(convolution_grande, interpolation='nearest')
ax2.set_title("Carte de densite convoluee 16'")
ax2.set_xlabel("X (arcmin)")
ax2.set_ylabel("Y (arcmin)")
divider = make_axes_locatable(ax2)          ### I changed this line
cax2 = divider.append_axes("right", size="5%", pad=0.05)
fig3.colorbar(fig_smoothed_heatmap_grande,cax=cax2)
ax2.invert_yaxis()

convolution_diff = convolution_locale - convolution_grande # AFFICHAGE DE LA CARTE DE DENSITE CONVOLUEE 2' - 8'
fig_smoothed_tab_diff = ax3.imshow(convolution_diff, interpolation='nearest')
ax3.set_title("Carte 2' - Carte 16'")
ax3.set_xlabel("X (arcmin)")
ax3.set_ylabel("Y (arcmin)")
divider = make_axes_locatable(ax3)          ### I changed this line too
cax3 = divider.append_axes("right", size="5%", pad=0.05)
fig3.colorbar(fig_smoothed_tab_diff,cax=cax3)
ax3.invert_yaxis()

# Create space for labels between subplots
fig3.tight_layout()

fig3.savefig(outname3)

【讨论】:

  • 非常感谢!我没有修改这一行,因为我的脚本有 1200 行,而我没有看到这一行。谢谢你的回答:)
猜你喜欢
  • 1970-01-01
  • 2018-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-02
  • 2019-09-15
  • 1970-01-01
  • 2013-08-14
相关资源
最近更新 更多