如何使用 seaborn.relplot 绘制宽格式数据框 [英] How to plot wide format dataframe with seaborn.relplot

查看:65
本文介绍了如何使用 seaborn.relplot 绘制宽格式数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 5 个城市 (C1-C5) 的虚拟数据绘制以下折线图.

根据我的理解,x=Year"y=员工人数"hue=City"代码>.我将如何设置它的代码?我试过按以下方式做,但没有用!

当前代码

将 seaborn 导入为 sns将熊猫导入为 pd区域 = r'C:\Users\Tachi\Desktop\City.xlsx'df = pd.read_excel(区域)df.set_index('City', inplace=True)sns.relplot(x=Year", y=员工人数",hue=City", kind=line", data=df)

示例数据

data = {'City': ['C1', 'C2', 'C3', 'C4', 'C5'],2015: [28564, 2585, 4679, 33227, 2000],2016: [83659, 4429, 35834, 1447, 3454],2017: [0, 453, 40903, 46826, 646],2018 年:[39470、8364、29464、36443、8364]}df = pd.DataFrame(数据)df.set_index('City', inplace=True)2015 2016 2017 2018城市C1 28564 83659 0 39470C2 2585 4429 453 8364C3 4679 35834 40903 29464C4 33227 1447 46826 36443C5 2000 3454 646 8364

解决方案

  • 给定 OP 中的测试数据框 df,绘制数据框的最简单方法是使用

    • 索引值为 int dtype,因此 x 轴使用中间数字进行格式化.
      • 解决此问题的一种方法是在绘图之前将索引转换为 str dtype.

    # 设置年份索引为 str dtypedf.index = df.index.astype(str)# 绘制数据框sns.relplot(data=df,kind='line',marker='o')

    I am trying to plot the following line graph with dummy data on 5 cities(C1-C5).

    Based on what I understand, x="Year", y="Number of Employees" and hue="City". How would I set up the code for it? I have tried doing it in the following manner, but it doesn't work!

    Current Code

    import seaborn as sns
    import pandas as pd
    
    Areas = r'C:\Users\Tachi\Desktop\City.xlsx'
    df = pd.read_excel(Areas)
    df.set_index('City', inplace=True)
    
    sns.relplot(x="Year", y="Number of Employees",hue="City", kind="line", data=df)
    

    Sample Data

    data = {'City': ['C1', 'C2', 'C3', 'C4', 'C5'], 
            2015: [28564, 2585, 4679, 33227, 2000], 
            2016: [83659, 4429, 35834, 1447, 3454], 
            2017: [0, 453, 40903, 46826, 646], 
            2018: [39470, 8364, 29464, 36443, 8364]}
    df = pd.DataFrame(data)
    df.set_index('City', inplace=True)
    
           2015   2016   2017   2018
    City                            
    C1    28564  83659      0  39470
    C2     2585   4429    453   8364
    C3     4679  35834  40903  29464
    C4    33227   1447  46826  36443
    C5     2000   3454    646   8364
    

    解决方案

    • Given the test dataframe, df, in the OP, the easiest way to plot the dataframe is to use pandas.DataFrame.transpose, and plot with seaborn.relplot using a wide format.
      • This automatically uses the dataframe index as the x-axis, and the column headers for hue.
      • The visualization can also be produced with sns.lineplot(data=df, marker='o') instead of using relplot.

    # transpose the dataframe
    df = df.T
    
    # display(df)
    City     C1    C2     C3     C4    C5
    2015  28564  2585   4679  33227  2000
    2016  83659  4429  35834   1447  3454
    2017      0   453  40903  46826   646
    2018  39470  8364  29464  36443  8364
    
    # plot the dataframe
    sns.relplot(data=df, kind='line', marker='o')
    

    • The index values are int dtype, so the x-axis is formatted with intermediated numbers.
      • One way to deal with this is to cast the index to a str dtype before plotting.

    # set the index of years to a str dtype
    df.index = df.index.astype(str)
    
    # plot the dataframe
    sns.relplot(data=df, kind='line', marker='o')
    

    这篇关于如何使用 seaborn.relplot 绘制宽格式数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