改变matplotlib盒形图样式

2024-06-05 23:56:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个pandas数据帧,我正在尝试使用Matplotlib创建boxplot。我可以得到boxplot,但是看起来boxplot的样式(boxplot组件的颜色)不起作用。我正在学习绘制图表,但有点搞不清所有元素是如何组合在一起的。如有任何指导,我们将不胜感激。在

import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
fig, ax = plt.subplots(figsize=(20,6))
ax.set_title('Height Distribution')
ax.set_xlabel('PANEL')
ax.set_ylabel('Height (um)')
# Define styling for each boxplot component
medianprops = {'color': 'magenta', 'linewidth': 2}
boxprops = {'color': 'black', 'linestyle': '-'}
whiskerprops = {'color': 'red', 'linestyle': '-'}
capprops = {'color': 'green', 'linestyle': '-'}
flierprops = {'color': 'yellow', 'marker': 'x'}
concatenated_df.boxplot(ax=ax, column='HEIGHT',by=['Label'],rot=90,fontsize=15,medianprops=medianprops,
           boxprops=boxprops,
           whiskerprops=whiskerprops,
           capprops=capprops,
           flierprops=flierprops)

# Save figure
fig.savefig("filename.jpg")

Boxplot


Tags: importmatplotlibasfigpltaxcolorset