python时间序列预测不连续怎么办_python – 不连续的时间序列在x轴上绘制日期
我獲得了幾個月的數(shù)據(jù),但在幾個月之間缺失了.如果我將整個數(shù)據(jù)集繪制在一個繪圖中(其間有很多空白空間),這看起來很奇怪.
我寫了一個小例子腳本來展示它是如何工作的(基于:
Python/Matplotlib – Is there a way to make a discontinuous axis?)
問題:我無法讓x軸使用相同的日期格式! ax或ax2都是正確的,但從來都不是.
你有什么主意嗎?
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import datetime
def getDates(startdate, enddate):
days = (enddate + datetime.timedelta(days=1) - startdate).days
dates = [ startdate + datetime.timedelta(days=x) for x in range(0,days) ]
return dates
dates1 = getDates(datetime.datetime(2013,1,1), datetime.datetime(2013,1,31))
dates2 = getDates(datetime.datetime(2013,3,1), datetime.datetime(2013,3,31))
dates = dates1+dates2
data = np.arange(len(dates))
Locator = mpl.dates.DayLocator(interval=5)
Formatter = mpl.dates.DateFormatter('%d-%m-%y')
fig,(ax,ax2) = plt.subplots(1,2,sharey=True)
fig.subplots_adjust(wspace=0.05)
fig.set_size_inches(10,3)
ax.plot(dates, data)
ax2.plot(dates, data)
ax.legend(loc=1)
ax.set_ylim( 0, 61 )
ax.set_xlim( datetime.datetime(2013,1,1), datetime.datetime(2013,1,31) )
ax2.set_xlim( datetime.datetime(2013,3,1), datetime.datetime(2013,3,31) )
labels = ax.get_xticklabels()
for label in labels: label.set_rotation(30)
labels = ax2.get_xticklabels()
for label in labels: label.set_rotation(30)
ax.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)
ax.tick_params(right='off')
ax2.tick_params(left='off')
ax2.yaxis.tick_right()
ax.xaxis.set_major_locator(Locator)
ax.xaxis.set_major_formatter(Formatter)
ax2.xaxis.set_major_locator(Locator)
ax2.xaxis.set_major_formatter(Formatter)
plt.savefig("test.png", bbox_inches='tight')
結(jié)果:
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的python时间序列预测不连续怎么办_python – 不连续的时间序列在x轴上绘制日期的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux 的内核参数优化,Linux服
- 下一篇: showdoc如何创建文件夹_showD