3 回答
![?](http://img1.sycdn.imooc.com/533e4c0500010c7602000200-100-100.jpg)
TA贡献1844条经验 获得超8个赞
groupby
和stack
数据框 要容易得多。两者
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}'))
![?](http://img1.sycdn.imooc.com/5458463b0001358f02200220-100-100.jpg)
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()
![?](http://img1.sycdn.imooc.com/54584cde0001d19202200220-100-100.jpg)
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')
添加回答
举报