首页 > 解决方案 > Plot.ly 在子图上绘制参考线?

问题描述

我编写了下面的函数来在图形上制作垂直参考线。

from plotly import graph_objects as go
import plotly.express as px


def add_vline(fig, x=0, text=None):
    if text is None:
        text = str(x)
    fig.update_layout(
        shapes=list(fig.layout.shapes) + [
            go.layout.Shape(
                type="line",
                x0=x,
                x1=x,

                yref="paper",
                y0=0,
                y1=1,

                line=dict(
                    color="Red",
                    width=2
                )
            )
        ],
        annotations=list(fig.layout.annotations) + [
            go.layout.Annotation(
                x=x,
                y=0.5,
                yref="paper",
                text=text
            )
        ]
    )


gapminder = px.data.gapminder()
for continent in gapminder.continent.unique():
    fig = px.histogram(gapminder, x="lifeExp", title=f'Life expectancy in {continent}')
    add_vline(fig, gapminder[gapminder.continent == continent].lifeExp.median())
    # add_figure_to_subplot() ?

我可以单独查看这些,但我想制作一个按顺序显示所有这些生成的数字的报告。如何制作这些图形对象的子图,或在子图跟踪中复制这些图?

在此处输入图像描述

标签: pythonfiguresubplotplotly.jsplotly-python

解决方案


推荐阅读