不连续的时间序列在x轴上绘制日期

Hyp*_*ube 5 python numpy matplotlib

我获得了几个月的数据,但在几个月之间缺失了.如果我将整个数据集绘制在一个绘图中(其间有很多空白空间),这看起来很奇怪.我写了一个小例子脚本来展示它是如何工作的(基于:Python/Matplotlib - 有没有办法制作一个不连续的轴?)

问题:我无法让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')
Run Code Online (Sandbox Code Playgroud)

结果: 结果

tac*_*ell 5

你已经找到了一个关于内部的有趣细节matplotlib.您传入的定位器对象set_major_locator 轴使用的对象,用于确定在哪里放置它的刻度都axes使用相同的定位器对象.作为绘图的一部分,定位器生成一个列表,其中的刻度应该基于轴的极限,当它完成第二个轴时,意味着在第一个轴上没有可见的刻度.你只需要传入不同的(单独的实例化)定位器对象,在这里完成copy.

import datetime
import copy

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, tight_layout=True)
fig.subplots_adjust(wspace=0.05)
fig.set_size_inches(10, 3, forward=True)

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


# note the copy here
ax.xaxis.set_major_locator(copy.copy(Locator))
ax.xaxis.set_major_formatter(copy.copy(Formatter))
ax2.xaxis.set_major_locator(copy.copy(Locator))
ax2.xaxis.set_major_formatter(copy.copy(Formatter))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述