2 回答
TA贡献1777条经验 获得超3个赞
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Take an example data
data = {"Date":["date{i}".format(i=i) for i in range(10)], "Topic":["topic{i}".format(i=i) for i in range(10)], "Return":[1,2,3,2,1,2,4,7,1,3]}
df = pd.DataFrame.from_dict(data)
dates = np.array(df["Date"].tolist())
returns = np.array(df["Return"].tolist())
# Calculate the minimas and the maximas
minimas = (np.diff(np.sign(np.diff(returns))) > 0).nonzero()[0] + 1
maximas = (np.diff(np.sign(np.diff(returns))) < 0).nonzero()[0] + 1
# Plot the entire data first
plt.plot(dates, returns)
# Then mark the maximas and the minimas
for minima in minimas:
plt.plot(df.iloc[minima]["Date"], df.iloc[minima]["Return"], marker="o", label=str(df.iloc[minima]["Topic"]))
for maxima in maximas:
plt.plot(df.iloc[maxima]["Date"], df.iloc[maxima]["Return"], marker="o", label=str(df.iloc[maxima]["Topic"]))
plt.legend()
plt.show()
示例数据框:
Date Topic Return
0 date0 topic0 1
1 date1 topic1 2
2 date2 topic2 3
3 date3 topic3 2
4 date4 topic4 1
5 date5 topic5 2
6 date6 topic6 4
7 date7 topic7 7
8 date8 topic8 1
9 date9 topic9 3
绘制它产生:
添加回答
举报