简体   繁体   English

Plotly 子图中的不同箱线图系列痕迹

[英]Different Box Plot Series Traces Within Plotly Subplots

I am trying to create a series of sub plots each containing box plots using Plotly.我正在尝试使用 Plotly 创建一系列子图,每个子图都包含箱形图。 At the moment I have managed to create the following plot;目前我已经设法创建了以下情节;

阴谋 . .

Unfortunately, this is not exactly what I want.不幸的是,这并不是我想要的。 I would like to create a subplot for S1 and S2 rather than A1 and A2 (such that each sub plot will have a green box plot and a red box plot).我想为 S1 和 S2 而不是 A1 和 A2 创建一个子图(这样每个子图都会有一个绿色箱形图和一个红色箱形图)。

Is this possible?这可能吗?

This is my current code.这是我当前的代码。

x = ['S1', 'S1', 'S1', 'S1', 'S1', 'S1',
     'S2', 'S2', 'S2', 'S2', 'S2', 'S2']

trace0 = go.Box(
    y=[1.935, 2.59, 2.97, 2.97, 3.28, 4.315, 1.935, 2.59, 2.97, 2.97, 3.28, 4.315],
    x=x,
    name='A1',
    marker=dict(
        color='#3D9970'
    )
)
trace1 = go.Box(
    y=[0.6, 0.7, 0.3, 0.6, 0.0, 0.5, 0.7, 0.9, 0.5, 0.8, 0.7, 0.2],
    x=x,
    name='A2',
    marker=dict(
        color='#FF4136'
    )
)
fig = tools.make_subplots(rows=1, cols=2)
fig.append_trace(trace0, 1, 1)
fig.append_trace(trace1, 1, 2)
py.iplot(fig)

I know it's late for the answer however for anyone out there that currently need this the issue was that if you want to compare S1 and S2 you should use a different X for each graph.我知道答案已经晚了,但是对于目前需要这个的任何人来说,问题是如果你想比较 S1 和 S2,你应该为每个图表使用不同的 X。

`from plotly.subplots import make_subplots
import plotly.graph_objects as go

x = ['S1', 'S1', 'S1', 'S1', 'S1', 'S1',
     'S2', 'S2', 'S2', 'S2', 'S2', 'S2']

trace0 = go.Box(
    y=[1.935, 2.59, 2.97, 2.97, 3.28, 4.315],
    x=[i+'_A1' for i in x[:6]],
    name='S1_A1'
)

trace1 = go.Box(
    y=[0.6, 0.7, 0.3, 0.6, 0.0, 0.5],
    x=[i+'_A2' for i in x[:6]],
    name='S1_A2'
)


trace2 = go.Box(
    y=[1.935, 2.59, 2.97, 2.97, 3.28, 4.315],
    x=[i+'_A1' for i in x[6:]],
    name='S2_A1'
)


trace3 = go.Box(
    y=[0.7, 0.9, 0.5, 0.8, 0.7, 0.2],
    x=[i+'_A2' for i in x[6:]],
    name='S2_A2'
)

fig = make_subplots(rows=1, cols=2)

### First subplot ###
# S1_A1
fig.append_trace(trace0, row = 1, col = 1)
# S2_A2
fig.append_trace(trace1, row = 1, col = 1)
#### Second subplot ###
# S1_A1
fig.append_trace(trace2, row = 1, col = 2)
# S2_A2
fig.append_trace(trace3, row = 1, col = 2)

fig.show()´

结果图片

Removed the colours that you got for plotly to use different ones删除了您为使用不同的颜色而获得的颜色

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

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