3

I have a Pandas data frame with a date column (in a datetime format) containing 3 years of data. Since it is a lot, I have grouped by month and year and plotted a countplot with matplotlib.

The graph doesn't show my x-axis labels, and also I suspect the results are not accurate (the graph looks different on excel), how can I add the labels? and make sure they are grouped by in the right order?

Here is the relevant code:

df['datetime_colummn'].groupby([df.datetime_colummn.dt.year.rename('year'),
         df.datetime_colummn.dt.month.rename('month')]).agg('count').plot()

And here is the graph:enter image description here

user11395824
  • 71
  • 1
  • 7
  • 1
    https://stackoverflow.com/a/43579371/6361531 – Scott Boston Dec 28 '19 at 06:04
  • This might help you out: https://stackoverflow.com/questions/43578976/pandas-missing-x-tick-labels – Yacine Mahdid Dec 28 '19 at 06:04
  • I'm pretty sure its a bug in the plotting capabilities of pandas. – Yacine Mahdid Dec 28 '19 at 06:05
  • @YacineMahdid I tried following the instructions in the question you've linked here but that didn't work either. Meaning, added: `fig, ax = plt.subplots()` and added `ax=ax` inside the `plot()`. That didn't change the final result – user11395824 Dec 28 '19 at 07:07
  • @ScottBoston I tried following the instructions in the question you've linked here but that didn't work either. Meaning, added: `fig, ax = plt.subplots()` and added `ax=ax` inside the `plot()`. That didn't change the final result – user11395824 Dec 28 '19 at 07:11
  • @user11395824 could you give a sample of your data so that we can help further? – Yacine Mahdid Dec 28 '19 at 15:17
  • @YacineMahdid yes, I downloaded it from this website: https://data.cityofnewyork.us/Public-Safety/NYPD-Complaint-Data-Historic/qgea-i56i/data – user11395824 Dec 29 '19 at 20:09

0 Answers0