Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
139 views
in Technique[技术] by (71.8m points)

python - 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) 
question from:https://stackoverflow.com/questions/66056359/plot-multiple-small-line-graphs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...