alexv
alexv

Reputation: 65

create a subplot of multiple histograms with titles

I have a dataframe with a column named "CityName" with repeated names of cities and another column named "CarTripDuration" showing multiple car trip events. I want to create a histogram per city showing the car trip duration.

The following code generates a histogram per city in a separate plot with the same title (i.e., CarTripDuration). However, I would like all histograms in a singe plot (e.g., 10 rows by 5 columns) and each plot having the title of the city that the histogram corresponds to.

ax = data.groupby(['CityName']).hist(column='CarTripDuration')

Upvotes: 1

Views: 4180

Answers (1)

Corralien
Corralien

Reputation: 120409

You have to do a loop like this:

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

N = 100
df = pd.DataFrame({'City': np.random.choice(['London', 'Paris', 'Madrid', 'Berlin'], size=N),
                   'CarTripDuration': np.random.randint(10, 100, N)})

# Create subplots 
fig, axes = plt.subplots(nrows=2, ncols=2)
fig.subplots_adjust(hspace=0.5)
fig.suptitle('Distributions of CarTripDuration by City')

# Generate histograms
for ax, (name, subdf) in zip(axes.flatten(), df.groupby('City')):
    subdf.hist('CarTripDuration', ax=ax)
    ax.set_title(name)

plt.show()

Distributions of CarTripDuration by City

Update:

To use seaborn:

import seaborn as sns
sns.set()

Replace subdf.hist(...) by:

sns.histplot(subdf, x='CarTripDuration', ax=ax)

Seaborn

Upvotes: 3

Related Questions