matplotlib.dates(和 datestr2num)的绘图刻度中的日期格式问题

Date format issues in plot ticks with matplotlib.dates (and datestr2num)

我正在使用 matplotlib.dates 绘制一个条形图,其中包含在特定日期发生的实例(以字符串列表的形式呈现),并使用 matplotlib.dates.datestr2num 显示两组数据日期(根据 Python matplotlib multiple bars 中的最佳答案)。

但是,对于每月第 12 天以下的日期,绘图将日期解释为 MM/DD/YY 格式,而对于每月第 12 天以上的日期,它将日期解释为 DD/MM/YY,导致数据在绘图中跳跃。我认为问题可能在于我如何使用 datestr2num,但不确定如何强制它采用一种或另一种格式。

import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter, AutoDateLocator, AutoDateFormatter, datestr2num

days = ['30/01/2019', '31/01/2019', '01/02/2019', '02/02/2019', '03/02/2019', '04/02/2019']
adata = [1, 9, 10, 3, 7, 6]
bdata = [1, 2, 11, 3, 6, 2]

x=datestr2num(days)

w=0.25
fig = plt.figure(figsize = (8,4))
ax = fig.add_subplot(111)
ax.bar(x-w, adata, width=2*w, color='g', align='center', tick_label = days)
ax.bar(x+w, bdata, width=2*w, color='r', align='center', tick_label = days)
ax.xaxis_date()
ax.xaxis.set_major_locator(AutoDateLocator(minticks=3, interval_multiples=False))
ax.xaxis.set_major_formatter(DateFormatter("%d/%m/%y"))

plt.show()

在上面的示例中,adatabdata 位于后续日期(1 月 30 日至 2 月 4 日),所有条形显示为彼此相邻,但图表显示的数据介于1 月 2 日和 4 月 2 日,使数据出现乱序。

任何帮助或澄清都会有所帮助,谢谢!

看来 datestr2num() calls dateutil.parser.parse(d, default=default). dateutil.parser.parse has a kwarg dayfirst but datestr2num() does not provide a way to pass the argument forward. If you pass a list of dates to datesttr2num() it will .

>>> [dateutil.parser.parse(day, dayfirst=True) for day in days]
[
    datetime.datetime(2019, 1, 30, 0, 0), 
    datetime.datetime(2019, 1, 31, 0, 0), 
    datetime.datetime(2019, 2, 1, 0, 0), 
    datetime.datetime(2019, 2, 2, 0, 0), 
    datetime.datetime(2019, 2, 3, 0, 0), 
    datetime.datetime(2019, 2, 4, 0, 0)
]

>>> datestr2num(days)
[737089. 737090. 737061. 737092. 737120. 737151.]

>>> datestr2num(days, dayfirst=True)
Traceback (most recent call last):
File "C:\Users\bcohan\Downloads\so.py", line 22, in <module>
    print(datestr2num(days, dayfirst=True))
TypeError: datestr2num() got an unexpected keyword argument 'dayfirst'

我建议先用 datetime 处理日期,然后 运行 处理其余日期。 (除非合理地重写原始 days 列表中的字符串。)

x = datestr2num([
    datetime.strptime(day, '%d/%m/%Y').strftime('%m/%d/%Y')
    for day in days
])

这是一个完整的脚本。

from datetime import datetime
import matplotlib.pyplot as plt
from matplotlib.dates import (
    DateFormatter, AutoDateLocator, AutoDateFormatter, datestr2num
)

days = [
    '30/01/2019', '31/01/2019', '01/02/2019',
    '02/02/2019', '03/02/2019', '04/02/2019'
]
adata = [1, 9, 10, 3, 7, 6]
bdata = [1, 2, 11, 3, 6, 2]

x = datestr2num([
    datetime.strptime(day, '%d/%m/%Y').strftime('%m/%d/%Y')
    for day in days
])
w = 0.25

fig = plt.figure(figsize=(8, 4))
ax = fig.add_subplot(111)
ax.bar(x - w, adata, width=2 * w, color='g', align='center', tick_label=days)
ax.bar(x + w, bdata, width=2 * w, color='r', align='center', tick_label=days)
ax.xaxis_date()
ax.xaxis.set_major_locator(
    AutoDateLocator(minticks=3, interval_multiples=False))
ax.xaxis.set_major_formatter(DateFormatter("%d/%m/%y"))

plt.show()