如何使用seaborn箱线图获得多种色调条件

2024-04-19 11:30:31 发布

您现在位置:Python中文网/ 问答频道 /正文

作为python、pandas、seaborn等的新用户,我正在寻求您的帮助。 这是我的问题:我有一个具有以下结构的熊猫数据帧:

df = pd.DataFrame([[1, "M1","2020","Brut","A",0.51], 
                   [1, "M2","2020","Brut","A",0.6], 
                   [1, "M3","2020","Brut","A",0.2],
                   [1, "M1","2030","Brut","A",0.5],
                   [1, "M2","2030","Brut","A",0.6],
                   [1, "M3","2030","Brut","A",0.8],
                   [2, "M1","2020","Brut","B",1], 
                   [2, "M2","2020","Brut","B",0.4], 
                   [2, "M3","2020","Brut","B",0.3],
                   [3, "M1","2030","Brut","B",0.6],
                   [3, "M2","2030","Brut","B",0.85],
                   [3, "M3","2030","Brut","B",0.59]], 
                   columns=['Site', 'Model','Time','Type','Region','Value'])

这就是熊猫的数据帧

    Site Model  Time  Type Region  Value
0      1    M1  2020  Brut      A   0.51
1      1    M2  2020  Brut      A   0.60
2      1    M3  2020  Brut      A   0.20
3      1    M1  2030  Brut      A   0.50
4      1    M2  2030  Brut      A   0.60
5      1    M3  2030  Brut      A   0.80
6      2    M1  2020  Brut      B   1.00
7      2    M2  2020  Brut      B   0.40
8      2    M3  2020  Brut      B   0.30
9      3    M1  2030  Brut      B   0.60
10     3    M2  2030  Brut      B   0.85
11     3    M3  2030  Brut      B   0.59

我想做的是有一个seaborn箱线图,场地和区域都是“色调”条件。 我想知道每个站点对应的区域。例如,我想网站2&;3连接到区域B。 现在,我只能通过一个简单的箱线图成功

这是我的密码:

import pandas as pd
from os.path import isfile, join
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.patches import PathPatch

path=r"C:\Downloads\test_seaborn.csv"
df=pd.read_csv(path,";")

fig = plt.gcf()
# Change seaborn plot size
fig.set_size_inches(20, 20)
#change seaborn x and y axis size
sns.set(font_scale=1.4)
sns.set_style("darkgrid") #format of the grid
# call boxplot function, x and y must be columns of the dataframe, part of the code to add the mean
bp=sns.boxplot(x='Site',y='Value',hue="Time", data=df)
# add a title
bp.set_title("Boxplot per site",fontdict= { 'fontsize': 24, 'fontweight':'bold'})
# set x label and y label size and name
bp.set_xlabel('Site',fontsize = 20, )
bp.set_ylabel('Value (m²/s)',fontsize = 20 )
# set legend caracteristics
bp.legend(fontsize = 20, \
               title="Time", \
               title_fontsize = 18, \
               shadow = True, \
               facecolor = 'white')

有什么办法帮我吗

致意 纳塔恩


Tags: andimportsizetimevaluesiteseabornm3