【发布时间】:2018-01-05 12:56:23
【问题描述】:
我想对日期执行算术运算,所以我转换了这些日期
idx_1 = 2017-06-07 00:00:00
idx_2 = 2017-07-27 00:00:00
使用浮动,
x1 = time.mktime(idx_1.timetuple()) # returns float of dates
>>> 1496773800.0
x2 = time.mktime(idx_2.timetuple())
>>> 1501093800.0
y1 = 155.98
y2 = 147.07
我使用下面的代码来绘制:
import datetime as dt
import time
import numpy as np
import matplotlib.pyplot as plt
x = [x1, x2]
y = [y1, y2]
Difference = x2 - x1 #this helps to end the plotted line at specific point
coefficients = np.polyfit(x, y, 1)
polynomial = np.poly1d(coefficients)
# the np.linspace lets you set number of data points, line length.
x_axis = np.linspace(x1, x2 + Difference, 3) # linspace(start, end, num)
y_axis = polynomial(x_axis)
plt.plot(x_axis, y_axis)
plt.plot(x[0], y[0], 'go')
plt.plot(x[1], y[1], 'go')
plt.show()
如何让 matplotlib 在 x 轴上绘制实际日期而不是浮点数?
非常感谢任何形式的帮助。
【问题讨论】:
标签: python datetime numpy matplotlib