一组绘图中带有自定义颜色条范围的子图

2024-05-05 16:56:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用颜色条为这个组绘制一组等高线图。由于colorbar依赖于您在ax参数中指定的子图,所以colorbar的范围并不反映前2列中所有绘图的值的范围。enter image description here

我可以指定第一个颜色条(在左边)来反映刻度上的最小值(mina=-500)和最大值(maxa=500)吗?在

这是我的代码:

import numpy as np
import matplotlib.pyplot as plt
v = np.arange(-180,181,15)
y,x = np.meshgrid(v,v)

p1 = 500*np.sin(x+y)
p2 = x
p3 = -500*np.sin(3*x+y)
p4 = y
p5 = -200*np.cos(x+5*y)
p6 = 100*np.cos(x+6*y)

f, ax = plt.subplots(2,3,sharex='row',sharey='col',figsize=(4,6))

mina = min(min(p1.reshape(625,)),min(p2.reshape(625,)),min(p3.reshape(625,)),min(p4.reshape(625,)))
maxa = max(max(p1.reshape(625,)),max(p2.reshape(625,)),max(p3.reshape(625,)),max(p4.reshape(625,)))
minb = min(min(p5.reshape(625,)),min(p6.reshape(625,)))
maxb = max(max(p5.reshape(625,)),max(p6.reshape(625,)))

ax[0,0].contourf(x,y,p1,20,vmin=mina,vmax=maxa)
ax[0,0].set(adjustable='box-forced', aspect='equal')
l0 = ax[1,0].contourf(x,y,p2,20,vmin=mina,vmax=maxa)
ax[1,0].set(adjustable='box-forced', aspect='equal')
ax[0,1].contourf(x,y,p3,20,vmin=mina,vmax=maxa)
ax[0,1].set(adjustable='box-forced', aspect='equal')
ax[1,1].contourf(x,y,p4,20,vmin=mina,vmax=maxa)
ax[1,1].set(adjustable='box-forced', aspect='equal')
ax[0,2].contourf(x,y,p5,20,vmin=minb,vmax=maxb)
ax[0,2].set(adjustable='box-forced', aspect='equal')
l1 = ax[1,2].contourf(x,y,p6,20,vmin=minb,vmax=maxb)
ax[1,2].set(adjustable='box-forced', aspect='equal')

f.colorbar(l0, ax=list(ax[1,0:2]),orientation='horizontal', pad=0.2)
f.colorbar(l1, ax=ax[1,2],orientation='horizontal', aspect=10, pad=0.2)

plt.setp([a.get_xticklabels() for a in ax[0,:]], visible=False)
plt.setp([a.get_yticklabels() for a in ax[:,1]], visible=False)
plt.setp([a.get_yticklabels() for a in ax[:,2]], visible=False)

plt.show()

Tags: boxnppltaxminmaxsetaspect
1条回答
网友
1楼 · 发布于 2024-05-05 16:56:26

我认为在这种情况下,解决方案很简单:将第一个plot用作colorbar的ScalarMappable

l0 = ax[0,0].contourf(x,y,p1,20,vmin=mina,vmax=maxa)
#...
l1 = ax[0,2].contourf(x,y,p5,20,vmin=minb,vmax=maxb)
#...

enter image description here

在更一般的情况下,一个绘图可能包含最小值,而另一个绘图包含最大值,解决方案是为colorbar创建一个ScalarMappable。在

^{pr2}$

相关问题 更多 >