2017-12-29 17 views
0

내가 내 차트에 표시 할 레이블에 내 플롯에 레이블을 설정했지만, 그것은 작동하지 않습니다 :변경 레이블 이름 파이썬

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 (...) '가있는 줄이 결국 있어야하기 때문에? – Georgy

답변

1

가 팬더처럼 보이는 레이블 명령을 무시합니다. 아래 예제를보십시오 - 위쪽 그림은 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로 재현 할 수있다.

enter image description here

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

는 그러나 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