matplotlib 未显示线形图[关闭]

hiz5n14c  于 2023-06-23  发布在  其他
关注(0)|答案(2)|浏览(105)

**关闭。**此题需要debugging details。目前不接受答复。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答这个问题。
8天前关闭
Improve this question
我试图绘制一个图表与多个情节一样线,散点等,线情节我试图情节是不显示在图表上。我想要一个这样的线图:

fig, ax = plt.subplots(figsize=(20, 6))
plt.plot(result, color='red')
plt.show()


我希望它显示在下面的图中。
下面是我的代码:

fig, ax = plt.subplots(figsize=(20, 10))
fig.suptitle("Performance Ratio Evolution\nFrom 2019-07-01 to 2022-03-24", fontsize=20)
plt.scatter('Date', 'PR', data=l2, label="<2", marker='D', color='#000080')
plt.scatter('Date', 'PR', data=g2l4, label="2~4", marker='D', s=15, color="#ADD8E6")
plt.scatter('Date', 'PR', data=g4l6, label="4~6", marker='D', s=15, color="#FFA500")
plt.scatter('Date', 'PR', data=g6, label=">6", marker='D', s=15, color="#964B00")
plt.plot(df['PR'].rolling(30).mean(),label='30-d moving avg of pr') #The one which is not showing up
plt.legend(["<2", "2~4", "4~6", ">6"])
y = [73.9 * (1 - 0.008) ** i for i in range(4)]
start_date = datetime.strptime("2019-07-01", "%Y-%m-%d")
end_date = datetime.strptime("2023-03-24", "%Y-%m-%d")
dates = []
while start_date <= end_date:
    dates.append(start_date)
    start_date += timedelta(days=365)
plt.step(dates, y, label="Performance Ratio", color="green")
plt.ylim(0, 100)
date_fmt = mdates.DateFormatter('%b/%y')
ax.xaxis.set_major_formatter(date_fmt)
ax.set_xlim([date(2019, 7, 1), date(2022, 3, 24)])
plt.ylabel("Performance Ratio (%)")
plt.legend(loc="center")
# plt.savefig("performance_ratio_evolution.png")

以下是情节:

我做错了什么?

ne5o7dgx

ne5o7dgx1#

问题出在散点图代码里。。如果你看第一幅图的x轴,你会发现它们都是数字。如果打印df['PR'].rolling(30).mean(),它将是一个数字列表。另一方面,散点图都是针对日期绘制的。将线图更改为plt.plot(df['Date'], df['PR'].rolling(30).mean(),label='30-d moving avg of pr')(基本上添加df.Date作为x轴应该可以。我用一些虚拟数据做了这个,它似乎起作用了。完整的代码和图下面…

df=pd.DataFrame({'PR':np.random.uniform(low=60, high=90, size=(100,)),
                'Date':pd.date_range('2019/07/01', periods=100, freq='SM')})
fig, ax = plt.subplots(figsize=(20, 6))
fig.suptitle("Performance Ratio Evolution\nFrom 2019-07-01 to 2022-03-24", fontsize=20)
plt.scatter('Date', 'PR', data=df[0:24], label="<2", marker='D', color='#000080')
plt.scatter('Date', 'PR', data=df[25:49], label="2~4", marker='D', s=15, color="#ADD8E6")
plt.scatter('Date', 'PR', data=df[50:74], label="4~6", marker='D', s=15, color="#FFA500")
plt.scatter('Date', 'PR', data=df[75:99], label=">6", marker='D', s=15, color="#964B00")
plt.plot(df['Date'], df['PR'].rolling(30).mean(),label='30-d moving avg of pr') #The one which is not showing up
plt.legend(["<2", "2~4", "4~6", ">6"])
y = [73.9 * (1 - 0.008) ** i for i in range(4)]
start_date = datetime.datetime.strptime("2019-07-01", "%Y-%m-%d")
end_date = datetime.datetime.strptime("2023-03-24", "%Y-%m-%d")
dates = []
while start_date <= end_date:
    dates.append(start_date)
    start_date += datetime.timedelta(days=365)
plt.step(dates, y, label="Performance Ratio", color="green")
plt.ylim(0, 100)
import matplotlib.dates as mdates
date_fmt = mdates.DateFormatter('%b/%y')
ax.xaxis.set_major_formatter(date_fmt)
ax.set_xlim([datetime.date(2019, 7, 1), datetime.date(2022, 3, 24)])
plt.ylabel("Performance Ratio (%)")
plt.legend(loc="center")

ccgok5k5

ccgok5k52#

你想画一条水平线吗?请尝试pyplot.axhline(y=somevalue),在您的情况下,使用

ax.axhline(y=df['PR'].rolling(30).mean(),label='30-d moving avg of pr',color='r', linestyle='--')

相关问题