2017-07-29 5 views
0

wollte ich arithmetische Operationen auf Daten durchzuführen, so umgewandelt ich diese TermineWie kann ich es zurück in das Datetime-Format konvertieren?

idx_1 = 2017-06-07 00:00:00 
idx_2 = 2017-07-27 00:00:00 

mit Schwimmern,

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 

den folgenden Code verwenden plotten:

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() 

, die Plots : enter image description here

Wie matplotlib erstellt werden, um die tatsächlichen Daten auf x-Achse anstelle von Schwimmern zu zeichnen?

Jede Art von Hilfe wird sehr geschätzt.

Antwort

0

Beginnend mit datetime-Objekten können Sie die Funktionen date2num und num2date von matplotlib verwenden, um in und aus numerischen Werten zu konvertieren. Der Vorteil ist, dass die numerischen Daten dann von matplotlib.dates Lokatoren und Formatierern verstanden werden.

import datetime 
import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.dates 

idx_1 = datetime.datetime(2017,06,07,0,0,0) 
idx_2 = datetime.datetime(2017,07,27,0,0,0) 

idx = [idx_1, idx_2] 

y1 = 155.98 
y2 = 147.07 

x = matplotlib.dates.date2num(idx) 
y = [y1, y2] 
Difference = x[1] - x[0] #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(x[0], x[1] + 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') 

loc= matplotlib.dates.AutoDateLocator() 
plt.gca().xaxis.set_major_locator(loc) 
plt.gca().xaxis.set_major_formatter(matplotlib.dates.AutoDateFormatter(loc)) 
plt.gcf().autofmt_xdate() 
plt.show() 

enter image description here

+0

danken Ihnen für die Zeit nehmen zu beantworten, bin froh :) –

Verwandte Themen