Python 共享同一色条的2x2轮廓图
我想在下面的2x2绘图的右侧添加一个单色条Python 共享同一色条的2x2轮廓图,python,matplotlib,colorbar,contourf,Python,Matplotlib,Colorbar,Contourf,我想在下面的2x2绘图的右侧添加一个单色条 import matplotlib.pyplot as plt import numpy as np x = np.linspace(-np.pi*2, np.pi*2, num=50) y = np.linspace(-np.pi*2, np.pi*2, num=50) def fun(x, y, pw1, pw2): X,Y = np.meshgrid(x, y) return (X, Y, np.cos(X*Y)**pw1 +
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-np.pi*2, np.pi*2, num=50)
y = np.linspace(-np.pi*2, np.pi*2, num=50)
def fun(x, y, pw1, pw2):
X,Y = np.meshgrid(x, y)
return (X, Y, np.cos(X*Y)**pw1 + np.sin(X+Y)**pw2)
X, Y, f01 = fun(x,y,0,1)
X, Y, f10 = fun(x,y,1,0)
X, Y, f11 = fun(x,y,1,1)
X, Y, f12 = fun(x,y,1,2)
fig1, axs = plt.subplots(nrows=2,ncols=2, figsize=(8,8),sharex='col', sharey='row')
(ax1, ax2), (ax3, ax4) = axs
levels = np.linspace(-2, 2, 10)
cs1 = ax1.contourf(X, Y, f01, levels=levels, cmap=cmap)
ax1.set_ylabel('angle y', fontsize=16)
ax1.tick_params(labelsize=14)
ax1.set_title('Interference level 1', fontsize=14)
ax1.set_aspect('equal')
cs2 = ax2.contourf(X, Y, f10, levels=levels, cmap=cmap)
ax2.set_title('Interference level 2', fontsize=14)
ax2.set_aspect('equal')
cs3 = ax3.contourf(X, Y, f11, levels=levels, cmap=cmap)
ax3.set_ylabel('angle y', fontsize=16)
ax3.set_xlabel('angle x', fontsize=16)
ax3.tick_params(labelsize=14)
ax3.set_title('Interference level 3', fontsize=14)
ax3.set_aspect('equal')
cs4 = ax4.contourf(X, Y, f12, levels=levels, cmap=cmap)
ax4.set_xlabel('angle x', fontsize=16)
ax4.tick_params(labelsize=14)
ax4.set_title('Interference level 4', fontsize=14)
ax4.set_aspect('equal')
plt.tight_layout()
plt.show()
我一直在复习前一个问题的答案。
但是,我有四个子图,而发布的解决方案使用的是imshow()
plot
# im = imshow
print(im)
AxesImage(223.004,36;98.8364x98.8364)
# Axes
print(ax1)
AxesSubplot(0.0896322,0.541995;0.436434x0.408005)
# Contourf
print(cs1)
<matplotlib.contour.QuadContourSet object at 0x17d398940>
#im=imshow
打印(im)
AxeImage(223.004,36;98.8364x98.8364)
#斧头
打印(ax1)
AxesSubplot(0.0896322,0.541995;0.436434x0.408005)
#轮廓
打印(cs1)
我不知道如何将以前的解决方案应用于我绘图的
ax
和cs
对象。IIUC,您只需将axs
传递到colorbar
:
plt.tight_layout()
plt.colorbar(cs4, ax=axs)
plt.show()
输出:
谢谢。这就是我一直在寻找的解决方案。但是,现在行和列打印之间的距离不同(列之间的距离更小)。有没有办法将颜色条的长度与第2行的底部和第一行的顶部相匹配?颜色条相对于绘图的大小看起来很大,使用
figsize
它固定条大小和间隔距离。非常感谢。