2016-07-01 7 views
0

それぞれにカラーバーが必要な一連のサブプロットがあります。 xの限界を設定せずに各サブプロットをプロットすると、x軸はデータのドメインを超えて伸び、多くの空白が示されます。私はこのコードを使用しています:私はmake_subplot機能にplt.xlim()を追加する場合カラーバーを縮小せずにサブプロットのx制限を調整する方法

import matplotlib.pyplot as plt 
from matplotlib.mlab import griddata 
from numpy import ma 
from mpl_toolkits.axes_grid1 import make_axes_locatable 

def plot_threshold(ax): 
    """ plot boundary condition (solid) and 
     extrapolated condition(dotted)""" 
    x = np.arange(0,10,.5) 
    slide= Threshold(x) 
    ax.plot(slide[0], slide[1], 'r-', 
      linewidth=2) 
    ex_slide = Extrapolated_threshold(x) 
    ax.plot(ex_slide[0], ex_slide[1], 'r:') 

def make_subplot(ax, x, y, zdata, title): 
    ax.set_title(title, size =14) 
    CS = ax.tricontourf(x, y, zdata, 100, cmap=clrmap) 
    plot_threshold(ax) 

    #TROUBLESOM LINE BELOW 
    plt.xlim(0,xmax) 

    # create divider for existing axes instance 
    divider = make_axes_locatable(ax) 
    # append axes to rhe right of ax, with 5% width of ax 
    cax1 = divider.append_axes('right', size='4%', pad = 0.1) 
    # create color bar in the appneded axes 
    cbar = plt.colorbar(CS, cax=cax1) 

clrmap = plt.cm.viridis 

# Three subplots, stacked vertically 
fig, axarr = plt.subplots(3, figsize =(8,10), sharex='col') 
make_subplot(axarr[0], x, y, z1, "Plot 1") 
make_subplot(axarr[1], x, y, z2, 'Plot 2')  
make_subplot(axarr[2], x, y, z3, 'Plot 3') 

は、上の2つのサブプロットのカラーバーは非常に狭くして読めなくなります。 3番目のサブプロットのカラーバーは影響を受けません。

make_subplotからplt.xlim()取り外しとこのように、関数呼び出しの下にそれを追加:

make_subplot(axarr[0], x, y, z1, "Plot 1") 
plt.xlim(0,14) 
make_subplot(axarr[1], x, y, z2, 'Plot 2') 
plt.xlim(0,14)  
make_subplot(axarr[2], x, y, z3, 'Plot 3') 
plt.xlim(0,14) 

がxの限界を調整し、カラーバーをsquishesしません。

1)カラーバーがすべて同じ方法で、make_subplotsの行で影響を受けるのはなぜですか?

2)幸せなカラーバーを維持しながらxの制限を調整するにはどうすればよいですか?

enter image description here

答えて

0

代わりの

make_subplot(axarr[0], x, y, z1, "Plot 1") 
plt.xlim(0,14) 
make_subplot(axarr[1], x, y, z2, 'Plot 2') 
plt.xlim(0,14)  
make_subplot(axarr[2], x, y, z3, 'Plot 3') 
plt.xlim(0,14) 

make_subplot(axarr[0], x, y, z1, "Plot 1") 
axarr[0].set_xlim(0,14) 
make_subplot(axarr[1], x, y, z2, 'Plot 2') 
axarr[1].set_xlim(0,14)  
make_subplot(axarr[2], x, y, z3, 'Plot 3') 
axarr[2].set_xlim(0,14) 

を試してみてください私はそれが呼び出された時点での現在の軸であるため、plt.xlimは、カラーバーの軸に作用していると仮定します。データが表示されている軸(axarr[i])にplt.xlimをコールすると、それが修正されます。

これがうまくいかない場合は、コードが実行可能な状態ではないため、このガイドラインhttps://stackoverflow.com/help/mcveに沿って質問を更新してください。

+0

あなたは 'set_xlim'の使用について正しいですが、' make_subplot'の中で 'ax.set_xlim(0,14)'として使用する必要がありました。 – Caroline

関連する問題