简体   繁体   中英

How do I change the starting point of x-ticks (using datetime data) in Matplotlib?

I have the following dataframe:

    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

And I need to plot a time series of the 'Clients Growth' column.

The 'Date' column is in the pandas datetime format.

So I used the following command:

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');

Output:

在此处输入图片说明
As you can see, I have changed the x-ticks interval to 3 months.

However, by default, matplotlib has started the x-ticks in 2016-07, and I would like the starting point to be in the first month that I have data (2016-08).

OBS: I know that if I change my inteval to 1 month instead of 3, the starting point of the x-ticks will be 2016-08, but I want to keep the interval as 3 months.

How can I solve this problem?

Thanks in advance.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM