我试图用垂直线画一条平滑的曲线。 我使用scipy.interpolate平滑曲线,但是由于某些原因,垂直线不再出现在图形中。我得到以下错误:
"'<' not supported between instances of 'Timestamp' and 'numpy.float64'"
图表
代码
from datetime import datetime
dates = list(df['Date'])
print(dates)
dates_list = []
for date in dates:
dates_list.append(datetime.strptime(date, '%Y-%m-%d'))
from scipy.interpolate import splrep, splev
plt.figure(figsize=(20, 10))
# plt.plot(dates_list, saudi_cases['Cases'], label="cases");
x = np.linspace(0, 48, num=49)
A_tck = splrep(x, list(saudi_cases['Cases']))
dates = dates_list
x_new = np.linspace(x.min(), x.max(), 200)
A_new = splev(x_new, A_tck)
plt.plot(x_new, A_new, label='A')
plt.xticks(x, dates, size='small')
plt.suptitle('Saudi Arabia Cases')
plt.xlabel("Dates")
plt.ylabel("Cases")
plt.axvline(pd.Timestamp('2020-03-09'),color='r',label="event1")
plt.text(pd.Timestamp('2020-03-09'),99,'event1',rotation=90,fontsize=12)
plt.axvline(pd.Timestamp('2020-03-15'),color='b',linestyle='dashed')
plt.text(pd.Timestamp('2020-03-15'),99,'event2',rotation=90,fontsize=12)
plt.axvline(pd.Timestamp('2020-03-16'),color='b',linestyle='dashed')
plt.text(pd.Timestamp('2020-03-16'),99,'event3',rotation=90,fontsize=12)
plt.legend()
plt.show()
我有一个类似的问题,我通过使用matplotlib的dates API将日期转换为数字来解决它。我想我不得不使用
date2num
,但还有其他几种选择看起来您的代码示例并不完整。(
df
未定义。)如果提供完整的样本,人们更容易确认他们的答案有效相关问题 更多 >
编程相关推荐