1

I have the following dataframe:

    Date        Prod_01  Prod_02
19  2018-03-01  49870    0.0
20  2018-04-01  47397    0.0
21  2018-05-01  53752    0.0
22  2018-06-01  47111    0.0
23  2018-07-01  53581    0.0
24  2018-08-01  55692    0.0
25  2018-09-01  51886    0.0
26  2018-10-01  56963    0.0
27  2018-11-01  56732    0.0
28  2018-12-01  59196    0.0
29  2019-01-01  57221    5.0
30  2019-02-01  55495    472.0
31  2019-03-01  65394    753.0
32  2019-04-01  59030    1174.0
33  2019-05-01  64466    2793.0
34  2019-06-01  58471    4413.0
35  2019-07-01  64785    6110.0
36  2019-08-01  63774    8360.0
37  2019-09-01  64324    9558.0
38  2019-10-01  65733    11050.0

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

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

So I used the following command:

plt.figure(figsize=(10,4))
plt.plot('Date', 'Prod_01', data=test, linewidth=2, color='steelblue')
plt.xticks(rotation=45, horizontalalignment='right');

Output:

enter image description here

However, I want to change the frequency of the xticks to one month, so I get one tick and one label for each month.

I have tried the following command:

plt.figure(figsize=(10,4))
plt.plot('Date', 'Prod_01', data=test, linewidth=2, color='steelblue')
plt.xticks(np.arange(1, len(test), 1), test['Date'] ,rotation=45, horizontalalignment='right');

But I get this: enter image description here

How can I solve this problem? Thanks in advance.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
Caroline
  • 373
  • 1
  • 5
  • 13
  • I found these two sources: https://stackoverflow.com/questions/44863375/how-to-change-spacing-between-ticks-in-matplotlib and https://subscription.packtpub.com/book/big_data_and_business_intelligence/9781849513265/3/ch03lvl1sec48/controlling-tick-spacing – AviFS Dec 18 '19 at 20:12
  • I have seen this post, but I don't know how to addapt my code. – Caroline Dec 18 '19 at 20:13
  • Do the docs help? Just us a MonthLocator as your major locator... https://matplotlib.org/3.1.1/gallery/text_labels_and_annotations/date.html – Jody Klymak Dec 18 '19 at 20:49
  • I couldn't reproduce the problem, using sample of the data-frame and your code and it worked – David Dec 18 '19 at 20:53

1 Answers1

-2

I'm not very familiar with pandas data frames. However, I can't see why this wouldn't work with any pyplot:

According the top SO answer on related post by ImportanceOfBeingErnest:

The spacing between ticklabels is exclusively determined by the space between ticks on the axes.

So, to change the distance between ticks, and the labels you can do this:

Suppose a cluttered and base-10 centered person displays the following graph: Cluttered

It takes the following code and importing matplotlib.ticker:

import numpy as np
import matplotlib.pyplot as plt
# Import this, too
import matplotlib.ticker as ticker


# Arbitrary graph with x-axis = [-32..32]
x = np.linspace(-32, 32, 1024)
y = np.sinc(x)

# -------------------- Look Here --------------------
# Access plot's axes
axs = plt.axes()
# Set distance between major ticks (which always have labels)
axs.xaxis.set_major_locator(ticker.MultipleLocator(5))
# Sets distance between minor ticks (which don't have labels)
axs.xaxis.set_minor_locator(ticker.MultipleLocator(1))
# -----------------------------------------------------

# Plot and show graph
plt.plot(x, y)
plt.show()



To change where the labels are placed, you can change the distance between the 'major ticks'. You can also change the smaller 'minor ticks' in between, which don't have a number attached. E.g., on a clock, the hour ticks have numbers on them and are larger (major ticks) with smaller, unlabeled ones between marking the minutes (minor ticks).

By changing the --- Look Here --- part to:

# -------------------- Look Here --------------------
# Access plot's axes
axs = plt.axes()
# Set distance between major ticks (which always have labels)
axs.xaxis.set_major_locator(ticker.MultipleLocator(8))
# Sets distance between minor ticks (which don't have labels)
axs.xaxis.set_minor_locator(ticker.MultipleLocator(4))
# -----------------------------------------------------

You can generate the cleaner and more elegant graph below: Clean

Hope that helps!

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
AviFS
  • 336
  • 2
  • 12
  • 2
    The problem OP faces is that they have dates. So this answer is not very helpful in that case. Check the third comment below the question, which says what to do in such case. – ImportanceOfBeingErnest Dec 18 '19 at 21:54
  • @ernest Thank you very much for explaining! Sorry for the false advice. Also, I apologize for misspelling your name, how funny that you found it! Perhaps, with some upvotes, this answer could still help other people who search something similar, though. The title _How to change ticks position in Matplotlib (Python)?_ could certainly attract people who'd still benefit from the answer... even if wasn't OP's problem. – AviFS Dec 18 '19 at 22:07
  • 2
    Well, but if they are searching for this kind of answer, they would better be guided to one of the existing threads on that topic, right? (Note that you can always answer old questions as well, if you think existing solutions lack detail - but spreading solutions over different threads is not so nice to future searchers.) You are right that the title is not very suitable here. I edited it, and also closed as duplicate. – ImportanceOfBeingErnest Dec 18 '19 at 22:25