如何在同一图中进行分组和绘制组

我有用于绘制图表的代码:


destinations = ['JPA', 'FOR']


for destiny in destinations:


    df_tmp = df[(df.DESTINY == destiny)]

    df_tmp['max'] = df_tmp.groupby('DAYS_UNTIL_DEPARTURE')['COST'].transform('max')

    df_tmp['min'] = df_tmp.groupby('DAYS_UNTIL_DEPARTURE')['COST'].transform('min')


    plt.figure(figsize=(10,2))

    sns.lineplot(x="DAYS_UNTIL_DEPARTURE", y="min", data=df_tmp, ci=None, palette="muted", label='min')

    sns.lineplot(x="DAYS_UNTIL_DEPARTURE", y="max", data=df_tmp, ci=None, palette="muted", label='max')

    plt.title(destiny , fontweight="bold", fontsize=16, pad=20)

    plt.ylabel('Cost')

    plt.show()

    

该代码运行得很好。


我想知道如何在同一个图上绘制多个图表?换句话说,两张图表合二为一。


我一直在尝试子图,但无法获得预期的结果。


谢谢,谢谢。


这是我的数据示例:


DAYS_UNTIL_DEPARTURE,DESTINY,COST

10,JPA,100

9,JPA,90

8,JPA,85

7,JPA,86

6,JPA,87

5,JPA,71

4,JPA,90

3,JPA,77

2,JPA,88

1,JPA,87

0,JPA,74

10,FOR,99

9,FOR,90

8,FOR,96

7,FOR,79

6,FOR,84

5,FOR,74

4,FOR,85

3,FOR,74

2,FOR,88

1,FOR,100

0,FOR,87


忽然笑
浏览 116回答 3
3回答

婷婷同学_

groupby和stack数据框 要容易得多。两者min, 和max可以同时聚合。seaborn是 的高级API选项matplotlib,因此我建议使用seaborn.relplot, 在同一个图中绘制两个目的地import pandas as pdimport numpy as np  # for sample dataimport random  # for sample dataimport seaborn as snsimport matplotlib.pyplot as ply# create sample datanp.random.seed(365)random.seed(365)rows = 300data = {'days': np.random.randint(10, size=(rows)), 'dest': [random.choice(['JPA', 'FOR']) for _ in range(rows)], 'cost': np.random.randint(70, 120, size=(rows))}df = pd.DataFrame(data)# groupby, aggregate, and stackdfg = df.groupby(['dest', 'days'])['cost'].agg(['min', 'max']).stack().reset_index().rename(columns={'level_2': 'range', 0: 'vals'})# plot with seaborn relplot(sns.relplot(x='days', y='vals', hue='range', col='dest', data=dfg, kind='line') .set_axis_labels('Day Until Departure', 'Cost') .set_titles('Destination: {col_name}'))

ibeautiful

可以使用以下代码实现将多个图表组合成单个图表的简单示例import matplotlib.pyplot as pltimport seaborn as snsfig = plt.figure(figsize=(10,2))ax = fig.add_subplot(111)destinations = ['JPA', 'FOR']for destiny in destinations:    df_tmp = df[(df.DESTINY == destiny)]    df_tmp['max'] = df_tmp.groupby('DAYS_UNTIL_DEPARTURE')['COST'].transform('max')    df_tmp['min'] = df_tmp.groupby('DAYS_UNTIL_DEPARTURE')['COST'].transform('min')    sns.lineplot(x="DAYS_UNTIL_DEPARTURE", y="min", data=df_tmp, ci=None, palette="muted", label='min')    sns.lineplot(x="DAYS_UNTIL_DEPARTURE", y="max", data=df_tmp, ci=None, palette="muted", label='max')    plt.title('Destiny', fontweight="bold", fontsize=16, pad=20)plt.ylabel('Cost')plt.show()

眼眸繁星

使用ax参数sns.lineplotfig, ax = plt.subplots(1,2)destinations = ['JPA', 'FOR']for i, destiny in enumerate(destinations):    df_tmp = df[(df.DESTINY == destiny)]    df_tmp['max'] = df_tmp.groupby('DAYS_UNTIL_DEPARTURE')['COST'].transform('max')    df_tmp['min'] = df_tmp.groupby('DAYS_UNTIL_DEPARTURE')['COST'].transform('min')    sns.lineplot(x="DAYS_UNTIL_DEPARTURE", y="min", data=df_tmp, ci=None, palette="muted", label='min', ax=ax[i])    sns.lineplot(x="DAYS_UNTIL_DEPARTURE", y="max", data=df_tmp, ci=None, palette="muted", label='max', ax=ax[i])    ax[i].set_title(destiny , fontweight="bold", fontsize=16, pad=20)    plt.ylabel('Cost')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python