更改标签名称Python

2024-09-29 23:25:51 发布

您现在位置: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.图例()重写了熊猫的初始标签。我把它传到了最后(就在之前表演())使用以下代码,并且它起作用:

^{pr2}$

Tags: nameeventplt标签allaxeventsmean
1条回答
网友
1楼 · 发布于 2024-09-29 23:25:51

从数据帧打印时,pandas似乎覆盖了label命令。请看下面的例子,上面的图是直接从pandas中用DataFrame.plot(x=...)绘制的,而底部的则是直接用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()

enter image description here

但是,可以在事实之后重命名它们,credit to this answer。例如:

^{pr2}$

enter image description here

我还不清楚在调用df.plot()期间无法直接设置序列标签是有意还是无意的。在

相关问题 更多 >

    热门问题