2016-12-28 13 views
3

私はmatplotlibを使用しています。両方のデータセットのx軸とy軸を同じにして、2つのデータセットのグラフを比較したいと思います。しかし、データセット2の制限がより小さいので、オートスケールはグラフを少しずつ保持し、リサイズします。写真のように。Python Matplotlib - オートスケールをオフにする

def make_figure(data, param ='Customers'): # default param is Customers 
    fig = plt.figure(figsize = (18,10)) 

    xticks = np.arange(0, 10000, 1000) 
    yticks = np.arange(0, 55000, 5000) 

    i = 0 
    colors = ['red','yellow','brown','orange','green','green','green','green','blue','cyan','navy','magenta'] 

    ax1 = fig.add_subplot(3,4,1) 
    ax1.set_xticks(xticks) 
    ax1.set_yticks(yticks) 
    ax1.autoscale(False, tight=False) 

    for assortment in ['a','b','c']: 
     for storetype in ['a','b','c','d']: 

      datax = data[param][data.StoreType == storetype][data.Assortment == assortment] 
      datay = data['Sales'][data.StoreType == storetype][data.Assortment == assortment] 
      plt.subplot(3, 4, i+1, sharex=ax1, sharey=ax1) 
      plt.title('Assortment ' + assortment + ' StoreType ' + storetype) 
      plt.scatter(y = datay, x = datax, c=colors[i], alpha=.65) 

      if i % 4 == 0: 
       plt.ylabel('Sales') 

      if i >= 8: 
       plt.xlabel(str(param)) 

      i += 1 

    plt.tight_layout() 

    return plt.show() 

データセット1

enter image description here

データセット2

enter image description here

+0

@tacaswell:2つの質問は同じように聞こえるが、set_xlim()を試してみたところ、私が望む結果が得られなかった。 –

+0

質問が再開されました。 – tacaswell

+0

実際に私はそれを修正しました。ループ内にax = plt.subplot()とax.set_xlim()とax.set_ylim()**を入れる必要があります。 –

答えて

0

あなたはユーザーXLIMとYLimプロパティ関数は、各軸の制限を設定することができます。 限度は例えば、一定である場合:

# Set the limits and disable scaling 
plt.xlim(0, 8000) 
plt.ylim(0, 40000) 
plt.autoscale(False) 

より一般的な解決策は、あなたが事前にプロットし、同様に制限を設定することを計画し、すべてのデータセットに基づいて、あなたの最高限度を決定することです。

+0

私は実際にそれを試しました。 ax1.set_xlim([0,8000]) ax1.set_ylim([0,45000]) –

+0

を追加しましたが、結果は同じです –

+0

実際には今修正しました。ループ内にax = plt.subplot()とax.set_xlim()とax.set_ylim()を入れる必要があります。 –

関連する問題