Python - 在每个绘图的多个绘图中添加颜色条

标签 python matplotlib colorbox subplot

我在一个图形上有多个图。我想为每个结果添加颜色条。这些颜色条必须与我的结果具有相同的高度,我想将颜色条上显示的数字限制为 3 个值(以便获得可读的数字)。 enter image description here

我的代码是:

fig, axes = plt.subplots(nrows=1, ncols=3)

plt.tight_layout(pad=0.5, w_pad=2.5, h_pad=2.0)
ax1 = plt.subplot(131) # creates first axis
ax1.set_xticks([0,2000,500,1000,1500])
ax1.set_yticks([0,2000,500,1000,1500])
ax1.imshow(U,cmap='hot',extent=(X.min(),2000,Y.min(),2000))
ax1.set_title("$ \mathrm{Ux_{mes} \/ (pix)}$")
ax2 = plt.subplot(132) # creates second axis
ax2.set_xticks([0,2000,500,1000,1500])
ax2.set_yticks([0,2000,500,1000,1500])
ax2.imshow(UU,cmap='hot',extent=(X.min(),2000,Y.min(),2000))
ax2.set_title("$\mathrm{Ux_{cal} \/ (pix)}$")
ax3 = plt.subplot(133) # creates first axis
ax3.set_xticks([0,2000,500,1000,1500])
ax3.set_yticks([0,2000,500,1000,1500])
ax3.imshow(resU,cmap='hot',extent=(X.min(),2000,Y.min(),2000))
ax3.set_title("$\mathrm{\mid Ux - Ux \mid \/ (pix)}$ ")
plt.show()

我尝试添加:“fig.colorbar(U, axes=ax1,fraction=0.046, pad=0.04)” 但它不起作用...

最佳答案

按照@plonser 的回答,

tick = np.linspace(min(your_variable),max(your_variable),3)

plt.tight_layout(pad=0.5, w_pad=2.5, h_pad=2.0)
ax1 = plt.subplot(131) # creates first axis
ax1.set_xticks([0,2000,500,1000,1500])
ax1.set_yticks([0,2000,500,1000,1500])
i1 = ax1.imshow(U,cmap='hot',extent=(X.min(),2000,Y.min(),2000))
plt.colorbar(i1,ax=ax1,ticks=tick)

ax1.set_title("$ \mathrm{Ux_{mes} \/ (pix)}$")
ax2 = plt.subplot(132) # creates second axis
ax2.set_xticks([0,2000,500,1000,1500])
ax2.set_yticks([0,2000,500,1000,1500])
i2=ax2.imshow(UU,cmap='hot',extent=(X.min(),2000,Y.min(),2000))
ax2.set_title("$\mathrm{Ux_{cal} \/ (pix)}$")
plt.colorbar(i2,ax=ax2,ticks=tick)

ax3 = plt.subplot(133) # creates first axis
ax3.set_xticks([0,2000,500,1000,1500])
ax3.set_yticks([0,2000,500,1000,1500])
i3 = ax3.imshow(resU,cmap='hot',extent=(X.min(),2000,Y.min(),2000))
ax3.set_title("$\mathrm{\mid Ux - Ux \mid \/ (pix)}$ ")
plt.colorbar(i3,ax=ax3,ticks=tick)

plt.show()

当您指定时,您使用 ax=ax1 而不是 axes=ax1。此外,要限制 colorbar 中的项目数量,您可以使用 ticks 选项来实现。

关于Python - 在每个绘图的多个绘图中添加颜色条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30639555/

相关文章:

jquery - ie8 页面在加载完成之前挂起

Python - 使用 IDLE 和 if - else block 时出现缩进错误,在命令行上工作正常

python - multiprocessing.Process 实例完成后我的队列为空

python - Snakemake:通配符不存在时出错

python - 如何在numpy数组中转换视频

python - 如何阻止matplotlib GUI线程卡住?

javascript - 关闭 colorbox 并将父窗口重定向到特定的 url

python - 如何在 python 中渲染 3D 直方图?

python - 在 pyplot/python 中绘图时如何强制散点点真实像素值?

jquery - 动态添加类名到 Colorbox