【问题标题】:How to plot multiple columns into a single seaborn boxenplot如何将多列绘制成单个 seaborn boxenplot
【发布时间】:2022-01-06 13:35:34
【问题描述】:

我有两个图表,我想把它们放在一起,但我不知道怎么做?

代码:

import pandas as pd 
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_theme(style="whitegrid")
boxenplot_graph = sns.boxenplot(x=expeditions["nbre_members"], color = "r")
boxenplot_graph2 = sns.boxenplot(x=expeditions["hired_staff"],color = "b")
plt.xlabel("Nombre de members/hired_staff")
plt.title("Répartition du nombre de membres/hired_staff")
#plt.gca().legend(('membres', 'morts'))
plt.legend(["members", "hired_staff"],['rouge', 'bleu'])

【问题讨论】:

    标签: python pandas matplotlib seaborn


    【解决方案1】:
    • 使用.melt 将列转换为长格式
    • seaborn.boxenplot
      • 通过为值指定一个轴,为类别列指定另一个轴进行绘图。 hue= 可用于可视化第三个分类列。
    • 不需要图例(对于这种情况),因为每个标签都在轴上,所以有一个图例是多余的
    • 可以通过多种方式更改分类轴标签。
      • 熔解前更改列名(使用.rename
      • 熔化后更改列中的值(使用.map
      • 更改绘图的刻度标签 (p.set_yticklabels(['Total Bill', 'Tips'])
    • python 3.9.7pandas 1.3.4matplotlib 3.5.0seaborn 0.11.2中测试
    dfm = expeditions[["nbre_members", "hired_staff"]].melt()
    sns.boxenplot(data=dfm, x='value', y='variable')
    

    工作示例

    import seaborn as sns
    import matplotlib.pyplot as plt
    
    # sample data for wide data
    tips = sns.load_dataset('tips')
    
    # display(tips.head(3))
       total_bill   tip     sex smoker  day    time  size
    0       16.99  1.01  Female     No  Sun  Dinner     2
    1       10.34  1.66    Male     No  Sun  Dinner     3
    2       21.01  3.50    Male     No  Sun  Dinner     3
    
    # convert two columns to a long form
    dfm = tips[['total_bill', 'tip']].melt()
    
    # display(dfm.head(3))
         variable  value
    0  total_bill  16.99
    1  total_bill  10.34
    2  total_bill  21.01
    
    # plot
    fig, ax = plt.subplots(figsize=(6, 4))
    p = sns.boxenplot(data=dfm, x='value', y='variable', ax=ax)
    p.set(ylabel='My yLabel', xlabel='My xLabel', title='My Title')
    p.set_yticklabels(['Total Bill', 'Tips'])
    plt.show()
    

    给定第三列

    • 此选项将第三列用于hue=
    # melt columns and have an id variable
    dfm = tips[['total_bill', 'tip', 'smoker']].melt(id_vars='smoker')
    
    # display(dfm.head(3))
      smoker    variable  value
    0     No  total_bill  16.99
    1     No  total_bill  10.34
    2     No  total_bill  21.01
    
    # plot
    fig, ax = plt.subplots(figsize=(6, 4))
    p = sns.boxenplot(data=dfm, x='value', y='variable', hue='smoker', ax=ax)
    p.set(ylabel='My yLabel', xlabel='My xLabel')
    plt.show()
    

    【讨论】:

    • 哦,非常感谢,这真的很有帮助
    【解决方案2】:

    如果您有两个不同的数据集 expeditions1 和 expeditions2,请使用子图

    import pandas as pd 
    import matplotlib.pyplot as plt
    import seaborn as sns
    expeditions1=np.random.random_integers(1, 100, size=500)
    expeditions2=np.random.random_integers(1, 1000, size=500)
    sns.set_theme(style="whitegrid")
    fig,ax=plt.subplots(2,figsize=(10,10))
    boxenplot_graph = sns.boxenplot(y=expeditions1, color = "r",ax=ax[0])
    ax[0].set_title("One boxenplot")
    boxenplot_graph2 = sns.boxenplot(y=expeditions2,color = "b",ax=ax[1])
    plt.xlabel("hired staff")
    ax[1].set_title("Two boxenplot")
    #plt.gca().legend(('membres', 'morts'))
    plt.legend(["members", "hired_staff"],['rouge', 'bleu'])
    

    【讨论】:

    • 您可以将图例添加到子图matplotlib.org/stable/gallery/text_labels_and_annotations/…。你是什​​么图例值以及如何与数字对应
    • 你不需要两个 boxenplot 调用。由于您有一个数据集,因此您可以使用一个二维数组调用 boxensplot,其中一个元素是值,另一个元素是变量名。
    猜你喜欢
    • 2018-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    相关资源
    最近更新 更多