2

I am plotting Pandas Series data, which records the sum of "events" each week in 1981. The series is named 'weekly_data'.

1981-03-16    1826
1981-03-23    1895
1981-03-30    1964
1981-04-06    1978
1981-04-13    2034
1981-04-20    2073
1981-04-27    2057
dtype: int64

I would like to place ticks by year, and by week. When I try to plot this, I receive an AttributeError:

fig = plt.figure(figsize=(12,5))
ax = plt.subplots(111)
plt.plot(weekly_data, color = 'green' )
yloc = YearLocator()
mloc = MonthLocator()
ax.xaxis.set_major_locator(yloc)
ax.xaxis.set_minor_locator(mloc)
ax.grid(True)
plt.show()

The error is

AttributeError                            Traceback (most recent call last)
<ipython-input-92-843dbab30ed7> in <module>()
      6 yloc = YearLocator()
      7 mloc = MonthLocator()
----> 8 ax.xaxis.set_major_locator(yloc)
      9 ax.xaxis.set_minor_locator(mloc)
     10 ax.grid(True)

AttributeError: 'tuple' object has no attribute 'xaxis'

How can I fix this?

EDIT: Following Mike Mueller, the error above was plt.subplot(111). However, I still cannot get weekly ticks to work. Perhaps we need to use ax.set_xticks(major_ticks) or ax.set_xticks(minor_ticks, minor=True)

Here is the pandas series data I am plotting from 1991

Datetime
1990-12-23    1980
1990-12-30    1860
1991-01-06    1761
1991-01-13    1792
1991-01-20    1825
....
dtype: int64

and this is the code

fig = plt.figure(figsize=(12,5))
ax = plt.subplot(111)
plt.plot(weekly_data1991, color = 'green' )
yloc = YearLocator()
mloc = MonthLocator()
ax.xaxis.set_major_locator(yloc)
ax.xaxis.set_minor_locator(mloc)
ax.grid(True)
plt.show()

Here is the plot output

enter image description here

I am confused myself

4
  • Well, I don't know about matplotlib, but I think plt.gca() returns a tuple in this case (or maybe plt.subplots(111) returns a tuple). Commented Dec 21, 2015 at 4:10
  • 1
    @KevinGuan is partly right - plt.subplots will return a tuple containing the figure and axes (although plt.gca just returns the axes). I would usually unpack the output like this: fig, ax = plt.subplots(1, 1). If there is more than one row and/or column then ax will be a numpy array of axis objects. Commented Dec 21, 2015 at 4:20
  • @ali_m: Huh, understand now :) Commented Dec 21, 2015 at 4:21
  • @ali_m Unfortunately, fig, ax = plt.subplots(1, 1) removes all ticks from the plot in question, major or minor. Commented Dec 21, 2015 at 4:25

1 Answer 1

2

You are creating 111 subplots. Change:

ax = plt.subplots(111)

into:

ax = plt.subplot(111)

and you it should work. It creates one subplot array of one row, one column, and one subplot. For example, this:

plt.subplot(231)
plt.subplot(236)

creates subplot 1 and subplot 6 in array of 2 rows and 3 columns:

enter image description here

You make the months visible with:

ax.xaxis.set_minor_formatter(matplotlib.dates.DateFormatter('%B'))

Th year an the month are printed on top of each other. One solution is to have the years on the top and the months on bottom:

ax.xaxis.set_tick_params(labeltop='on', labelbottom='off')

Use:

mloc = matplotlib.dates.MonthLocator(range(1, 12, 4))

to show only every fourth month.

Sign up to request clarification or add additional context in comments.

2 Comments

Ah, I missed this. Thank you for the help. Can you explain the standard of 111? What does this exactly mean? Also, see my edit above---unfortunately there are no "monthly" ticks...
Yes, thank you. The labels are so close together---is there a way to plot every fourth month?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.