我已将我的图表中的标签设置为我想在图表中看到的标签,但它不起作用:
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})
答案 0 :(得分:1)
在从DataFrame绘图时,看起来pandas会覆盖label命令。请参阅下面的示例 - 顶部图是直接从带有DataFrame.plot(x=...)
的pandas绘制的,而底部是直接使用plt.plot()
通过matplotlib绘制的。
直接绘制一个系列,例如df["series1"].plot()
也不会覆盖标签。
显然是this was a behavior known as an issue in an old version of pandas - 所以有可能它还没有修好吗?我可以用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()
但是,可以在事实之后重命名它们,归功于this answer。例如:
ax = df[["x","series1"]].plot(x = "x", color = "red", label = "Label 1")
ax.legend(["Label 1"])
我还不清楚在调用df.plot()
期间无法直接设置系列标签是有意还是无意。