import plotly.plotly as py
from plotly.graph_objs import *
x = ['TRUE', 'TRUE', 'TRUE', 'TRUE', 'TRUE', 'TRUE',
'FALSE', 'FALSE', 'FALSE', 'FALSE', 'FALSE', 'FALSE']
y_noClassGc = [0.2, 0.2, 0.6, 1.0, 0.5, 0.4, 0.2, 0.7, 0.9, 0.1, 0.5, 0.3]
y_aggresiveOpts = [0.6, 0.7, 0.3, 0.6, 0.0, 0.5, 0.7, 0.9, 0.5, 0.8, 0.7, 0.2]
trace0 = Box(
y=y_noClassGc,
x=x,
name='noClassGc',
marker=Marker(
color='#3D9970'
)
)
trace1 = Box(
y=y_aggresiveOpts,
x=x,
name='aggresiveOpts',
marker=Marker(
color='#FF4136'
)
)
data = Data([trace0, trace1])
layout = Layout(
yaxis=YAxis(
title='confidence',
zeroline=False
),
boxmode='group'
)
fig = Figure(data=data, layout=layout)
py.iplot(fig, filename='box-grouped')
Above, the two traces share the same 'x'
coordinates. At a given 'x'
coordinate, there are as many box plot as there are traces.
To plot the desired result, instead of sending traces delimited by category ('noClassGc'
and 'aggresiveOpts'
), send traces delimited by 'TRUE'
/ 'FALSE'
values.
So, generate two new lists of 'y'
sample points, one for 'TRUE'
and one for 'FALSE'
:
y_true = []
y_false = []
for xi, yi_noClassGc, yi_aggresiveOpts in zip(x, y_noClassGc, y_aggresiveOpts):
if xi == 'TRUE':
y_true.append(yi_noClassGc)
y_true.append(yi_aggresiveOpts)
else:
y_false.append(yi_noClassGc)
y_false.append(yi_aggresiveOpts)
zip(y_true, y_false)
[(0.2, 0.2), (0.6, 0.7), (0.2, 0.7), (0.7, 0.9), (0.6, 0.9), (0.3, 0.5), (1.0, 0.1), (0.6, 0.8), (0.5, 0.5), (0.0, 0.7), (0.4, 0.3), (0.5, 0.2)]
Generate the 'x'
coordinates corresponding to the two categories:
x2 = ['noClassGc'] * (len(y_noClassGc) / 2) + ['aggresiveOpts'] * (len(y_aggresiveOpts) / 2)
x2
['noClassGc', 'noClassGc', 'noClassGc', 'noClassGc', 'noClassGc', 'noClassGc', 'aggresiveOpts', 'aggresiveOpts', 'aggresiveOpts', 'aggresiveOpts', 'aggresiveOpts', 'aggresiveOpts']
trace0 = Box(
y=y_true,
x=x2,
name='TRUE',
marker=Marker(
color='#3D9970'
)
)
trace1 = Box(
y=y_false,
x=x2,
name='FALSE',
marker=Marker(
color='#FF4136'
)
)
data = Data([trace0, trace1])
layout = Layout(
yaxis=YAxis(
title='confidence',
zeroline=False
),
boxmode='group'
)
fig = Figure(data=data, layout=layout)
py.iplot(fig, filename='box-grouped2')