用Python绘图填充错误线

2024-04-20 09:40:10 发布

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

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

Filled errorbars in matplotlib (rectangles)

我想知道这样的事情是否有可能?在

具体地说,我想知道您是否可以使用“散点”图形对象并用矩形填充错误栏,如下图所示:

enter image description here

提前谢谢!在


Tags: 对象in图形matplotlib错误事情矩形散点
1条回答
网友
1楼 · 发布于 2024-04-20 09:40:10

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

假设错误是二维列表:

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

^{pr2}$

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)

相关问题 更多 >