python - 设置每个子图旁边调整大小的颜色条

标签 python matplotlib colorbar

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

我读了这篇文章: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)

这就是我得到的:

enter image description here

如果有解决办法的话谢谢! :)

最佳答案

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

更改第二个和第三个divider以使用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)

关于python - 设置每个子图旁边调整大小的颜色条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37568708/

相关文章:

python字典难题

python - seaborn 中的白盒图? (覆盖层不透明)

python - matplotlib 中共享轴方形子图的新 pythonic 样式?

pandas - Matplotlib:如何使用 pandas plot api 在散点图中绘制一个空圆?

python - 如何为两个 gridspec 子图放置单个颜色条

matlab - Matlab 中小数字散点图的常见颜色条

python - 在子图之外放置多个颜色条 (matplotlib)

python - 绘制一个看起来像球体的球体

未调用python连接信号

python - 通过将 Stanford NER 与 Python 结合使用将名字和姓氏标记为一个标记