2017-11-23 23 views
0

the chartmatplotlib double y軸

runoffとvalueにはそれぞれ13個のデータがあります。 double y軸は、2つのセットデータを毎年比較するために使用されます。しかし、赤い記号が整列していないことと、毎年青い記号の間の数字が間違っています。私は逃した任意の詳細は?

runoff=[1877, 2690.9, 3632, 4525, 1512, 4459, 1614.3, 2595, 1270.2, 2308.2, 2436, 1742.8,2658.7] 
TSM=['22.805241997828', '26.649050918463', '23.875097901606', '22.60339679601', '25.898807017902', '28.610553136975', '28.489485098388', '22.206851921378', '21.958918945009', '15.629827074318', '18.894425896003', '20.511980938426', '19.879687929129'] 

def draw(df): 

    font = {'family' : 'Times New Roman',    
     'color' : 'black'   , 
     'weight' : 'normal'   , 
     'size' : 11 
     } 

    fig=plt.figure(11,figsize=(4.5,3)) 
    ax1=plt.axes([0.10,0.24,0.78,0.73]) 
    ax2=ax1.twinx() 
    matplotlib.rcParams["font.family"] = "Times New Roman"#全局times new roamn 
    matplotlib.rcParams['xtick.direction'] = 'in' 
    matplotlib.rcParams['ytick.direction'] = 'inout' 
    ax1.plot(value,label='TSM',marker='o',color='red') 
    #plt.ylim([10,30]) 


    ax2.plot(runoff,label='Sediment runoff',marker='s',color='blue',linestyle='--') 
    ax2.spines['left'].set_color('red') 
    ax2.spines['right'].set_color('blue') 
    ax2.tick_params(which='both',color='blue') 
    ax1.tick_params(axis='y',which='both',color='red') 

    xticks=list(range(0,len(time),2)) 
    xlabels=[time[index] for index in xticks] 
    xticks.append(len(time)+1) 
    #xlabels.append('2015') 
    #xlabels.append('2016') 
    ax1.set_xticks(xticks) 
    ax1.set_xticklabels(xlabels,rotation=0,fontdict=font) 


    ax2.spines['bottom'].set_linewidth(1.25) 
    ax2.spines['left'].set_linewidth(1.25) 
    ax2.spines['right'].set_linewidth(1.25) 
    ax2.spines['top'].set_linewidth(1.25) 

    legend1=ax1.legend(loc=1,ncol=2) 
    legend1.get_title().set_fontsize(fontsize=20) 
    legend2=ax2.legend(loc=0,bbox_to_anchor=(0.6,0.85),borderaxespad=0.) 
    legend2.get_title().set_fontsize(fontsize=20) 

    f=plt.gcf() 
    f.text(0.001,0.7,u'TSM $(mg/L)$',fontdict=font,color='black',rotation=90) 
    f.text(0.5,0.09,u'Time (year)',fontdict=font,color='black') 
    f.text(0.99,0.82,u'Sediment runoff ($ton$)',fontdict=font,color='black',rotation=90) 
    f.canvas.draw() 
    plt.show() 
    return True 
+1

されるようにあなたが '' TSM''と '' runoff''の両方を与えてみました:あなたのx軸のセットアップコードは、ちょうどこのax2 = ax1.twinx()

移動を呼び出す前に移動します共通のx配列? '' plot(t、TSM) ''と '' plot(t、runoff) ''と同様です。 – alexblae

答えて

0

一つまっすぐ進むべき道は最初の年のリストを作成するためにあなたのデータフレームから正しく年間で(2003 ... 2015)あなたの時間に取り組むことを確認することですあり、言及あなたのコードで「時間」として。私はPython 3を使って簡単なリスト(範囲(..))を使いました。年のリストとしての 'time'変数は、次の行を必要としません( "#xticks.append(len(time)+1)") draw()の内部で使用されていないので、(df)引数は不要です。 2軸の凡例を組み合わせる方法をコーディングしました。 )AxesAll = axes1 + axes2を作成し、axes1 = ...ではなく、以前のax1.plot(...)ステートメントで作成し、axes2と同様のステートメントを作成し、結合凡例を定義します。

