更新绘图中的特定子图轴

Mic*_*ael 7 python python-3.x plotly plotly-python

设置:

  • 我正在尝试绘制一个子图plotly库绘制子图,但无法弄清楚如何引用特定子图的轴来更改其名称(或其他属性)。
  • Code 1我展示了一个简单的示例,其中我添加了两个图,一个在另一个的上方,并使用plotly.subplots.make_subplots.

代码1

import numpy as np
from plotly.subplots import make_subplots
from math import exp

fig = make_subplots(2, 1)

x = np.linspace(0, 10, 1000)
y = np.array(list(map(lambda x: 1 / (1 + exp(-0.1 * x + 5)), x)))
fig.add_trace(
    go.Scatter(
        x=x,
        y=y,
        name=f'\N{Greek Small Letter Sigma}(x)',
        showlegend=True
    ),
    row=1,
    col=1
)

x = np.where(np.random.randint(0, 2, 100)==1)[0]
fig.add_trace(
    go.Scatter(
        x=x,
        y=np.zeros_like(x),
        name=f'Plot 2',
        mode='markers', 
        marker=dict(
                symbol='circle-open',
                color='green',
                size=5
            ),
        showlegend=True
    ),
    row=2,
    col=1
)
fig.show()
Run Code Online (Sandbox Code Playgroud)

我尝试过的

我尝试fig.update_xaxes()在每次添加跟踪后使用,但它会弄乱绘图并且不会产生所需的输出,如图所示Code 2

代码2:

import numpy as np
from plotly.subplots import make_subplots
from math import exp

fig = make_subplots(2, 1)

x = np.linspace(0, 10, 1000)
y = np.array(list(map(lambda x: 1 / (1 + exp(-0.1 * x + 5)), x)))
fig.add_trace(
    go.Scatter(
        x=x,
        y=y,
        name=f'\N{Greek Small Letter Sigma}(x)',
        showlegend=True
    ),
    row=1,
    col=1
)
fig.update_xaxes(title_text='x')
x = np.where(np.random.randint(0, 2, 100)==1)[0]
fig.add_trace(
    go.Scatter(
        x=x,
        y=np.zeros_like(x),
        name=f'Plot 2',
        mode='markers', 
        marker=dict(
                symbol='circle-open',
                color='green',
                size=5
            ),
        showlegend=True
    ),
    row=2,
    col=1
)
fig.update_xaxes(title_text='active users')
fig.show()
Run Code Online (Sandbox Code Playgroud)

结果是(注意active users打印在顶部):

在此输入图像描述

我的问题:

  1. 如何将顶部图的 x 轴分配给 label x,并将active userslabel 分配给底部图的 x 轴?
  2. 一般来说 - 我如何访问单个子图的属性?

Mic*_*ael 15

在这个答案的帮助下,我能够通过引用xaxis位置上的图row=1和位置上的图col=1来解决它。完整的解决方案位于.xaxis1row=2col=1Code 1

代码1:

import numpy as np
from plotly.subplots import make_subplots
from math import exp

fig = make_subplots(2, 1)

x = np.linspace(0, 10, 1000)
y = np.array(list(map(lambda x: 1 / (1 + exp(-0.1 * x + 5)), x)))
fig.add_trace(
    go.Scatter(
        x=x,
        y=y,
        name=f'\N{Greek Small Letter Sigma}(x)',
        showlegend=True
    ),
    row=1,
    col=1
)
fig['layout']['xaxis'].update(title_text='x')

x = np.where(np.random.randint(0, 2, 100)==1)[0]
fig.add_trace(
    go.Scatter(
        x=x,
        y=np.zeros_like(x),
        name=f'Plot 2',
        mode='markers', 
        marker=dict(
                symbol='circle-open',
                color='green',
                size=5
            ),
        showlegend=True
    ),
    row=2,
    col=1
)
fig['layout']['xaxis2'].update(title_text='active users')

fig.show()
Run Code Online (Sandbox Code Playgroud)

干杯。

  • `fig.layout` 比 `fig['layout']` 稍微安全一些。 (2认同)