我下面的代码创建了两个轴图。该图显示了三个类别变量(x轴,色相和图形轴)和一个数值(y轴,两个图形轴ax0
和ax1
共享的箱形图。
# modules used
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
# data generation
df = pd.DataFrame([[np.random.choice(['PFOS','PFOA','PFHxS']),
np.random.choice(region_order),
np.random.choice([True, False]),
v] for v in np.random.normal(10,5,200)],
columns=['c','r','u','value'])
dfu = df.loc[df['u']==False,:] # subset of dataframe used for second axis
# required order of categorical variables
region_order = [*reversed(['Arctic Sea', 'North Atlantic'])]
compound_order = ['PFOS', 'PFHxS', 'PFOA']
# figure with two axes
f, (ax0,ax1) = plt.subplots(nrows=1, ncols=2, figsize=(12, 8), sharey=True)
sns.boxplot(data=df, x='value', y="r",
hue='c', palette="PuOr", ax=ax0,
hue_order=compound_order, order=region_order,
fliersize=0.5, linewidth=0.75)
sns.boxplot(data=dfu, x='value', y="r",
hue='c', palette="PuOr", ax=ax1,
hue_order=compound_order, order=region_order,
fliersize=0.5, linewidth=.75)
plt.show()
第一个图形轴使用完整的数据帧,而第二个轴仅使用一个子集。
如您所见,两个轴之间的色调顺序不一致。化合物(c
)的色相图例在两个轴上都相同,因此第二个轴出于某种原因使色相顺序相反。
如何使两个轴保持相同的色相顺序?