0

I'm trying to make a stacked bar chart where the x-axis is a combination of years and quarters.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.DataFrame({'year': [2019, 2019, 2019, 2019, 2020, 2020, 2020, 2020],
                   'quarter': [1, 2, 3, 4, 1, 2, 3, 4],
                   'value1': range(1, 9),
                   'value2': np.ones(8)})
df.set_index(['year', 'quarter'], inplace=True)
df.plot.bar(stacked=True)
plt.tight_layout()
plt.show()

Currently, it looks like this: enter image description here

However, I would like the axis labels to be hierarchical, such that 2019 and 2020 only show once on the bottom, and above it it shows the quarter.

Is there an easy way to do this other than using a separate subplot for each year?

Wouter
  • 3,201
  • 6
  • 17
  • Or this... https://stackoverflow.com/questions/20532614/multiple-lines-of-x-tick-labels-in-matplotlib – Redox Feb 06 '23 at 16:04

0 Answers0