共享相同颜色条的2x2 Contourf图 [英] 2x2 Contourf plots sharing the same colorbar
本文介绍了共享相同颜色条的2x2 Contourf图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在下面的2x2图的右侧添加一个颜色条.
将matplotlib.pyplot导入为plt将numpy导入为npx = 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)返回(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(行数= 2,ncols = 2,figsize =(8,8),sharex ='col',sharey ='row')(ax1,ax2),(ax3,ax4)= axs等级= np.linspace(-2,2,10)cs1 = ax1.contourf(X,Y,f01,level = levels,cmap = cmap)ax1.set_ylabel('angle y',fontsize = 16)ax1.tick_params(labelsize = 14)ax1.set_title('干扰等级1',fontsize = 14)ax1.set_aspect('等于')cs2 = ax2.contourf(X,Y,f10,level = levels,cmap = cmap)ax2.set_title('干扰等级2',fontsize = 14)ax2.set_aspect('等于')cs3 = ax3.contourf(X,Y,f11,level = 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('干扰等级3',fontsize = 14)ax3.set_aspect('等于')cs4 = ax4.contourf(X,Y,f12,level = levels,cmap = cmap)ax4.set_xlabel('angle x',fontsize = 16)ax4.tick_params(labelsize = 14)ax4.set_title('干扰等级4',fontsize = 14)ax4.set_aspect('等于')plt.tight_layout()plt.show()
我一直在审查上一个问题的解决方案
I would like to add a single color bar on the right side of the following 2x2 plot.
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()
I have been reviewing the solutions of previous question Matplotlib 2 Subplots, 1 Colorbar
.
However, I have four contourf()
subplots, whereas the posted solutions use 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>
I do not know how to apply the previous solutions with the ax
and cs
object of my plot.
解决方案
IIUC, you can just pass axs
into colorbar
:
plt.tight_layout()
plt.colorbar(cs4, ax=axs)
plt.show()
Output:
这篇关于共享相同颜色条的2x2 Contourf图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文