gpt4 book ai didi

python - x 轴上带有日期的不连续时间序列图

转载 作者:太空狗 更新时间:2023-10-30 01:08:54 33 4
gpt4 key购买 nike

我得到了几个月的数据,但中间有几个月丢失了。如果我在一个图中绘制整个数据集(中间有很多空白区域),这看起来很奇怪。我写了一个小示例脚本来展示它是如何工作的(基于: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')

结果: Result

最佳答案

您发现了有关 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))

enter image description here

关于python - x 轴上带有日期的不连续时间序列图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18982418/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com