Python 在plotly中更新特定子地块轴

Python 在plotly中更新特定子地块轴,python,python-3.x,plotly,plotly-python,Python,Python 3.x,Plotly,Plotly Python,设置: 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=

设置:

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()
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()
  • 我正尝试使用
    plotly
    库绘制子地块,但不知道如何引用特定子地块的轴来更改其名称(或其他属性)
  • code 1
    中,我展示了一个简单的示例,其中我使用
    plotly.subplot.make_subplot
    在另一个图的顶部添加了两个图
代码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()
我所尝试的

我曾尝试在每次添加跟踪后使用
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()
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()
这会导致(注意顶部打印的
活动用户
):

我的问题:

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()
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()
  • 如何将标签
    x
    分配给顶部打印x轴,将
    活动用户
    标签分配给底部打印的x轴
  • 一般来说,如何访问单个子地块的属性
  • 谢谢你的帮助。提前感谢。

    在我的帮助下,我可以通过参考
    行=1
    位置上的绘图的
    xaxis
    col=1
    行=2
    位置上的绘图的
    xaxis1
    来解决这个问题。完整的解决方案在
    代码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.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()
    
    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()
    

    干杯。

    fig.layout
    fig['layout']
    稍微安全一些。