调整子图中的颜色条

时间:2019-04-24 09:28:58

标签: python matplotlib

我的目标是将三个图绘制为子图,并对其中三个仅使用一个颜色条,我尝试通过制作一个具有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()

矩阵M1,M2和M3以前是在代码中计算出来的,但是对于我的问题,我猜并不是很重要。 同样重要的是,三个图应为正方形。到目前为止,我得到了: enter image description here

1 个答案:

答案 0 :(得分:2)

在现有的轴(又称子图)元素中创建颜色条,或者创建自己的新颜色条。要使用轴 public static BindableProperty NumberProperty = BindableProperty.Create(nameof(Number), typeof(int), typeof(MyView11), null, propertyChanged: OnNumberChanged); private static void OnNumberChanged(BindableObject bindable, object oldValue, object newValue) { var num = (MyView11)bindable; num.Number = (int)newValue; } public static BindableProperty NumberpProperty = BindableProperty.Create(nameof(Numberp), typeof(int), typeof(MyView11), null, propertyChanged: OnNumber1Changed); private static void OnNumber1Changed(BindableObject bindable, object oldValue, object newValue) { var num = (MyView11)bindable; num.Numberp = (int)newValue; } public int Number { get => (int)GetValue(NumberProperty); set => SetValue(NumberProperty, value); } public int Numberp { get => Number + 1; set => Number = value - 1; } ,您只需执行以下操作:

cb

在cb内部创建颜色栏。

但是也许您希望matplotlib为您做到这一点。然后,您可以指定颜色条从哪个轴(=子图)窃取空间:

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

所有这些信息均来自help(plt.colorbar)

相关问题