gpt4 book ai didi

python - matplotlib:分组箱线图

转载 作者:IT老高 更新时间:2023-10-28 20:26:37 32 4
gpt4 key购买 nike

有没有办法在 matplotlib 中对箱线图进行分组?

假设我们有三个组“A”、“B”和“C”,我们希望为每个组创建一个“apples”和“oranges”的箱线图。如果无法直接进行分组,我们可以创建所有六个组合并将它们并排线性放置。可视化分组的最简单方法是什么?我试图避免将刻度标签设置为“A + apples”之类的东西,因为我的场景涉及的名称比“A”长得多。

最佳答案

如何使用颜色来区分“苹果”和“橙子”并使用间距来区分“A”、“B”和“C”?

类似这样的:

from pylab import plot, show, savefig, xlim, figure, \
hold, ylim, legend, boxplot, setp, axes

# function for setting the colors of the box plots pairs
def setBoxColors(bp):
setp(bp['boxes'][0], color='blue')
setp(bp['caps'][0], color='blue')
setp(bp['caps'][1], color='blue')
setp(bp['whiskers'][0], color='blue')
setp(bp['whiskers'][1], color='blue')
setp(bp['fliers'][0], color='blue')
setp(bp['fliers'][1], color='blue')
setp(bp['medians'][0], color='blue')

setp(bp['boxes'][1], color='red')
setp(bp['caps'][2], color='red')
setp(bp['caps'][3], color='red')
setp(bp['whiskers'][2], color='red')
setp(bp['whiskers'][3], color='red')
setp(bp['fliers'][2], color='red')
setp(bp['fliers'][3], color='red')
setp(bp['medians'][1], color='red')

# Some fake data to plot
A= [[1, 2, 5,], [7, 2]]
B = [[5, 7, 2, 2, 5], [7, 2, 5]]
C = [[3,2,5,7], [6, 7, 3]]

fig = figure()
ax = axes()
hold(True)

# first boxplot pair
bp = boxplot(A, positions = [1, 2], widths = 0.6)
setBoxColors(bp)

# second boxplot pair
bp = boxplot(B, positions = [4, 5], widths = 0.6)
setBoxColors(bp)

# thrid boxplot pair
bp = boxplot(C, positions = [7, 8], widths = 0.6)
setBoxColors(bp)

# set axes limits and labels
xlim(0,9)
ylim(0,9)
ax.set_xticklabels(['A', 'B', 'C'])
ax.set_xticks([1.5, 4.5, 7.5])

# draw temporary red and blue lines and use them to create a legend
hB, = plot([1,1],'b-')
hR, = plot([1,1],'r-')
legend((hB, hR),('Apples', 'Oranges'))
hB.set_visible(False)
hR.set_visible(False)

savefig('boxcompare.png')
show()

grouped box plot

关于python - matplotlib:分组箱线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16592222/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com