分组箱线图替代颜色、标签和紧密间距问题
Grouped Boxplot Alternate Color, Label and Tight Spacing Issue
使用以下数据(用于快速生成 csv 文件,数据集 link):
和以下代码:
df_all[['all', 'taste','fruit']].melt('fruit').boxplot(column=['value'], by=['fruit', 'variable'], rot=45, fontsize=11, patch_artist=True,
color=dict(boxes='#053061', whiskers='#67001F', medians='#A7D0E4', caps='#67001F'),
boxprops=dict(linestyle='-', linewidth=0.8),
flierprops=dict(linestyle='-', linewidth=0, marker='+'),
medianprops=dict(linestyle='-', linewidth=0.8),
whiskerprops=dict(linestyle='-', linewidth=0.8),
capprops=dict(linestyle='-', linewidth=0.8),
showfliers=True,
grid=False,)
我得到了这个可憎的东西:
我要实现
- 箱线图上的替代颜色(第一个是蓝色,然后是红色,每个颜色交替为红色)
- 每个箱线图下方的标签
- 传说
类似于:
可能无法获得准确的输出,但非常感谢您的帮助!
如果您愿意使用 seaborn,可以使用 sns.boxplot()
并将融化的 variable
设置为 hue
:
import seaborn as sns
sns.boxplot(
data=df[['all', 'taste', 'fruit']].melt('fruit'),
x='fruit',
y='value',
hue='variable',
width=0.5,
order=['apple', 'mango', 'orange'],
)
使用以下数据(用于快速生成 csv 文件,数据集 link):
和以下代码:
df_all[['all', 'taste','fruit']].melt('fruit').boxplot(column=['value'], by=['fruit', 'variable'], rot=45, fontsize=11, patch_artist=True,
color=dict(boxes='#053061', whiskers='#67001F', medians='#A7D0E4', caps='#67001F'),
boxprops=dict(linestyle='-', linewidth=0.8),
flierprops=dict(linestyle='-', linewidth=0, marker='+'),
medianprops=dict(linestyle='-', linewidth=0.8),
whiskerprops=dict(linestyle='-', linewidth=0.8),
capprops=dict(linestyle='-', linewidth=0.8),
showfliers=True,
grid=False,)
我得到了这个可憎的东西:
我要实现
- 箱线图上的替代颜色(第一个是蓝色,然后是红色,每个颜色交替为红色)
- 每个箱线图下方的标签
- 传说
类似于:
可能无法获得准确的输出,但非常感谢您的帮助!
如果您愿意使用 seaborn,可以使用 sns.boxplot()
并将融化的 variable
设置为 hue
:
import seaborn as sns
sns.boxplot(
data=df[['all', 'taste', 'fruit']].melt('fruit'),
x='fruit',
y='value',
hue='variable',
width=0.5,
order=['apple', 'mango', 'orange'],
)