在Python Plotly中填充错误栏

时间:2017-09-17 13:49:37

标签: python plotly rectangles

我在这里看到了与matplotlib有关的问题:

Filled errorbars in matplotlib (rectangles)

我想知道在Plotly这样的事情是否可行?

具体来说,我想知道您是否可以使用“Scatter”图形对象并用矩形填充误差条,如下图所示:

enter image description here

提前致谢!

1 个答案:

答案 0 :(得分:1)

没有直接的方法,但您可以在shapes中添加layout

我们假设错误是二维列表:

errors = {'x': [[0.1, 0.4],
                [0.2, 0.3],
                [0.3, 0.2],
                [0.4, 0.1],
                [0.45, 0.05]
               ],
          'y': [[0.4, 0.1],
                [0.3, 0.2],
                [0.2, 0.3],
                [0.1, 0.4],
                [0.05, 0.45]]}

现在我们为每个错误创建一个矩形shape

shapes = list()
for i in range(len(errors['x'])):
    shapes.append({'x0': points['x'][i] - errors['x'][i][0],
                   'y0': points['y'][i] - errors['y'][i][0],
                   'x1': points['x'][i] + errors['x'][i][1],
                   'y1': points['y'][i] + errors['y'][i][1],
                   'fillcolor': 'rgb(160, 0, 0)',
                   'layer': 'below',
                   'line': {'width': 0}
                  })
layout = plotly.graph_objs.Layout(shapes=shapes)

enter image description here

非对称误差线是通过'symmetric': False

创建的

完整代码

points = {'x': [0, 1, 2, 3, 4],
          'y': [0, 2, 4, 1, 3]}
errors = {'x': [[0.1, 0.4],
                [0.2, 0.3],
                [0.3, 0.2],
                [0.4, 0.1],
                [0.45, 0.05]
               ],
          'y': [[0.4, 0.1],
                [0.3, 0.2],
                [0.2, 0.3],
                [0.1, 0.4],
                [0.05, 0.45]]}

scatter = plotly.graph_objs.Scatter(x=points['x'],
                                    y=points['y'],
                                    error_x={'type': 'data',
                                             'array': [e[1] for e in errors['x']],
                                             'arrayminus': [e[0] for e in errors['x']],
                                             'symmetric': False
                                            },
                                    error_y={'type': 'data',
                                             'array': [e[1] for e in errors['y']],
                                             'arrayminus': [e[0] for e in errors['y']],
                                             'symmetric': False
                                            },                                    
                                    mode='markers')
shapes = list()
for i in range(len(errors['x'])):
    shapes.append({'x0': points['x'][i] - errors['x'][i][0],
                   'y0': points['y'][i] - errors['y'][i][0],
                   'x1': points['x'][i] + errors['x'][i][1],
                   'y1': points['y'][i] + errors['y'][i][1],
                   'fillcolor': 'rgb(160, 0, 0)',
                   'layer': 'below',
                   'line': {'width': 0}
                  })
layout = plotly.graph_objs.Layout(shapes=shapes)
data = plotly.graph_objs.Data([scatter], error_x=[e[0] for e in errors['x']])
fig = plotly.graph_objs.Figure(data=data, layout=layout)
plotly.offline.iplot(fig)