2 回答
TA贡献1848条经验 获得超6个赞
映射seaborn.boxplot
到seaborn.Facetgrid
:
A
Facetgrid
是用于绘制条件关系的多图网格。可以使用参数将标题
WEEKDAY
放置在右边距,但这也需要更多的垂直空间。FacetGrid
margin_titles=True
import pandas as pd
import calendar
import seaborn as sns
# given your data in dataframe df, read from a csv
df = pd.read_csv('test.csv', index_col=0)
# create the FacetGrid with boxplot
g = sns.FacetGrid(data=df, col='COMPANY', row='WEEKDAY', height=1.5, aspect=4, margin_titles=True, row_order=calendar.day_name)
g.map(sns.boxplot, 'DESTINATION', 'COST', order=sorted(df.DESTINATION.unique()))
进一步定制
归因于在seaborn FacetGrid图中编辑正确的ylabel
import pandas as pd
import calendar
import seaborn as sns
# given your data in dataframe df, read from a csv
df = pd.read_csv('test.csv', index_col=0)
# create the FacetGrid with boxplot
g = sns.FacetGrid(data=df, col='COMPANY', row='WEEKDAY', height=1.5, aspect=4, margin_titles=True, row_order=calendar.day_name)
g.map(sns.boxplot, 'DESTINATION', 'COST', order=sorted(df.DESTINATION.unique()))
TA贡献1802条经验 获得超4个赞
以下方法创建一个没有间距的 7x2 子图网格,并用于sns.boxplot每个子图内的箱线图:
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
# df = pd.read_csv(...)
fig, axes = plt.subplots(ncols=2, nrows=7, sharex=True, sharey=True, figsize=(10, 16),
gridspec_kw={'wspace': 0, 'hspace': 0})
companies = ['GOL', 'TAM']
for comp_index, company in enumerate(companies):
axes[0, comp_index].set_title(company)
for weekd_index, weekd in enumerate(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']):
ax = axes[weekd_index, comp_index]
sns.boxplot(data=df[(df['WEEKDAY'] == weekd) & (df['COMPANY'] == company)],
x='DESTINATION', y='COST', color='skyblue', ax=ax)
if comp_index != 0:
ax.set_ylabel('')
if comp_index == len(companies) - 1:
ax.text(1.02, 0.5, weekd, ha='left', va='center', rotation=90, transform=ax.transAxes)
plt.tight_layout()
plt.show()
添加回答
举报