简体   繁体   English

熊猫箱图x轴设置

[英]Pandas boxplot x-axis setting

I want to create a boxplot of data collected from four different sites over the past twenty years (ie each site will have 20y of data). 我想创建一个在过去二十年中从四个不同站点收集的数据的箱线图(即每个站点将有20y数据)。 This will produce 80 boxes on the figure. 这将在图上产生80个盒子。 To make the figure legible, I want each box offset, and have different color boxes for each site. 为了使图形清晰,我希望每个框都偏移,并为每个站点设置不同的颜色框。 This will yield a repeated series of boxes (eg boxes for site1,site2,site3,site3,site1,site2,site3,...). 这将产生一系列重复的框(例如,site1,site2,site3,site3,site1,site2,site3,...的框)。 Creating a boxplot is not a problem; 创建箱线图不是问题; offsetting the boxes does seem to be an issue. 抵消框似乎确实是一个问题。 eg 例如

import numpy as np
import pandas as pd
from pylab import *

first  = pd.DataFrame(np.random.rand(10,5),columns=np.arange(0,5))
second = pd.DataFrame(np.random.rand(10,5),columns=np.arange(5,10))

fig = figure( figsize=(9,6.5) )
ax  = fig.add_subplot(111)

box1 = first.boxplot(ax=ax,notch=False,widths=0.20,sym='',rot=-45)
setp(box1['caps'],color='r',linewidth=2)
setp(box1['boxes'],color='r',linewidth=2)
setp(box1['medians'],color='r',linewidth=2)
setp(box1['whiskers'],color='r',linewidth=2,linestyle='-')

box2 = second.boxplot(ax=ax,notch=False,widths=0.20,sym='',rot=-45)
setp(box2['caps'],color='k',linewidth=2)
setp(box2['boxes'],color='k',linewidth=2)
setp(box2['medians'],color='k',linewidth=2)
setp(box2['whiskers'],color='k',linewidth=2,linestyle='-')

Initially I hoped Pandas would index the x-axis by column name, but Pandas seems to be indexing the x-axis according to column position, which is frustrating. 最初我希望Pandas会按列名索引x轴,但Pandas似乎是根据列位置索引x轴,这令人沮丧。 Can anyone recommend a method of offsetting the boxes so they do not lay on top of one another? 任何人都可以推荐一种抵消盒子的方法,这样它们就不会叠在一起了吗?

You need to specify the positions of the bars: 您需要指定条形的位置:

box1 = first.boxplot(ax=ax,notch=False,widths=0.20,sym='',rot=-45, positions=np.arange(0.0,4.0,1.0))
box2 = second.boxplot(ax=ax,notch=False,widths=0.20,sym='',rot=-45, positions=np.arange(0.3,4.3,1.0))

Or you could move the boxs to the side you pleased (this have the extra of keeping the label centered): 或者您可以将盒子移动到您喜欢的一侧(这样可以保持标签居中):

disp = 0.15
for k in box1.keys():
    for line1,line2 in zip(box1[k],box2[k]):
        setp(line1,xdata=getp(line1,'xdata') - disp)
        setp(line2,xdata=getp(line2,'xdata') + disp)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM