[英]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:在哪里:
range
defines the minimum and maximum value of the x-axis (in this example -3 and 3), see https://plotly.com/python/reference/layout/xaxis/#layout-xaxis-range . range
定义了 x 轴的最小值和最大值(在此示例中为 -3 和 3),请参阅https://plotly.com/python/reference/layout/xaxis/#layout-xaxis-range 。autorange = False
ensures that the range is kept fixed instead of being recalculated based on the input data, see https://plotly.com/python/reference/layout/xaxis/#layout-xaxis-autorange . autorange = False
确保范围保持固定,而不是根据输入数据重新计算,请参阅https://plotly.com/python/reference/layout/xaxis/#layout-xaxis-autorange 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.