慕勒3428872
映射seaborn.boxplot到seaborn.Facetgrid:AFacetgrid是用于绘制条件关系的多图网格。可以使用参数将标题WEEKDAY放置在右边距,但这也需要更多的垂直空间。FacetGridmargin_titles=Trueimport pandas as pdimport calendarimport seaborn as sns# given your data in dataframe df, read from a csvdf = pd.read_csv('test.csv', index_col=0)# create the FacetGrid with boxplotg = 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图中编辑正确的ylabelimport pandas as pdimport calendarimport seaborn as sns# given your data in dataframe df, read from a csvdf = pd.read_csv('test.csv', index_col=0)# create the FacetGrid with boxplotg = 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()))
慕虎7371278
以下方法创建一个没有间距的 7x2 子图网格,并用于sns.boxplot每个子图内的箱线图:import matplotlib.pyplot as pltimport seaborn as snsimport pandas as pdimport 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()