简体   繁体   English

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

[英]How to groupby and plot groups in the same figure

I have that code for plotting a chart:我有用于绘制图表的代码:

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

The code works pretty well.该代码运行良好。

I would like to know how to plot the multiple charts on the same figure?我想知道如何在同一张图上绘制多个图表? In other words, two charts in one figure.换句话说,一张图中的两个图表。

I've been trying to subplot, but I wasn't enabled to get the result expected.我一直在尝试进行子图,但我无法获得预期的结果。

Thanks, thanks.谢谢,谢谢。

Here is a sample of my data:这是我的数据示例:

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

use parameter ax of sns.lineplot使用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')

在此处输入图片说明

A simple example of combining multiple charts into a single graph can be achieved with the following code可以通过以下代码实现将多个图表组合成单个图形的简单示例

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

在此处输入图片说明

  • It's much easier to groupby , and stack the dataframe. groupbystack数据帧要容易得多。
    • Both min , and max can be aggregated at the same time. minmax可以同时聚合。
  • seaborn is a high-level API for matplotlib , so I recommend usingseaborn.relplot , to plot both destinations in the same figure seabornmatplotlib的高级API ,所以我建议使用seaborn.relplot ,在同一图中绘制两个目的地
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}'))

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM