简体   繁体   中英

Prevent Autoscale on colorbar within Plotly.py animation

I have a Plotly animation with a colorscale and associated colorbar. What I want to do is fix the range of the colorbar and prevent it being rescaled on every frame. What I want to see is where my data is 'in' or 'out of predefined specification but this doesn't work with autoscale working on every frame. it rescales with each frame, so the colorbar may go from -2 to -4 on one frame but the same colour range is divided between 0 to -5 on the next frame.

figA = px.scatter(dfAnime, x="Easting", y="Northing", animation_frame="shot",
    color='Depth',range_x=[np.min(dfAnime['Easting']),np.max(dfAnime['Easting'])],
    range_y=[np.min(dfAnime['Northing']),np.max(dfAnime['Northing'])],
    color_continuous_scale=[(0,'red'),(0.33,'orange'),(0.58,'green'),(0.83,'orange'),(1,'red')],
    range_color=[0,-6])
    
figA.layout.updatemenus[0].buttons[0].args[1]["frame"]["duration"]=100 # Turns off play/stop button
figA.update_yaxes(
    scaleanchor="x",
    scaleratio=1,
    exponentformat='none')
figA.update_xaxes(
    scaleanchor="x",
    scaleratio=1,
    exponentformat='none')
figA.update_layout(template="plotly_dark",title="Line  "+str(name),autoscale=False)           
#figA.show()
figA.write_html("C:/Users/client1/Desktop/anime.html")

I'd like to fix my range between 0 and -6 (these are the extremes I'm likely to see). I thought I'd done that in the code above but I assume Autoscale is overriding it. 在此处输入图像描述

I have tried examples, and the conclusion I can draw from them is range_color is working fine. However, your argument to range_color is what is causing the problem. Since it is a range, it is only sensible to define it in an increasing manner, ie, [0,-6] is not a correct range. Instead, you should use [-6,0].

I can confirm this by the following:

import plotly.express as px
dfx = px.data.tips()

After this, making the scatter plot with 3 choices for range_color:

px.scatter(dfx, x='total_bill', y='tip', color='size', template='plotly_dark').show()
px.scatter(dfx, x='total_bill', y='tip', color='size', template='plotly_dark', range_color=[2,4]).show()
px.scatter(dfx, x='total_bill', y='tip', color='size', template='plotly_dark', range_color=[4,2]).show()

imgur album with results of all 3 cases

How range_color works is that it narrows the range of the colors to the specified range and the color of any point lower/higher than that range is taken as the minimum/maximum color value.

So, px cannot make sense of an inverted range, and you would need to invert it manually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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