【问题标题】:Swarmplot from pandas with comma separated categorical values来自带有逗号分隔分类值的熊猫的 Swarmplot
【发布时间】:2021-11-08 12:15:27
【问题描述】:

我正在使用一个数据集,该数据集在其列中包含逗号分隔的分类响应,并试图绘制一个类似于下面的散点图。我认为正确的方法可能是使用 seaborn swarmplot,例如:ax = sns.swarmplot(x="id", y="family", data=dftest) 但结果与我想要的相差甚远。我正在寻找一些关于如何重建类似情节的指示?

样本数据:

id family city nation world
1 next week, next few years next week, next few years lifetime children's lifetime
2 next few years, lifetime children's lifetime next few years next week, next few years, children's lifetime
3 next week, next few years lifetime children's lifetime children's lifetime
4 next week, next few years next few years next week, next few years, lifetime, next week, next few years, children's lifetime
5 next few years, lifetime children's lifetime children's lifetime lifetime
figure source: Meadows, D., Meadows, D., Randers, J. and Behrens, W., 1972. The Limits to growth. A report for the club of Rome's project on the predicament of mankind. 1st ed. New York: Universe Books.

【问题讨论】:

    标签: python pandas matplotlib seaborn


    【解决方案1】:

    据我所知,图像中呈现的预期输出很难用 python 的图形库实现,所以我试图模仿 seaborn 的 swarmplot 的预期输出。我希望这篇文章能从经验丰​​富的工程师那里得到一些很好的答案。

    import pandas as pd
    import numpy as np
    import io
    
    data = '''
    id family city nation world
    1 "next week,next few years" "next week,next few years" lifetime "children's lifetime"
    2 "next few years,lifetime" "children's lifetime" "next few years" "next week,next few years,children's lifetime"
    3 "next week,next few years" lifetime "children's lifetime" "children's lifetime"
    4 "next week,next few years" "next few years" "next week,next few years,lifetime," "next week,next few years,children's lifetime"
    5 "next few years,lifetime" "children's lifetime" "children's lifetime" lifetime
    '''
    df = pd.read_csv(io.StringIO(data), delim_whitespace=True)
    dfs = pd.concat([
        df['family'].str.split(',', expand=True).rename(columns={0:'family',1:'family'}),
        df['city'].str.split(',', expand=True).rename(columns={0:'city',1:'city'}),
        df['nation'].str.split(',', expand=True).rename(columns={0:'nation',1:'nation',2:'nation'}),
        df['world'].str.split(',', expand=True).rename(columns={0:'world',1:'world',2:'world'})
    ], axis=1)
    
    dfs = dfs.unstack().to_frame(name='TIME').reset_index().rename(columns={'level_0':'SPACE'})
    dfs['TIME'].replace('', np.NaN, inplace=True)
    dfs.dropna(subset=['TIME'], inplace=True)
    
    import seaborn as sns
    
    ax = sns.swarmplot(x="TIME", y="SPACE", data=dfs, size=15)
    ax.grid(which='minor', axis='both')
    print(ax.get_ylim())
    ax.set_ylim(-0.5, 3.5)
    
    for y_pos in [0.5, 1.5, 2.5]:
        ax.axhline(y=y_pos, color='k', lw=0.8)
    
    for x_pos in [0.5, 1.5, 2.5]:
        ax.axvline(x=x_pos, color='k', lw=0.8)
    

    【讨论】:

      猜你喜欢
      • 2018-10-29
      • 1970-01-01
      • 1970-01-01
      • 2021-10-13
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多