绘图框绘图:groupby选项?

2024-10-01 04:54:41 发布

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

我有两个布尔变量,我试着用组创建一个箱线图。每个组应该代表一个变量,它应该包含两个框线图,一个代表真,一个代表假。相反,我得到两组,一组代表真,一组代表假,每组两组对应于每个变量的方框图,如附图所示:

boxplot

我知道群是从xaxis派生的。但是我怎么能把变量名想象成组呢?我用于输出的代码:

trace3= Box(
 y=raw_matrix.TPS,
 x=raw_matrix.noClassGc,
 name='noClassGc',
 marker=Marker(
 color='#3F5D7D'
))

trace4= Box(
 y=raw_matrix.TPS,
 x=raw_matrix.aggresiveOpts,
 name='aggresiveOpts',
 marker=Marker(
 color='#0099FF'
 ))

data = Data([trace3, trace4])
layout = Layout(
 yaxis=YAxis(
 title='confidence',
 zeroline=False),
 boxmode='group',
 boxgroupgap=0.5
 )


fig = Figure(data=data, layout=layout)
plot_url = ploteczki.plot(fig, filename='Performance by categoricals parameters')

Tags: nameboxdataraw代表matrixmarkercolor
2条回答

您需要重新排列数据数组,以便两条Box记录道具有'noClassGc'和{}的'x'坐标。在

这个IPython notebook向您展示了如何这样做。在

或者,要用布尔盒图表示每个组,可以将每个记录道分配到不同的x轴。下面是一个例子:

trace0 = Box(
    y=raw_matrix_TPS,
    x=raw_matrix_noClassGc,
    name='noClassGc',
    marker=Marker(
        color='#3F5D7D'
    )
)
trace1 = Box(
    y=raw_matrix_TPS,
    x=raw_matrix_aggresiveOpts,
    name='aggresiveOpts',
    xaxis='x2',
    marker=Marker(
        color='#0099FF'
    )
)
data = Data([trace0, trace1])
layout = Layout(
    xaxis = XAxis(
        domain=[0, 0.55],
    ),
    xaxis2 = XAxis(
         domain=[0.55, 1],
    ),
    yaxis = YAxis(
         title='confidence',
         zeroline=False
    ),
    boxmode='group',
    boxgroupgap=0.5
)
fig = Figure(data=data, layout=layout)
plot_url = py.plot(fig, filename='Performance by categoricals parameters')

这是the link to the plot

要了解更多信息,您可以签出Plotly Python reference

相关问题 更多 >