python框图_python – matplotlib:组框图

如何使用颜色区分“苹果”和“橘子”和间距分开“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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值