使用 matplotlib 绘制多个箱线图

Using matplotlib to plot a multiple boxplots

我有一个看起来像这样的数据:

  1. 带有城市名称、纬度和经度的 DataFrame:
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. 看起来像这样的 5 个 DataFrame 的列表:
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]

我想创建一个如下图所示的情节enter image description here

对于每个城市,箱线图包含特定组的值,对于 y 轴,城市按纬度排序。

我尝试使用 pd.concatpd.melt(来自:)。

这对我来说是一个挑战。谢谢你的时间。

您可以concat将城市名称作为MultiIndex,并使用seaborn.catplot绘制:

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)

输出:

           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