为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

忽然笑 2023-09-26 17:14:37
我有用于绘制图表的代码: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,COST10,JPA,1009,JPA,908,JPA,857,JPA,866,JPA,875,JPA,714,JPA,903,JPA,772,JPA,881,JPA,870,JPA,7410,FOR,999,FOR,908,FOR,967,FOR,796,FOR,845,FOR,744,FOR,853,FOR,742,FOR,881,FOR,1000,FOR,87
查看完整描述

3 回答

?
婷婷同学_

TA贡献1844条经验 获得超8个赞

  • groupbystack数据框 要容易得多。

    • 两者min, 和max可以同时聚合。

  • seaborn是 的高级API选项matplotlib,因此我建议使用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}'))

https://img1.sycdn.imooc.com//6512a150000135eb07850376.jpg

查看完整回答
反对 回复 2023-09-26
?
ibeautiful

TA贡献1993条经验 获得超5个赞

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


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

https://img1.sycdn.imooc.com//6512a160000109dc06180183.jpg

查看完整回答
反对 回复 2023-09-26
?
眼眸繁星

TA贡献1873条经验 获得超9个赞

使用ax参数sns.lineplot


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

https://img1.sycdn.imooc.com//6512a16c0001ac3406500356.jpg

查看完整回答
反对 回复 2023-09-26
  • 3 回答
  • 0 关注
  • 74 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信