简体   繁体   English

Plotly:当滑块用于显示连续直方图时,强制 x 轴范围保持不变

[英]Plotly: Force x-axis range to remain constant when slider is used to display sequential histograms

This is relatively straightforward question but I cannot find adequate documentation to do what I want.这是一个相对简单的问题,但我找不到足够的文档来做我想做的事。 I want the x-axis to remain constant when the slider is moved from day to day.当滑块每天移动时,我希望 x 轴保持不变。

Here is the figure and slider for reference.这是供参考的图和滑块。 Note that the x-axis ranges from appx.请注意,x 轴的范围从 appx。 -3.25 to 3: -3.25 到 3:

在此处输入图片说明

and then if I move the slider the x-axis now ranges from approx.然后如果我移动滑块,x 轴现在的范围从大约。 -3 to 4. -3 到 4。

在此处输入图片说明

How do I force the min and max x-axis values to remain constant?如何强制最小和最大 x 轴值保持不变?

Here is code for the example:这是示例的代码:

import numpy as np
import plotly.express as px
from plotly.offline import plot

total_days = 3

data = list()

for day in range(total_days):
    data.append(plotly.graph_objs.Histogram(
        x=np.random.randn(500) + day * 0.5,
        name='Day {}, control'.format(day),
        visible=day < 1
    )
    )

steps = list()
for i in range(total_days):
    step = dict(
        method='restyle',
        args=['visible', [False] * total_days * 2],
        label='Day {}'.format(i)
    )
    step['args'][1][i] = True
    steps.append(step)

sliders = [dict(
    active=0,
    steps=steps
)]

layout = dict(sliders=sliders)
fig = dict(data=data, layout=layout)
plot(fig)

Try updating the layout dictionary as follows:尝试按如下方式更新布局字典:

layout = dict(sliders=sliders,
              xaxis=dict(range=[-3, 3],
                         autorange=False))

where:在哪里:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM