[英]In python, how to correctly use `colorbar` and `pcolormesh`?
Here is my code, 这是我的代码,
from mpl_toolkits.axes_grid1 import make_axes_locatable # colorbar
from matplotlib import pyplot as plt
from matplotlib import cm # 3D surface color
import numpy as np
data1 = np.random.rand(10, 12)
data2 = np.random.rand(10, 12)
data3 = data1 - data2
vmin = min([data1.min(), data2.min(), data3.min()])
vmax = max([data1.max(), data2.max(), data2.max()])
fig, (ax_1, ax_2, ax_error) = plt.subplots(nrows=3, ncols=1, figsize=(6, 6))
ax_1.set_ylabel('x')
mesh_1 = ax_1.pcolormesh(data1.T, cmap = cm.coolwarm)
ax_2.set_ylabel('x')
mesh_2 = ax_2.pcolormesh(data2.T, cmap = cm.coolwarm)
mesh_error = ax_error.pcolormesh(data3.T, cmap = cm.coolwarm)
ax_error.set_ylabel('x')
ax_error.set_xlabel('t')
divider = make_axes_locatable(ax_2)
cax_val = divider.append_axes("right", size="2%", pad=.1)
fig.colorbar(mesh_2, ax=[ax_1, ax_2, ax_error], cax=cax_val)
fig.tight_layout()
plt.show()
and it produces an image 并产生图像
However, what I expect is that it produces the picture below 但是,我期望它会产生下面的图片
Can anyone help me with this problem? 谁能帮助我解决这个问题? Thanks in advance for any helpful suggestion! 在此先感谢您提供任何有用的建议!
tight_layout
doesn't help with this problem, unfortunately. tight_layout
是, tight_layout
这个问题。 No tight_layout
and no axes_grid
works fine: 没有tight_layout
和axes_grid
可以正常工作:
from matplotlib import pyplot as plt
from matplotlib import cm # 3D surface color
import numpy as np
data1 = np.random.rand(10, 12)
data2 = np.random.rand(10, 12)
data3 = data1 - data2
fig, (ax_1, ax_2, ax_error) = plt.subplots(nrows=3, ncols=1, figsize=(6, 6))
mesh_1 = ax_1.pcolormesh(data1.T, cmap = cm.coolwarm)
mesh_2 = ax_2.pcolormesh(data2.T, cmap = cm.coolwarm)
mesh_error = ax_error.pcolormesh(data3.T, cmap = cm.coolwarm)
fig.colorbar(mesh_2, ax=[ax_1, ax_2, ax_error])
plt.show()
If you want better spacing you can try constrained_layout
: 如果您想获得更好的间距,可以尝试使用constrained_layout
:
fig, (ax_1, ax_2, ax_error) = plt.subplots(nrows=3, ncols=1, figsize=(6, 6),
constrained_layout=True)
Constrained layout will also work for just one axes: 约束布局也仅适用于一个轴:
fig.colorbar(mesh_2, ax=ax_2)
With the help from @JodyKlymak, I finally solved the problem. 在@JodyKlymak的帮助下,我终于解决了这个问题。 The keypoint lies in using shrink
, ie fig.colorbar(mesh_2, ax=[ax_1, ax_2, ax_error], shrink=0.3)
. 关键在于使用shrink
,即fig.colorbar(mesh_2, ax=[ax_1, ax_2, ax_error], shrink=0.3)
。 Here is the solution 这是解决方案
from matplotlib import pyplot as plt
from matplotlib import cm # 3D surface color
import numpy as np
data1 = np.random.rand(10, 12)
data2 = np.random.rand(10, 12)
data3 = data1 - data2
fig, (ax_1, ax_2, ax_error) = plt.subplots(nrows=3, ncols=1, figsize=(6, 6))
mesh_1 = ax_1.pcolormesh(data1.T, cmap = cm.coolwarm)
mesh_2 = ax_2.pcolormesh(data2.T, cmap = cm.coolwarm)
mesh_error = ax_error.pcolormesh(data3.T, cmap = cm.coolwarm)
fig.colorbar(mesh_2, ax=[ax_1, ax_2, ax_error], shrink=0.3)
plt.show()
and it produces 它产生
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.