zwol
zwol

Reputation: 140876

"panel barchart" in matplotlib

I would like to produce a figure like this one using matplotlib:

"panel barchart"
(source: peltiertech.com)

My data are in a pandas DataFrame, and I've gotten as far as a regular stacked barchart, but I can't figure out how to do the part where each category is given its own y-axis baseline.

Ideally I would like the vertical scale to be exactly the same for all the subplots and move the panel labels off to the side so there can be no gaps between the rows.

Upvotes: 1

Views: 1680

Answers (1)

jmz
jmz

Reputation: 4328

I haven't exactly replicated what you want but this should get you pretty close.

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

#create dummy data
cols = ['col'+str(i) for i in range(10)]
ind = ['ind'+str(i) for i in range(10)]
df = pd.DataFrame(np.random.normal(loc=10, scale=5, size=(10, 10)), index=ind, columns=cols)

#create plot
sns.set_style("whitegrid")
axs = df.plot(kind='bar', subplots=True, sharey=True, 
              figsize=(6, 5), legend=False, yticks=[], 
              grid=False, ylim=(0, 14), edgecolor='none', 
              fontsize=14, color=[sns.xkcd_rgb["brownish red"]])
plt.text(-1, 100, "The y-axis label", fontsize=14, rotation=90)  # add a y-label with custom positioning
sns.despine(left=True)  # get rid of the axes
for ax in axs:  # set the names beside the axes
    ax.lines[0].set_visible(False)  # remove ugly dashed line
    ax.set_title('')
    sername = ax.get_legend_handles_labels()[1][0]
    ax.text(9.8, 5, sername, fontsize=14)
plt.suptitle("My panel chart", fontsize=18)

Example chart with dummy data

Upvotes: 5

Related Questions