python
  • pandas
  • matplotlib
  • 2017-12-29 15 views 0 likes 
    0

    は私が私のチャートに表示するラベルに私のプロットにラベルを設定したが、それは働いていない:変更ラベル名のPython

    sns.set(rc={"figure.figsize": (16, 8)}) 
    ax = events_all_metrics[["event_name","kambi_payback"]].plot(x="event_name", style='.',use_index=False, color ='green', label='Kambi Payback') 
    events_all_metrics[["event_name","pinny_payback"]].plot(x="event_name",style='.', color='red', label='Pinnacle Payback', ax=ax) 
    plt.tick_params(
        axis='x',   # changes apply to the x-axis 
        which='both',  # both major and minor ticks are affected 
        bottom='off',  # ticks along the bottom edge are off 
        top='off',   # ticks along the top edge are off 
        labelbottom='off') 
    plt.legend(loc=4, prop={'size': 15}) 
    
    pinny_mean = events_all_metrics["pinny_payback"].mean() 
    plt.axhline(y=pinny_mean, label='Pinny Mean', linestyle='--', color='red') 
    
    kambi_mean = events_all_metrics["kambi_payback"].mean() 
    plt.axhline(y=kambi_mean, label='Kambi Mean', linestyle='--', color='green') 
    plt.show() 
    

    kambi_pinny

    をので、私はそれを考え出しました基本的にplt.legend()はパンダの初期ラベルを上書きしていました。私は、次のコードで終了(ちょうどplt.showの前に())にそれを渡され、それが働いた:DATAFRAMEからプロットするとき

    plt.legend(["Kambi Payback","Pinnacle Payback", "Kambi Mean", "Pinnacle Mean"], loc=4, prop={'size': 15}) 
    
    +0

    おそらく、 'plt.legend(...)'の行が最後にあるはずだからでしょうか? Patrickに感謝します。 – Georgy

    答えて

    1

    は、パンダのように見えるlabelコマンドを上書きします。下の例を参照してください - 上の図はDataFrame.plot(x=...)のパンダから直接描かれ、下はplt.plot()で直接matplotlibを通ります。

    シリーズを直接プロットする。 df["series1"].plot()もラベルを上書きしません。

    明らかにthis was a behavior known as an issue in an old version of pandas - これは修正されていない可能性がありますか?私はOPの問題を0.20.1で再現することができます。

    import numpy as np 
    import pandas as pd 
    import matplotlib.pyplot as plt 
    
    data = list(zip(np.arange(100),np.random.random(100),2*np.random.random(100))) 
    
    fig,axes = plt.subplots(2,1) 
    
    df = pd.DataFrame(data, columns = ["x","series1","series2"]) 
    df[["x","series1"]].plot(x = "x", color = "red", label = "Label 1", ax = axes[0]) 
    
    #df[["x","series2"]].plot(x = "x", color = "green", label = "Label 2", ax = ax) 
    
    axes[1].plot(df["x"], df["series1"], color = "red", label = "Label 1") 
    
    plt.legend() 
    

    enter image description here

    しかし、this answerに、事実の後にクレジットをそれらの名前を変更することが可能です。たとえば、次のように

    ax = df[["x","series1"]].plot(x = "x", color = "red", label = "Label 1") 
    ax.legend(["Label 1"]) 
    
    df.plot()に通話中に直接系列ラベルを設定できないことが意図的であるかどうかは私にまだ不明

    enter image description here

    +0

    私はちょうど将来の修正を待つべきですか? –

    +0

    @IvoFilipeまあ、今のところ回避策へのリンクを更新しましたが、コア動作を変更する修正がカードにあるかどうかわかりません。少なくとも助けてくれることを願っています。 –

    +0

    @IvoFilipeあなたが未解決のままでいないなど、あなたが問題を解決した場合、あなたの質問に対する回答を受け入れることができますか?そうでなければ、もちろん説明を求めるかもしれません。 – ImportanceOfBeingErnest

    関連する問題