【问题标题】:How do I change the starting point of x-ticks (using datetime data) in Matplotlib?如何在 Matplotlib 中更改 x-ticks 的起点(使用日期时间数据)?
【发布时间】:2019-12-19 23:06:56
【问题描述】:

我有以下数据框:

    Date        Prod_1  Prod_2  Clients     Clients Growth
0   2016-08-01  17768   0.0     17768.0     9.877308
1   2016-09-01  19523   0.0     19523.0     10.295549
2   2016-10-01  21533   0.0     21533.0     7.709098
3   2016-11-01  23193   0.0     23193.0     17.410426
4   2016-12-01  27231   0.0     27231.0     -3.473982
5   2017-01-01  26285   0.0     26285.0     0.604908
6   2017-02-01  26444   0.0     26444.0     26.864317
7   2017-03-01  33548   0.0     33548.0     -12.626684
8   2017-04-01  29312   0.0     29312.0     21.114219
9   2017-05-01  35501   0.0     35501.0     6.577280
10  2017-06-01  37836   0.0     37836.0     3.282588
11  2017-07-01  39078   0.0     39078.0     7.733251
12  2017-08-01  42100   0.0     42100.0     -3.111639
13  2017-09-01  40790   0.0     40790.0     5.339544
14  2017-10-01  42968   0.0     42968.0     -5.797338
15  2017-11-01  40477   0.0     40477.0     13.508906
16  2017-12-01  45945   0.0     45945.0     -11.881598
17  2018-01-01  40486   0.0     40486.0     5.893395
18  2018-02-01  42872   0.0     42872.0     16.323008
19  2018-03-01  49870   0.0     49870.0     -4.958893
20  2018-04-01  47397   0.0     47397.0     13.408022
21  2018-05-01  53752   0.0     53752.0     -12.354889
22  2018-06-01  47111   0.0     47111.0     13.733523
23  2018-07-01  53581   0.0     53581.0     3.939829
24  2018-08-01  55692   0.0     55692.0     -6.834016
25  2018-09-01  51886   0.0     51886.0     9.784913
26  2018-10-01  56963   0.0     56963.0     -0.405526
27  2018-11-01  56732   0.0     56732.0     4.343228
28  2018-12-01  59196   0.0     59196.0     -3.327928
29  2019-01-01  57221   5.0     57226.0     -2.200049
30  2019-02-01  55495   472.0   55967.0     18.189290
31  2019-03-01  65394   753.0   66147.0     -8.984534
32  2019-04-01  59030   1174.0  60204.0     11.718490
33  2019-05-01  64466   2793.0  67259.0     -6.504706
34  2019-06-01  58471   4413.0  62884.0     12.739330
35  2019-07-01  64785   6110.0  70895.0     1.747655
36  2019-08-01  63774   8360.0  72134.0     2.423268
37  2019-09-01  64324   9558.0  73882.0     3.926531
38  2019-10-01  65733   11050.0 76783.0     NaN

我需要绘制“客户增长”列的时间序列。

“日期”列采用 pandas 日期时间格式。

所以我使用了以下命令:

import matplotlib.dates as mdates
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(12,4))
ax = fig.add_subplot(1,1,1)  
plt.plot('Date', 'Clients Growth', data=test, linewidth=2, color='steelblue')

ax.xaxis.set_major_locator(mdates.MonthLocator(interval=3))
plt.xticks(rotation=45, ha='right');

输出:


如您所见,我已将 x-ticks 间隔更改为 3 个月。

但是,默认情况下,matplotlib 已在 2016-07 开始 x-ticks,我希望起点是在我有数据的第一个月(2016-08)。

OBS:我知道如果我将间隔更改为 1 个月而不是 3 个月,x-ticks 的起点将是 2016-08,但我想保持间隔为 3 个月。

我该如何解决这个问题?

提前致谢。

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

    您可以提供要打勾的月份列表,

    MonthLocator(bymonth=(2, 5, 8, 11))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-19
      • 2018-12-12
      • 2016-10-19
      • 1970-01-01
      • 2021-02-03
      • 2017-07-15
      • 2021-12-09
      • 2017-10-13
      相关资源
      最近更新 更多