本文介绍了曲线拟合到“日期时间"格式的时间序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题:polyfit不使用日期时间值,所以我用mktime转换了日期时间,从而产生了多项式拟合工作

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

其余代码:

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]

我计算出的误差线的标准偏差

my calculated standard deviations for error bars

yerr = [0.0000137547160254577,
        0.0000225670232594083,
        0.000105623642510075,
        0.00011343121508]

推荐答案

使用关联的日期时间代替绘制datenum.

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

收益

在代码中请注意,x代表日期时间列表,而d代表数字.我决定扭转这种情况:我将dates用于日期时间列表,并将x用于表示数字.

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.

这篇关于曲线拟合到“日期时间"格式的时间序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 06:09