我可以正确绘制带有价格数据的趋势线,但日期格式的两个 XY 轴都是空白的。我不确定是什么弄乱了这个轴的绘图配置。这是 Python 2.7 代码:
y = df['Close']
# calc the trendline http://stackoverflow.com/questions/26447191/how-to-add-trendline-in-python-matplotlib-dot-scatter-graphs
l = []
for t in df['Time']:
datetime_object = datetime.datetime.strptime(str(t), '%H:%M')
print datetime_object.hour
print datetime_object.minute
l.append((3600 * datetime_object.hour + 60 * datetime_object.minute))
x = l
z = np.polyfit(x, y, 1)
p = np.poly1d(z)
fig = plt.figure()
ax = fig.add_subplot(111)
#http://stackoverflow.com/questions/17709823/plotting-timestamps-hour-minute-seconds-with-matplotlib
plt.xticks(rotation=25)
ax = plt.gca()
ax.set_xticks(x)
xfmt = md.DateFormatter('%H:%M')
ax.xaxis.set_major_formatter(xfmt)
ax.plot(x, p(x), 'r--')
ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%3.4f')) #http://stackoverflow.com/questions/29188757/matplotlib-specify-format-of-floats-for-tick-lables
plt.show()
此外,df['Close']
将具有以下价值样本:
114.684
114.679
df['Time']
将包含样本值:
23:20
23:21