carousallie
carousallie

Reputation: 863

Plot multiple small line graphs

I have some data where I have deaths each year across each state. I am attempting to make several small line graphs (sort of like what's demonstrated here), however, I cannot get my data to go the way it seems to need to go to make this.

Without attempting this method, I attempted df.pivot(index="fyear", columns="state_abbr", values="total_deaths").plot() which of course just put 50 lines on one graph...not a great look.

How do I get individual line graphs for each state in a neat, organized way? (well, as neat as you can get with 50 graphs)

Sample Data

data = {'state_abbr':['AL', 'AK', 'AZ', 'AL', 'AK', 'AZ', 'AL', 'AK', 'AZ',], 
        'fyear':[2014, 2014, 2014, 2015, 2015, 2015, 2016, 2016, 2016],
        'total_deaths':[500, 490, 510, 530, 480, 510, 600, 400, 230]} 
df = pd.DataFrame(data) 

Upvotes: 1

Views: 161

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

You can use seaborn's FacetGrid for this:

# sns style, looks like those in your picture
sns.set()

# play with `col_wrap`
g = sns.FacetGrid(df, col='state_abbr', col_wrap=5)
g.map(sns.lineplot, 'fyear', 'total_deaths')

Output:

enter image description here

Upvotes: 1

Related Questions