简体   繁体   English

调整子图中的颜色条

[英]Adjust colorbar in a subplot

My goal is to plot three Graphs as subplots and use just one colorbar for the three of them, I have tryed that by making a figure with 4 subplots like shown in the following Code: 我的目标是将三个图绘制为子图,并对其中三个仅使用一个颜色条,我尝试通过制作一个具有4个子图的图形,如以下代码所示:

fig=plt.figure()
ax1=plt.subplot(1,4,1)
im=ax1.contourf(  M1, 50,vmax=100,vmin=-100)
x0,x1 = ax1.get_xlim()
y0,y1 = ax1.get_ylim()
ax1.set_aspect((x1-x0)/(y1-y0))
ax2=plt.subplot(1,4,2,aspect=1)
im2=ax2.contourf(  M2, 50,vmax=100,vmin=-100)
ax3=plt.subplot(1,4,3,aspect=1)
im3=ax3.contourf( M3, 50,vmax=100,vmin=-100)
cb = plt.subplot(1,4,4)
im4=plt.colorbar(im3)
#cb.ax.set_visible(True)
plt.show()

The Matrices M1, M2 and M3 are previously computed in the Code, but I guess for my Problem that is not very important. 矩阵M1,M2和M3以前是在代码中计算出来的,但是对于我的问题,我猜不是很重要。 Also it is important that the three of the plots are squared-shaped. 同样重要的是,三个图应为正方形。 So far, I got: 到目前为止,我得到了: 在此处输入图片说明

A colorbar is either created in an already existing axes (aka subplot) element or it creates its own new one. 在已经存在的轴(又称子图)元素中创建一个颜色条,或者创建一个自己的新颜色条。 To use the axes cb you can simply do: 要使用cb轴,您只需执行以下操作:

im4 = plt.colorbar(im3, cax=cb)

to create the colorbar inside of cb. 在cb内创建颜色栏。

But perhaps you want matplotlib to do this for you. 但是也许您希望matplotlib为您做到这一点。 Then you can specify from which axes (=subplots) colorbar should steal space: 然后,您可以指定颜色条从哪个轴(= subplots)窃取空间:

fig=plt.figure()
ax1=plt.subplot(1,3,1)
im=ax1.contourf(  M1, 50,vmax=100,vmin=-100)
x0,x1 = ax1.get_xlim()
y0,y1 = ax1.get_ylim()
ax1.set_aspect((x1-x0)/(y1-y0))
ax2=plt.subplot(1,3,2,aspect=1)
im2=ax2.contourf(  M2, 50,vmax=100,vmin=-100)
ax3=plt.subplot(1,3,3,aspect=1)
im3=ax3.contourf( M3, 50,vmax=100,vmin=-100)
im4 = plt.colorbar(im3, ax=[ax1, ax2, ax3])

All this information is taken from help(plt.colorbar) 所有这些信息均来自help(plt.colorbar)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM