Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using matplotlib to plot a multiple boxplots

I have a data which looks like that:

  1. DataFrame with cities names, their latitude and longitude:
import pandas as pd

city = {'Name': ['San Franciso', 'Paris', 'Tokyo', 'London', 'Barcelona'], 'Latitude': [50.69460297, 43.64984221, 60.5331547, 62.5331547, 63.5331547],'Longtitude': [41.43147227, 49.78045496691, 122.23536080538, 19.78045496691, 29.78045496691]}

city_df = pd.DataFrame(city)
  1. List of 5 DataFrame which looks like that:
list1= [[1,"kids",0.00094], [2,"adult",0.0012], [3,"elderly",0.00114],[5,"kids",0.00088], [6,"adult",0.00113], [7,"elderly",0.00105]]
l1 = pd.DataFrame(list1)

list2= [[1,"kids",0.00044], [2,"adult",0.0012], [3,"elderly",0.00114],[5,"kids",0.00088], [6,"adult",0.00113], [7,"elderly",0.00105]]
l2 = pd.DataFrame(list2)

list3= [[1,"kids",0.00394], [2,"adult",0.0012], [3,"elderly",0.00114],[5,"kids",0.00588], [6,"adult",0.00113], [7,"elderly",0.00105]]
l3 = pd.DataFrame(list3)

list4= [[1,"kids",0.00074], [2,"adult",0.0012], [3,"elderly",0.00114],[5,"kids",0.00088], [6,"adult",0.00113], [7,"elderly",0.00105]]
l4 = pd.DataFrame(list4)

list5= [[1,"kids",0.00095], [2,"adult",0.0012], [3,"elderly",0.00114],[5,"kids",0.00043], [6,"adult",0.00113], [7,"elderly",0.00105]]
l5 = pd.DataFrame(list5)
    
l = [l1, l2, l3, l4, l5]

I want to create a plot looking like the one below enter image description here

For each city, a boxplot with values for a particular group, and for the y-axis, the cities are sorted by latitude.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I try to make that work with pd.concat and pd.melt (from: Plotting multiple boxplots in seaborn?).

It is a challenge for me. Thank you for your time.

>Solution :

You can concat with the city names as MultiIndex, and use seaborn.catplot to plot:

df = pd.concat(dict(zip(city_df['Name'], l)), names=['city']).reset_index(level=0)

import seaborn as sns
sns.catplot(data=df, row=1, x='city', y=2, kind='box', sharey=False)

output:

           city  0        1        2
0  San Franciso  1     kids  0.00094
1  San Franciso  2    adult  0.00120
2  San Franciso  3  elderly  0.00114
3  San Franciso  5     kids  0.00088
4  San Franciso  6    adult  0.00113
5  San Franciso  7  elderly  0.00105
0         Paris  1     kids  0.00044
1         Paris  2    adult  0.00120
2         Paris  3  elderly  0.00114
3         Paris  5     kids  0.00088
4         Paris  6    adult  0.00113
5         Paris  7  elderly  0.00105
0         Tokyo  1     kids  0.00394
1         Tokyo  2    adult  0.00120
2         Tokyo  3  elderly  0.00114
3         Tokyo  5     kids  0.00588
4         Tokyo  6    adult  0.00113
5         Tokyo  7  elderly  0.00105
0        London  1     kids  0.00074
1        London  2    adult  0.00120
2        London  3  elderly  0.00114
3        London  5     kids  0.00088
4        London  6    adult  0.00113
5        London  7  elderly  0.00105
0     Barcelona  1     kids  0.00095
1     Barcelona  2    adult  0.00120
2     Barcelona  3  elderly  0.00114
3     Barcelona  5     kids  0.00043
4     Barcelona  6    adult  0.00113
5     Barcelona  7  elderly  0.00105

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading