Python 更改标签名称

Python 更改标签名称,python,pandas,matplotlib,Python,Pandas,Matplotlib,我已将绘图中的标签设置为希望在图表中看到的标签,但它不起作用: 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","p

我已将绘图中的标签设置为希望在图表中看到的标签,但它不起作用:

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

因此,我发现plt.legend()基本上覆盖了pandas中的初始标签。我用以下代码将它传递到末尾(就在plt.show()之前),它成功了:

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

从数据帧打印时,熊猫似乎会覆盖label命令。请参见下面的示例——顶部的图形直接从pandas中通过
DataFrame.plot(x=…)
绘制,而底部的图形直接通过matplotlib通过
plt.plot()
绘制

直接打印系列,例如
df[“series1”]。plot()
也不会覆盖标签

显然,有没有可能还没有修好?我可以用0.20.1复制OP的问题

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

然而,有可能在事实发生后将其重命名为credit to。例如:

ax = df[["x","series1"]].plot(x = "x", color = "red", label = "Label 1")
ax.legend(["Label 1"])


我仍然不清楚调用
df.plot()
期间无法直接设置系列标签是否是故意的

谢谢你,帕特里克。那么,我是否应该等待将来的修复呢?@IvoFilipe嗯,我现在已经更新了链接到解决方案的答案,但我不知道是否有一个改变核心行为的修复方案。希望这至少有帮助。@IvoFilipe你能接受你的任何问题的答案吗,如果他们解决了问题,这样他们就不会一直没有解决?!否则,您当然可以要求澄清。@importantanceofbeingerest我最后用“ax.legend([“AAA”,“BBB”])尝试了上述解决方案,但仍然无效…@ivofilipe您能编辑您的问题以合并新代码吗?