import matplotlib.pyplot as plt 
import matplotlib 

time = list(range(2003,2016)) 
#print(time) 

runoff=[1877, 2690.9, 3632, 4525, 1512, 4459, 1614.3, 2595, 1270.2, 2308.2, 
2436, 1742.8,2658.7] 
TSM=['22.805241997828', '26.649050918463', '23.875097901606', '22.60339679601', '25.898807017902', '28.610553136975', '28.489485098388', '22.206851921378', '21.958918945009', '15.629827074318', '18.894425896003', '20.511980938426', '19.879687929129'] 

def draw(): 

    font = {'family' : 'Times New Roman',    
    'color' : 'black'   , 
    'weight' : 'normal'   , 
    'size' : 11 
    } 

    fig=plt.figure(11,figsize=(4.5,3)) 
    ax1=plt.axes([0.10,0.24,0.78,0.73]) 
    ax2=ax1.twinx() 
    matplotlib.rcParams["font.family"] = "Times New Roman"#全局times new roamn 
    matplotlib.rcParams['xtick.direction'] = 'in' 
    matplotlib.rcParams['ytick.direction'] = 'inout' 
    axes1 = ax1.plot(TSM,label='TSM',marker='o',color='red') 
    #plt.ylim([10,30]) 


    axes2 = ax2.plot(runoff,label='Sediment runoff',marker='s',color='blue',linestyle='--') 
    ax2.spines['left'].set_color('red') 
    ax2.spines['right'].set_color('blue') 
    ax2.tick_params(which='both',color='blue') 
    ax1.tick_params(axis='y',which='both',color='red') 

    xticks=list(range(0,len(time),2)) 
    xlabels=[time[index] for index in xticks] 
    #xticks.append(len(time)+1) 
    #xlabels.append('2015') 
    #xlabels.append('2016') 
    ax1.set_xticks(xticks) 
    ax1.set_xticklabels(xlabels,rotation=0,fontdict=font) 


    ax2.spines['bottom'].set_linewidth(1.25) 
    ax2.spines['left'].set_linewidth(1.25) 
    ax2.spines['right'].set_linewidth(1.25) 
    ax2.spines['top'].set_linewidth(1.25) 

    #legend1=ax1.legend(loc=1,ncol=2) 
    #legend1.get_title().set_fontsize(fontsize=20) 
    #legend2=ax2.legend(loc=0,bbox_to_anchor=(0.6,0.85),borderaxespad=0.) 
    #legend2.get_title().set_fontsize(fontsize=20) 
    axesAll = axes1+axes2 
    labsAll = (l.get_label() for l in axesAll) 
    plt.legend(axesAll, labsAll, loc=0) 

    f=plt.gcf() 
    f.text(0.001,0.7,u'TSM 
    $(mg/L)$',fontdict=font,color='black',rotation=90) 
    f.text(0.5,0.09,u'Time (year)',fontdict=font,color='black') 
    f.text(0.99,0.82,u'Sediment runoff ($ton$)',fontdict=font,color='black',rotation=90) 
    f.canvas.draw() 
    plt.show() 
    return True 

draw() 
0

あなたは、単純な問題を抱えている - あなたが実際にax1にx軸を設定する前に、AX2を作成するためにax1twinxを呼んでいます。

xticks=list(range(0,len(time),2)) 
xlabels=[time[index] for index in xticks] 
xticks.append(len(time)+1) 
#xlabels.append('2015') 
#xlabels.append('2016') 
ax1.set_xticks(xticks) 
ax1.set_xticklabels(xlabels,rotation=0,fontdict=font) 

ここ

ax1=plt.axes([0.10,0.24,0.78,0.73]) 
#INSERT HERE 
... 
#END OF INSERT 
ax2=ax1.twinx() 
関連する問題