编程语言
首页 > 编程语言> > python – 曲线拟合为’datetime’格式的时间序列?

python – 曲线拟合为’datetime’格式的时间序列?

作者:互联网

这是我的问题:polyfit没有采用日期时间值,所以我用mktime转换日期时间产生多项式拟合工作

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

然而,对于情节,我想在轴上的日期时间描述,并没有#弄清楚如何做到这一点.你能帮助我吗?

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

其余代码:

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

我的前4次数据点

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

我的前4个数据值

y = [0.00134328779552718,
     0.00155187668863844,
     0.0039431374327427,
     0.00780037563783297]

我计算的误差棒的标准偏差

yerr = [0.0000137547160254577,
        0.0000225670232594083,
        0.000105623642510075,
        0.00011343121508]

解决方法:

而不是绘制datenums,使用相关的日期时间.

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

产量

请注意,在代码中,x表示日期时间列表,d表示数字.我决定反过来说:我使用日期作为日期时间列表,x代表数字.

标签:mktime,axis-labels,python,datetime,curve-fitting
来源: https://codeday.me/bug/20190927/1823860.html