Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
570 views
in Technique[技术] by (71.8m points)

python - Curve Fitting to a time series in the format 'datetime'?

Here is my problem: polyfit does not take datetime values, so that I converted datetime with mktime producing the polynomial fit works

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

For the plot however, I would like the datetime description on the axis and didn't # figure out how to do that. Can you help me?

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 of the code:

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

my first 4 time data points

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

my first 4 data values

y = [0.00134328779552718,
     0.00155187668863844,
     0.0039431374327427,
     0.00780037563783297]

my calculated standard deviations for error bars

yerr = [0.0000137547160254577,
        0.0000225670232594083,
        0.000105623642510075,
        0.00011343121508]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Instead of plotting datenums, use the associated datetimes.


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

yields

enter image description here

Note in your code, x represented a list of datetimes, and d represented numbers. I've decided to reverse that: I use dates for a list of datetimes, and x to represent numbers.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...