在python中,如何将标签添加到plotly boxplot?

2024-10-03 17:16:57 发布

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

我有以下代码

y = errnums
err_box = Box(
    y=y,
    name='Error Percent',
    boxmean='sd',
    marker=Marker(color='red'),
    boxpoints='all',
    jitter=0.5,
    pointpos=-2.0
)
layout = Layout(
    title='Error BoxPlot',
    height=500,
    width=500
)
fig = Figure(data=Data([err_box]), layout=layout)
plotly.image.save_as(fig, os.path.join(output_images, 'err_box.png'))

生成以下图像; img

我想做的是以下两件事

1)在y轴编号旁边添加%。(而不是传统的y轴标签上写着“Error(%)”)

2)标记所有生命点:平均值、第一个四分位数、第三个四分位数和标准偏差。理想情况下,标签应该是一个4 sig fig('.2f')号,紧挨着一行。在

另外,标准偏差是虚线,菱形代表1西格玛?2西格玛?在


Tags: 代码nameboxfigerror标签sderr
1条回答
网友
1楼 · 发布于 2024-10-03 17:16:57

对于标签,请尝试annotations.你必须计算四分位数,并让你自己来定位标签。在

简单示例:

import plotly.plotly as py
from plotly.graph_objs import *

data = Data([
    Box(
        y=[0, 1, 1, 2, 3, 5, 8, 13, 21],
        boxpoints='all',
        jitter=0.3,
        pointpos=-1.8
    )
])
layout = Layout(
    annotations=Annotations([
        Annotation(
            x=0.3,
            y=8.822,
            text='3rd Quartile',
            showarrow=False,
            font=Font(
                size=16
            )
        )
    ])
)
fig = Figure(data=data, layout=layout)
plot_url = py.plot(fig)

Simple Python boxplotSimple boxplot with Plotly and Python

我建议在绘图工作空间中添加和定位注释,然后查看生成的代码:

Adding and editing annotations in the plotly workspace

Plotly boxplot with an annotation at the 3rd quartile

Python code to generate an annotation in Plotly

菱形显示平均值,与之相差+-1标准差。在

目前无法将%添加到y轴标签。在

相关问题 更多 >