2017-09-23 1 views
1

Ich habe einen 2x3 Unterplot von Konturkarten. Ich möchte zwei horizontale Farbbalken haben, einen für die ersten beiden Spalten und einen für die dritte Spalte. Ich kann es nur für jede Spalte tun (also 3 Farbbalken), aber ich brauche eine gemeinsame Farbleiste für die ersten beiden Spalten.Subplots mit verschiedenen Farbbalken

Hier ist mein Beispielcode:

import numpy as np 
import matplotlib.pyplot as plt 

v = np.arange(-180,181,15) 
y,x = np.meshgrid(v,v) 

p1 = 100*np.sin(x+y) 
p2 = 100*np.sin(2*x+y) 
p3 = 100*np.sin(3*x+y) 
p4 = 100*np.sin(4*x+y) 
p5 = 5*np.cos(x+5*y) 
p6 = 5*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') 
l1 = 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') 
l2 = 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[:,0]),orientation='horizontal') 
f.colorbar(l1, ax=list(ax[:,1]),orientation='horizontal') 
f.colorbar(l2, ax=list(ax[:,2]),orientation='horizontal') 

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() 

Antwort

2

Das ax Argument von colorbar ermöglicht die Achsen festlegen, entlang derer die colorbar erstellt wird. Wenn Sie für die erste Farbleiste die Achsen der ersten beiden Spalten angeben, erstreckt sich diese entlang beiden.

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

enter image description here