Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

python - Why clicking on dropdown menu option resets the x-range?

I am playing with Plotly in order to learn to make interactive plots and dashboards but I have encountered a weird behaviour that I am not able to understand, regarding the functioning of the dropdown menu.

The plot is displayed correctly, along with the button. Clicking in one of options effectively changes the plot content, but it comes with a caveat, the range is reset to the default of the dataset. I simplified the code to the minimal expression, as an example.

from plotly.subplots import make_subplots

buttons =  [  dict( method='update', label='b1', visible=True, args=[{'y':[[1,2,3]],   'type':'scatter'}] ),  
              dict( method='update', label='b2', visible=True, args=[{'y':[[30,20,10]],'type':'scatter'}] )]

fig = make_subplots()
fig.add_scatter(x=[2,4,6], y=[1,2,3]) 
    .update_xaxes(range=[-10, 10])
    .update_layout(updatemenus=[{'buttons':buttons}])

Executing this piece of code returns a plot with the x-axis range in [-10,10] (correct). However, when selecting any of the dropdown options, the x-range reverts to [2,6] (incorrect), the basic range of the dataset.

I believe I am doing something wrong with the button/layout update, since I cannot change any other attribute as the color either, (I tried, for example, adding line_color : 'red' to the args list, with no effect), so I am a bit lost.

Do you have any idea what I am doing wrong?

question from:https://stackoverflow.com/questions/65516719/why-clicking-on-dropdown-menu-option-resets-the-x-range

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have just removed the plot type and now the axis is not resetting, i have frozen both x and y axes in the below code

from plotly.subplots import make_subplots
buttons =  [  dict( method='update', label='b1', visible=True, args=[{'y':[1,2,3]}] ),  
              dict( method='update', label='b2', visible=True, args=[{'y':[30,20,10]}] )]
fig = make_subplots()
fig.add_scatter(x=[2,4,6], y=[1,2,3]) 
    .update_layout(updatemenus=[{'buttons':buttons}]) 
    .update_xaxes(range=[-10, 10]) 
    .update_yaxes(range=[0, 30])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...