在Python中覆盖平均线

时间:2017-12-29 19:59:47

标签: 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')
events_all_metrics[["event_name","pinny_payback"]].plot(x="event_name",style='.', color='red', 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()
ax.plot(pinny_mean, label='Pinny Mean', linestyle='--', color='red')
plt.show()

kambi_pinny

1 个答案:

答案 0 :(得分:1)

这不起作用,因为您的pinny_mean是y中的单个值。 plot需要y和x中的点。在这种情况下,我建议您使用plt.axhline而不是plot。它绘制了一条常数y线,覆盖了x的整个范围。以你的例子:

plt.axhline(y=pinny_mean, label='Pinny Mean', linestyle='--', color='red')