3

GOAL & CONTENT

Using python plotly.express, I have a slider that allows me to “slide” through the different dates of my data. 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. If it’s hard to understand please see the picture below (it is taken from internet but shows a slider which slides through different years) : enter image description here

So my goal is to have a slider that shows me on the graph the data corresponding to the current slider date.

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. 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. 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.

enter image description here

As an example, if I slide to date X I will have as my x-axis range A, B and C each with y-axis between 1-3$. However, if I slide to date Y, the data will change correctly but the ranges do not scale to the new data. Therefore, for date Y I 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. 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.

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

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 :)

colla
  • 717
  • 1
  • 10
  • 22

1 Answers1

2

Unfortunately this is a hard limitation of Plotly's animation system at the moment: it cannot update ranges automatically. See the "caveats" section here: https://plotly.com/python/animations/#current-animation-limitations-and-caveats

nicolaskruchten
  • 26,384
  • 8
  • 83
  • 101
  • Thank you. Is there a way to initialize the x-axis with the possible categorical variables ? – colla Aug 22 '20 at 07:31
  • for example, as my x-axis put A,B,C,E,F and force my points to adapt to the initialized x-axis ? I am using plotly express and can't find a way to initialize my x-axis ... So that it is the same x-axis no matter what the date is – colla Aug 22 '20 at 07:32
  • I formulated the question better here if you need :https://stackoverflow.com/questions/63533802/fix-x-axis-range-plotly-express – colla Aug 22 '20 at 08:29