微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当使用滑块在Plotly中显示顺序直方图时,强制x轴范围保持恒定

如何解决当使用滑块在Plotly中显示顺序直方图时,强制x轴范围保持恒定

这是一个相对简单的问题,但是我找不到足够的文档来完成自己想要的事情。我希望当滑块每天移动时,x轴保持不变。

以下是图形和滑块供参考。请注意,x轴的范围是appx。 -3.25至3:

enter image description here

,然后如果我移动滑块,则x轴的范围大约为。 -3至4。

enter image description here

如何强制最小和最大x轴值保持恒定?

以下是示例代码

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)

解决方法

尝试如下更新布局字典:

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

其中:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。