亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在同一圖中進行分組和繪制組

如何在同一圖中進行分組和繪制組

忽然笑 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條經驗 獲得超6個贊

可以使用以下代碼實現將多個圖表組合成單個圖表的簡單示例


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 關注
  • 189 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號