2017-09-24 49 views
0

我想绘制一组颜色条的一组等值线图。由于色条取决于您在参数ax中指定的子图,因此色条的范围不反映前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() 
+0

这不是通过设置'mina = minb = -500'和'maxa = maxb = 500'来实现的吗? – ImportanceOfBeingErnest

+0

规格说,前2列有一个彩条,最后一列有另一个。另外,'mina!= minb'和'maxa!= maxb'。不过,这并不能解决问题,因为色条范围的滴答符合指定的等值线图。 – jjv

+0

究竟*规格是什么?如果您具体了解每个子图的限制应该是多少,以及限制每个颜色条的限制,我们可以找到一个解决方案。 – ImportanceOfBeingErnest

回答

0

我认为在这种情况下,解决办法很简单:使用第一个情节一样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

在更一般情况下,其中一个图可能包含最小值,另一个图包含最大值,解决方案将是专门为颜色条创建一个ScalarMappable。

sm = plt.cm.ScalarMappable(cmap="viridis", norm=plt.Normalize(vmin=mina, vmax=maxa)) 
sm.set_array([]) 
f.colorbar(sm, ax=list(ax[1,0:2]),orientation='horizontal', pad=0.2) 
相关问题