本文介绍了带有颜色的Python并排matplotlib boxplots的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遵循了
解决方案
使用预制的
I followed the examples on this link on how to create a boxplot with colors. I have been trying different ways to separate these boxplots on two different positions instead of both of them overlapping but to no avail. If I specify different positions for both of them, they stay on the bp2 position. How do I put these two boxplots side by side?
import matplotlib.pyplot as plt
def color_boxplot(data, color):
bp = boxplot(data, patch_artist=True, showmeans=True)
for item in ['boxes', 'whiskers', 'fliers', 'medians', 'caps']:
plt.setp(bp[item], color=color)
data1 = [1, 2, 3, 4, 5]
data2 = [4, 5, 6, 7, 8]
fig, ax = plt.subplots()
bp1 = color_boxplot(data1, 'green')
bp2 = color_boxplot(data2, 'red')
plt.show()
EDIT: Added sample data.
解决方案
What about using a pre-made boxplot from seaborn?
import seaborn as sns
sns.boxplot(data=[data1, data2])
If you want to choose the colors manually you can use the xkcd color list:
sns.boxplot(
data=[data1, data2],
palette=[sns.xkcd_rgb["pale red"], sns.xkcd_rgb["medium green"]],
showmeans=True,
)
这篇关于带有颜色的Python并排matplotlib boxplots的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!