1

Im totally new in this field and almost everything is still a mistery to me.

I have the following data frame:

            usersPerWeek date
      date      
2018-03-07  127          2018-03-07
2018-03-14  3177         2018-03-14
2018-03-21  8758         2018-03-21
2018-03-28  16770        2018-03-28
2018-04-04  17964        2018-04-04

And I am trying to plot this on as a simple bar chart:

import pandas as pd
import matplotlib.pyplot as plt
figPerWeek = plt.figure(dpi=80, figsize=(8,1))
axis = figPerWeek.add_subplot(1,1,1)
axis.bar(x=data.index, height=data.usersPerWeek)

This results in the error message Axis must havefreqset to convert to Periods

When I add (just before calling axis.bar)

axis.xaxis_date()

I then get the error message 'Period' object has no attribute 'toordinal'.

Im hoping that I am missing something really simple, but so far Google has been less than helpful.

Cheers

mg74
  • 651
  • 1
  • 6
  • 10
  • 1
    See [mcve] or possibly [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). – ImportanceOfBeingErnest Jun 03 '18 at 14:31

1 Answers1

1

It seems to be working in the example below.

from numpy.random import randint
import pandas as pd
import matplotlib.pyplot as plt

# Create data frames for the example
rng = pd.date_range('3/7/2018 00:00', periods=10, freq='1w')
df = pd.DataFrame({'usersPerWeek': randint(10, 200, 10)}, index=rng)

figPerWeek = plt.figure(dpi=80, figsize=(8,1))
axis = figPerWeek.add_subplot(1,1,1)
axis.bar(x=df.index, height=df.usersPerWeek)
plt.ylabel('usersPerWeek')
plt.show()

enter image description here

KRKirov
  • 3,854
  • 2
  • 16
  • 20
  • That works for me as well. The difference between your example and mine is the index. Your index is a `DateTimeIndex' while mine is 'PeriodIndex'. After seeing that and google a bit, the solution for me is: axis.bar(x=data.index.to_timestamp(), height=data.usersPerWeek) Thanks a lot for the help! – mg74 Jun 03 '18 at 14:56
  • This is what I suspected from the start. – KRKirov Jun 03 '18 at 14:56