简体   繁体   English

Plotly.express slider 不是自动缩放范围

[英]Plotly.express slider not autoscaling ranges

GOAL & CONTENT目标与内容

Using python plotly.express, I have a slider that allows me to “slide” through the different dates of my data.使用 python plotly.express,我有一个 slider,它允许我“滑动”我的数据的不同日期。 Therefore, I can have one graph for the data with the same date and then slide to another date and have another graph which contains the new data corresponding to the slider date.因此,我可以为具有相同日期的数据创建一个图表,然后滑动到另一个日期并获得另一个包含与 slider 日期对应的新数据的图表。 If it's hard to understand please see the picture below (it is taken from inte.net but shows a slider which slides through different years):如果难以理解请看下图(取自inte.net但显示的是一个slider在不同年份间滑动): 在此处输入图像描述

So my goal is to have a slider that shows me on the graph the data corresponding to the current slider date.所以我的目标是有一个 slider,它在图表上显示与当前 slider 日期对应的数据。

PROBLEM问题

My problem is that when I use the slider, it does show the correct data points but the x-axis and the y-axis do not change ranges.我的问题是,当我使用 slider 时,它确实显示了正确的数据点,但 x 轴和 y 轴不改变范围。 In short, it does not auto scale the ranges correctly when sliding .简而言之,它不会在滑动时正确地自动缩放范围

As my x-axis, I have categorical variables (names) and y-axis is the price.作为我的 x 轴,我有分类变量(名称),y 轴是价格。 So, when I slide to another date, I do have the points changing but the categories stay as the initial categories.所以,当我滑到另一个日期时,我确实改变了点,但类别保持为初始类别。 It does the same thing for y-axis.它对 y 轴做同样的事情。

在此处输入图像描述

As an example, if I slide to date XI will have as my x-axis range A, B and C each with y-axis between 1-3$.例如,如果我滑动到日期 XI,我的 x 轴范围为 A、B 和 C,每个 y 轴在 1-3$ 之间。 However, if I slide to date Y, the data will change correctly but the ranges do not scale to the new data.但是,如果我滑动到日期 Y,数据将正确更改,但范围不会缩放到新数据。 Therefore, for date YI will not see any points as the category will stay as the initiale ones A,B,C instead of showing C,E,F on x-axis.因此,对于日期 YI 将看不到任何点,因为类别将保留为初始点 A、B、C,而不是在 x 轴上显示 C、E、F。 Moreover, for the y-axis, it will still only show from range 1 to 3$ instead of scaling to the new range 3-130 for example.此外,对于 y 轴,它仍然只会显示 1 到 3$ 的范围,而不是缩放到新的范围 3-130,例如。

WHAT I TRIED我试过什么

I seem to have the same problem as this person but there was no answer.我似乎和这个人有同样的问题,但没有答案。 I tried what she tried as well: https://community.plotly.com/t/auto-scale-plotly-express/33364 I wanted to try this aswell but I do not know where to place it in my code correclty: https://community.plotly.com/t/solved-how-to-progamatically-autoscale-plot/3278我也尝试了她尝试过的方法: https:https://community.plotly.com/t/auto-scale-plotly-express/33364我也想尝试这个,但我不知道把它放在我的代码中的正确位置: https //community.plotly.com/t/solved-how-to-progamatically-autoscale-plot/3278

MY CODE我的代码

This is my code for now:这是我现在的代码:

        data.columns = ['price', 'category', 'date']
        data = data.sort_values(by=['date', 'price'])
        fig = px.scatter(data, x = "category", y = "price", animation_frame="date")
        fig.update_layout(
            yaxis_title="Price (€)",
        )
        fig['layout']['updatemenus'][0]['pad']['t'] = 180
        fig['layout']['sliders'][0]['pad']['t'] = 200
        fig.write_html("/home/**/Desktop/1.html", auto_play=True)

Ihope I was clear enough.我希望我足够清楚。 Please let me know if you need any extra information.如果您需要任何额外信息,请告诉我。 Any ideas or tips is welcome:)欢迎任何想法或提示:)

Unfortunately this is a hard limitation of Plotly's animation system at the moment: it cannot update ranges automatically.不幸的是,这是目前 Plotly 的 animation 系统的硬性限制:它无法自动更新范围。 See the "caveats" section here: https://plotly.com/python/animations/#current-animation-limitations-and-caveats请参阅此处的“注意事项”部分: https://plotly.com/python/animations/#current-animation-limitations-and-caveats

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

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