I'd like to create a grouped bar chart that shows a customized Date-Time Index - just showing Month and year instead of the full dates. I want the bars to be grouped and not stacked.
I assumed pandas could handle this easily, using:
import pandas as pd
import matplotlib.pylab as plt
import matplotlib.dates as mdates
testdata = pd.DataFrame({"A": [1, 2, 3]
,"B": [2, 3, 1]
, "C": [2, 3, 1]}
,index=pd.to_datetime(pd.DatetimeIndex(
data=["2019-03-02", "2019-04-01","2019-05-01"])))
ax = testdata.plot.bar()
This creates the plot that I want, I'd just like to change to date into something more simple, like March 2019, April 2019, May 2019.
I assumed using a Custom Date Formatter would work, therefore I tried
ax.xaxis.set_major_locator(mdates.MonthLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %Y'))
But than my labels are gone completely. And this question implies that pandas and the DateFormatter have a bit of a difficult relationship. Therefore I tried to do it with Matplotlib basics:
fig, ax = plt.subplots()
width = 0.8
ax.bar(testdata.index, testdata["A"])
ax.bar(testdata.index, testdata["B"])
ax.bar(testdata.index, testdata["C"])
ax.xaxis.set_major_locator(mdates.MonthLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %Y'))
plt.show()
Now the date representation is as expected (although the whitespace could be reduced), but the data overlap, which doesn't help.
Defining a width and subtracting it from the x values (as suggested normally) won't help due to the DateTime-Index I use. I get an error that subtracting DatetimeIndes and float is unsupported.
fig, ax = plt.subplots()
width = 0.8
ax.bar(testdata.index-width, testdata["A"])
ax.bar(testdata.index, testdata["B"])
ax.bar(testdata.index+width, testdata["C"])
ax.xaxis.set_major_locator(mdates.MonthLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %Y'))
plt.show()
So now I'm running out of ideas and hope for input