0

I unfortunately cannot upload my dataset but here is how my dataset looks like:

            UMTMVS    month
DATE        
1992-01-01  209438.0    1
1992-02-01  232679.0    2
1992-03-01  249673.0    3
1992-04-01  239666.0    4
1992-05-01  243231.0    5
1992-06-01  262854.0    6
1992-07-01  222832.0    7
1992-08-01  240299.0    8
1992-09-01  260216.0    9
1992-10-01  252272.0    10
1992-11-01  245261.0    11
1992-12-01  245603.0    12
1993-01-01  223258.0    1
1993-02-01  246941.0    2
1993-03-01  264886.0    3
1993-04-01  249181.0    4
1993-05-01  250870.0    5
1993-06-01  271047.0    6
1993-07-01  224077.0    7
1993-08-01  248963.0    8
1993-09-01  269227.0    9
1993-10-01  263075.0    10
1993-11-01  256142.0    11
1993-12-01  252830.0    12
1994-01-01  234097.0    1
1994-02-01  259041.0    2
1994-03-01  277243.0    3
1994-04-01  261755.0    4
1994-05-01  267573.0    5
1994-06-01  287336.0    6
1994-07-01  239931.0    7
1994-08-01  276947.0    8
1994-09-01  291357.0    9
1994-10-01  282489.0    10
1994-11-01  280455.0    11
1994-12-01  279888.0    12
1995-01-01  260175.0    1
1995-02-01  286290.0    2
1995-03-01  303201.0    3
1995-04-01  283129.0    4
1995-05-01  289257.0    5
1995-06-01  310201.0    6
1995-07-01  255163.0    7
1995-08-01  293605.0    8
1995-09-01  313228.0    9
1995-10-01  301301.0    10
1995-11-01  293164.0    11
1995-12-01  290963.0    12
1996-01-01  263041.0    1
1996-02-01  290317.0    2

I want to set a locator for each year and ran the following code

ax = df.UMTMVS.plot(figsize=(12, 5))
ax.xaxis.set_major_locator(dates.YearLocator())

but it simply gives the following figure without any locator at all enter image description here

Why does the locator fail to point out the years?

Nuri Taş
  • 3,828
  • 2
  • 4
  • 22

1 Answers1

1

Try applying set_major_locator() to the axis before df.plot(). Like this:

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import dates

# reading your sample data into dataframe
df = pd.read_clipboard()

# dates should be dates (datetime), not strings
df.index = df.index.to_series().apply(pd.to_datetime)

fig, ax = plt.subplots(1,1,figsize=(12, 5))

# set locator before df.plot()
ax.xaxis.set_major_locator(dates.YearLocator())

df.UMTMVS.plot()

Result:

plot


Slightly different result could be achieved with last bit of code above modified to the following:

fig, ax = plt.subplots(1,1,figsize=(12, 5))
ax.plot(df.UMTMVS)
ax.xaxis.set_major_locator(dates.YearLocator())
plt.xlabel('DATE')
plt.show()

Result_alt (note the "padding" and loss of minor ticks):

result2

ouroboros1
  • 9,113
  • 3
  • 7
  • 26
  • I followed similar steps but couldn't get all the years. Also, you applied YearLocator but it still doesn't show each separate year, isn't this supposed to be a default value? When I simply write df.UMTMVS.plot() , it gives me the figure you have but I want to locate each year. – Nuri Taş Jul 23 '22 at 13:28
  • I'm not entirely sure what you mean. Applying one of the suggested methods, are you getting only *some* years, but not all? Or do you mean that my results do not show each separate year? Clearly they do, or do you have a different distribution in mind for the axis? – ouroboros1 Jul 23 '22 at 14:00
  • Sorry, It doesn't show each year in my case. When I ran the code you wrote on my dataset, I get only some years not all. – Nuri Taş Jul 23 '22 at 16:19
  • Hmm. Couple of qs: 1) Are you sure your entire df.index consists of actual datetime dates? Maybe check output for `df.index.dtype`. Is it: `dtype(' – ouroboros1 Jul 23 '22 at 17:02
  • 1) Yes, df.index is a datetime object 2)I manage to create some plots but it has locator in each 10 years 3) The dates have the 'MS' frequency. I didn't check all the dates but I don't think there are any missing dates. More importantly, I tried to plot a subset where I know there is a value for each 'MS' but it locates nothing What is interesting is that I try to set locators on very different datasets, and not only it doesn't work on any of them, but when I additionally set formatter, it only shows the year 1970 on various datasets. I have no idea why 1970 is a magic number here – Nuri Taş Jul 23 '22 at 17:30
  • 1970 *is* sort of a magic "number" (see [here](https://stackoverflow.com/questions/1090869/why-is-1-1-1970-the-epoch-time) why), so that might be very relevant. In 3rd comment on [this post](https://stackoverflow.com/questions/64919511/dateformatter-is-bringing-1970-as-year-not-the-original-year-in-the-dataset) it is claimed that a prev. version of matplotlib caused bad interaction with pd. From googling it appears that the version is 3.3. You might want to check: `import matplotlib` and then `matplotlib.__version__`. But in general, you could try upgrading: `pip install --upgrade matplotlib`. – ouroboros1 Jul 23 '22 at 18:04
  • the discussion about 1970 is a good gem. My matplotlib is of version 3.3.4. Anyway, thank you for your time and effort, I think I will pass over this issue for now. – Nuri Taş Jul 23 '22 at 18:30
  • 1
    "My matplotlib is of version 3.3.4". Right, so good chance that upgrading will fix the problem with the suggested solution. Good luck! – ouroboros1 Jul 23 '22 at 19:24