2013-07-14 10 views
8

Hier ist mein Problem: polyfit nicht Datetime-Werte annehmen, so dass ich Datetime mit mktime die PolynomfitKurvenanpassung an eine Zeitreihe im Format 'datetime'?

z4 = polyfit(d, y, 3) 
p4 = poly1d(z4) 

Für das Grundstück jedoch arbeitet Herstellung umgewandelt, würde ich die Datetime-Beschreibung auf der Achse mag und didn‘ t # finde heraus, wie das geht. Kannst du mir helfen?

fig = plt.figure(1) 
cx= fig.add_subplot(111) 

xx = linspace(0, d[3], 100) 
pylab.plot(d, y, '+', xx, p4(xx),'-g') 
cx.plot(d, y,'+', color= 'b', label='blub') 
plt.errorbar(d, y, 
      yerr, 
      marker='.', 
      color='k', 
      ecolor='b', 
      markerfacecolor='b', 
      label="series 1", 
      capsize=0, 
      linestyle='') 

cx.grid() 
cx.set_ylim(0,0.03) 
plt.show() 

Rest des Codes:

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib import axis 
from datetime import datetime 
from numpy import * 
import pylab 
import time 

meine ersten 4 Zeitdatenpunkte

x = [datetime(1978, 7, 7), 
    datetime(1980, 9, 26), 
    datetime(1983, 8, 1), 
    datetime(1985,8,8)] 

d=[] 
for i in x: 
    d.append(time.mktime(i.timetuple())) 

meine ersten 4 Datenwerte

y = [0.00134328779552718, 
    0.00155187668863844, 
    0.0039431374327427, 
    0.00780037563783297] 

meine berechneten Standardabweichungen für Fehler Bars

yerr = [0.0000137547160254577, 
     0.0000225670232594083, 
     0.000105623642510075, 
     0.00011343121508] 

Antwort

10

Verwenden Sie anstelle der Datumsangaben die zugehörigen Datumsangaben.


import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.dates as mdates 
import datetime as DT 
import time 

dates = [DT.datetime(1978, 7, 7), 
    DT.datetime(1980, 9, 26), 
    DT.datetime(1983, 8, 1), 
    DT.datetime(1985, 8, 8)] 

y = [0.00134328779552718, 
    0.00155187668863844, 
    0.0039431374327427, 
    0.00780037563783297] 


yerr = [0.0000137547160254577, 
     0.0000225670232594083, 
     0.000105623642510075, 
     0.00011343121508] 

x = mdates.date2num(dates) 

z4 = np.polyfit(x, y, 3) 
p4 = np.poly1d(z4) 

fig, cx = plt.subplots() 

xx = np.linspace(x.min(), x.max(), 100) 
dd = mdates.num2date(xx) 

cx.plot(dd, p4(xx), '-g') 
cx.plot(dates, y, '+', color='b', label='blub') 
cx.errorbar(dates, y, 
      yerr, 
      marker='.', 
      color='k', 
      ecolor='b', 
      markerfacecolor='b', 
      label="series 1", 
      capsize=0, 
      linestyle='') 

cx.grid() 
cx.set_ylim(0, 0.03) 
plt.show() 

enter image description here

Hinweis im Code ergibt, dargestellt x eine Liste von Datetimes und d Zahlen dargestellt. Ich habe beschlossen, das umzukehren: Ich verwende für eine Liste der Datumswerte und x, um Zahlen darzustellen.

+0

Sie haben gerade meinen Tag gemacht !!! :) Ich danke dir sehr!! – Corins