Subham Burnwal
Subham Burnwal

Reputation: 369

Multiple multiple-bar graphs using matplotlib

I wish to do something like requested in here: Check any figure on page 6

example plot

Using subplots I have only managed to get the following result: grouped multiple-bar-chart using subplots

However, these are still 3 different graphs put side by side, and not like the above.

The closest answer I got here was this StackOverflow question here which is not using pyplot, and is also drawn under different boxes.

I am not providing any codes because this question is not specific to my code only, but just in case I should, do tell me.

Upvotes: 1

Views: 8328

Answers (1)

JohanC
JohanC

Reputation: 80339

The easiest way to simulate a two-level x-axis is via subplots and adapting the x-label. Erasing the intermediate spines and minimizing the distance helps to get a view similar to the linked example.

import matplotlib.pyplot as plt
import seaborn as sns

titanic = sns.load_dataset("titanic")
sns.set_style('whitegrid')
g = sns.catplot(x="sex", hue="alive", col="deck",
                data=titanic[titanic.deck.notnull()],
                kind="count", height=3, aspect=.4, palette='Set1')
for ax in g.axes.flat[1:]:
    sns.despine(ax=ax, left=True)
for ax in g.axes.flat:
    ax.set_xlabel(ax.get_title())
    ax.set_title('')
    ax.margins(x=0.1) # slightly more margin as a separation
plt.subplots_adjust(wspace=0, bottom=0.18, left=0.06)
plt.show()

sns.catplot with minimized distance between subplots

Here is another example, with rotated x-tick labels:

import matplotlib.pyplot as plt
import seaborn as sns

flights = sns.load_dataset("flights")
sns.set_style('whitegrid')
g = sns.catplot(x="month", y="passengers", col="year",
                data=flights[flights.year % 2 == 0],
                kind="bar", height=3, aspect=.7, palette='turbo')
for ax in g.axes.flat[1:]:
    sns.despine(ax=ax, left=True)
for ax in g.axes.flat:
    ax.set_xlabel(ax.get_title())
    ax.set_title('')
    ax.margins(x=0.03)
    ax.tick_params(axis='x', labelrotation=90)
plt.tight_layout()
plt.subplots_adjust(wspace=0)
plt.show()

sns.barplot with secondary x labels

Upvotes: 2

Related Questions