如何在同一图形中按groupby和打印组

2024-03-28 23:03:38 发布

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

我有绘制图表的代码:

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

Tags: dffor图表绘制pltmindaystmp
3条回答
  • 使用groupbystack数据帧要容易得多。
    • minmax可以同时聚合
  • seaborn是{}的高级API,因此我建议使用^{}在同一个图中绘制两个目的地
import pandas as pd
import numpy as np  # for sample data
import random  # for sample data
import seaborn as sns
import matplotlib.pyplot as ply

# create sample data
np.random.seed(365)
random.seed(365)
rows = 300
data = {'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 stack
dfg = 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}'))

enter image description here

使用sns.lineplot的参数ax

fig, 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')

enter image description here

下面的代码可以实现将多个图表组合成单个图表的简单示例

import matplotlib.pyplot as plt
import seaborn as sns

fig = 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()

enter image description here

相关问题 更多 >