3

I have a pretty simple set of data as displayed below. I am looking for a way to plot this stacked bar chart and format the x-axis (dates) so it starts at 1996-31-12 and ends at 2016-31-12 on increments of 365 days. The code I have written is plotting every single date and therefore the x-axis is very bunched up and not readable.

Datafame:

Date             A             B  
1996-31-12       10            3
1997-31-03       5             6
1997-31-07       7             5
1997-30-11       3             12
1997-31-12       4             10
1998-31-03       5             8
.
.
.
2016-31-12       3             9
spacedinosaur10
  • 695
  • 3
  • 10
  • 24

1 Answers1

4

This is a similar question: Pandas timeseries plot setting x-axis major and minor ticks and labels

You can manage this using matplotlib itself instead of pandas.

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

# if your dates are strings you need this step
df.Date = pd.to_datetime(df.Date)

fig,ax = plt.subplots()
ax.plot_date(df.Date,df.A)
ax.plot_date(df.Date,df.B)
ax.xaxis.set_major_locator(mdates.YearLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b\n%Y'))
plt.show()
Community
  • 1
  • 1
mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
  • I tried the code above and I get an error message : AttributeError: 'str' object has no attribute 'toordinal'. Any ideas? – spacedinosaur10 Mar 13 '17 at 16:52
  • 1
    Oh are your dates strings? Please see edited answer. – mechanical_meat Mar 13 '17 at 17:03
  • I guess the other issue I am now having that cant seem to figure out is i need a stacked bar chart. I tried adding in kind=bar and stacked=true but nothing seems to work with the plot_date function – spacedinosaur10 Mar 14 '17 at 17:35
  • 1
    I've been trying to get a solution to this using pandas and haven't been able to. It looks like there might be an issue specific to bar plots here: https://github.com/pandas-dev/pandas/issues/14119 – mechanical_meat Mar 15 '17 at 00:08