Matplotlib:以day.month.year样式格式化日期

时间:2012-12-19 10:27:32

标签: date formatting matplotlib

我想用x轴上的日期绘制一些线条,但我能找到的所有例子都使用美国风格,如12-31-2012。但我想要2012年12月31日,但它似乎只是从

更改日期格式化程序
dateFormatter = dates.DateFormatter('%Y-%m-%d')

dateFormatter = dates.DateFormatter('%d.%m.%y')

我的日期列表的工作原理如下:我想手动定义“firstDay”,然后在接下来的几天内生成X.当我打印结果列表时,我可以看到这一点。 但是当我想绘制该列表(由num2date转换)时,我的日期完全不同。

E.g。我将我的第一天设置为734517.0,即2012年1月15日。然后我在轴上打印我的日期,我得到第一个日期01.01.87 ??

这是我的完整代码:

import numpy as np
import matplotlib.pyplot as plot
import matplotlib.ticker as mticker
from matplotlib import dates
import datetime

fig = plot.figure(1)
DAU = (  2,  20,  25,  60, 190, 210,  18, 196, 212, 200, 160, 150, 185, 175, 316, 320, 294, 260, 180, 145, 135,  97,  84,  80,  60,  45,  37,  20,  20,  24,  39,  73,  99)
WAU = ( 50, 160, 412, 403, 308, 379, 345, 299, 258, 367, 319, 381, 461, 412, 470, 470, 468, 380, 290, 268, 300, 312, 360, 350, 316, 307, 289, 321, 360, 378, 344, 340, 346)
MAU = (760, 620, 487, 751, 612, 601, 546, 409, 457, 518, 534, 576, 599, 637, 670, 686, 574, 568, 816, 578, 615, 520, 499, 503, 529, 571, 461, 614, 685, 702, 687, 649, 489)

firstDay = 734517.0     #15. Januar 2012

#create an array with len(DAU) entries from given starting day...
dayArray = []
for i in xrange(len(DAU)):
    dayArray.append(firstDay + i)

#...and fill them with the converted dates
dayLabels = [dates.num2date(dayArray[j]) for j in xrange(len(DAU))]

for k in xrange(len(DAU)):
    print dayLabels[k]

spacing = np.arange(len(DAU)) + 1

line1 = plot.plot(spacing, DAU, 'o-', color = '#336699')
line2 = plot.plot(spacing, WAU, 'o-', color = '#993333')
line3 = plot.plot(spacing, MAU, 'o-', color = '#89a54e')

ax = plot.subplot(111)
plot.ylabel('', weight = 'bold')
plot.title('', weight = 'bold')
ticks, labels = plot.xticks(spacing, dayLabels)
plot.setp(labels, rotation = 90, fontsize = 11)

dateFormatter = dates.DateFormatter('%d.%m.%y')
ax.xaxis.set_major_formatter(dateFormatter)
#ax.fmt_xdata = dates.DateFormatter('%Y-%m-%d')
#fig.autofmt_xdate()

yMax = max(np.max(DAU), np.max(WAU), np.max(MAU))
yLimit = 100 - (yMax % 100) + yMax
plot.yticks(np.arange(0, yLimit + 1, 100))

plot.grid(True, axis = 'y')
plot.subplots_adjust(bottom = 0.5)
plot.subplots_adjust(right = 0.82)

legend = plot.legend((line1[0], line2[0], line3[0]),
                     ('DAU',
                     'WAU',
                     'MAU'),
                     'upper left',
                     bbox_to_anchor = [1, 1],
                     shadow = True)

frame = legend.get_frame()
frame.set_facecolor('0.80')
for t in legend.get_texts():
    t.set_fontsize('small')

plot.show()

使用这个日期格式化程序也没问题:

ax.fmt_xdata = dates.DateFormatter('%Y-%m-%d')

但是这也给了我时间戳,例如2012-01-15 00:00:00 + 00:00 如果有人能告诉我如何缩短休假时间,那真的很棒!!

1 个答案:

答案 0 :(得分:4)

在我看来,最简单的方法是使用真正的Datetime对象。这样您就可以使用datetime.timedelta(days = i)来设置日期范围。如果日期不规则,matplotlib会自动考虑间距。它还允许您使用matplotlib中的默认日期格式选项。

我留下了一些代码以保持简单,但你应该能够将它与你的脚本混合:

![import numpy as np
import matplotlib.pyplot as plot
import matplotlib.ticker as mticker
from matplotlib import dates
import datetime

fig = plot.figure(1)
DAU = (  2,  20,  25,  60, 190, 210,  18, 196, 212, 200, 160, 150, 185, 175, 316, 320, 294, 260, 180, 145, 135,  97,  84,  80,  60,  45,  37,  20,  20,  24,  39,  73,  99)
WAU = ( 50, 160, 412, 403, 308, 379, 345, 299, 258, 367, 319, 381, 461, 412, 470, 470, 468, 380, 290, 268, 300, 312, 360, 350, 316, 307, 289, 321, 360, 378, 344, 340, 346)
MAU = (760, 620, 487, 751, 612, 601, 546, 409, 457, 518, 534, 576, 599, 637, 670, 686, 574, 568, 816, 578, 615, 520, 499, 503, 529, 571, 461, 614, 685, 702, 687, 649, 489)

firstDay = datetime.datetime(2012,1,15)     #15. Januar 2012

dayArray = [firstDay + datetime.timedelta(days=i) for i in xrange(len(DAU))]

ax = plot.subplot(111)

line1 = ax.plot(dayArray, DAU, 'o-', color = '#336699')
line2 = ax.plot(dayArray, WAU, 'o-', color = '#993333')
line3 = ax.plot(dayArray, MAU, 'o-', color = '#89a54e')

ax.xaxis.set_major_formatter(dates.DateFormatter('%d.%m.%Y'))]

enter image description here

与脚本的主要区别在于“dayArray”的创建方式(并用作绘图命令中的x值)以及设置x轴格式的最后一行。