0
fig = go.Figure()
f = []
hours = range(0, 60)
pts = [rd.randrange(11, 100, 13) for i in range(1, 61)]
for i in range(1, 61):
           f.append(go.Frame(data=[go.Scatter(x=list(hours[1:61]), y=list(pts[:i]), name="PRB
           Usage",line=dict(color="#1E90FF"),marker=dict(size=10,line=dict(width=3,color="yellow")))]))
fig.update(frames=f)
c = fig.to_html(config={'displayModeBar': False})

Here my animation have 60 frames of jointed scatter plot , after finishing with animation i want to change color of those points who are greater than 80 and less than 20

I tried to add_trace of new plot after fig.update(frames=f) statement but it showing brefore completing the animation

1 Answers1

0

Your question only has a partial code, so I have made up the deficiency with my guess. If you want to add special processing to the last graph of the animation, create a new final frame and replace the last frame in the list of already existing frames. The color condition specifies a list of colors above and beyond the standard value in the new last frame.

import plotly.graph_objects as go
import random as rd

f = []
hours = range(0, 60)
pts = [rd.randrange(11, 100, 13) for i in range(1, 61)]

data = [go.Scatter(x=[], y=[])]

layout = go.Layout(
    xaxis=dict(range=[0,60], autorange=False),
    yaxis=dict(range=[0,100], autorange=False),
    title='Animation frames',
    updatemenus=[dict(
        type="buttons",
        buttons=[dict(label="Play",
                          method="animate",
                          args=[None])])]
)
    
for i in range(1, 61):
    f.append(go.Frame(data=[
        go.Scatter(
            mode='markers',
            x=list(hours[:i]),
            y=list(pts[:i]),
            name="PRB Usage",
            # line=dict(color="#1E90FF"),
            marker=dict(size=10, color="blue")
        )
    ]))

f[-1] = go.Frame(data=[go.Scatter(mode='markers', x=list(hours), y=pts, marker=dict(size=10, color=['red' if x >= 80 else 'blue' for x in pts]))])
    
fig = go.Figure(data=data, layout=layout, frames=f)

fig.show()

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32
  • okay , its working n what we can do if we want legend for those red points and blue points – Sonali Shelke Nov 25 '22 at 08:16
  • The legend of plotly is in units of graph elements, so in this case the y-value is one of a kind and the legend is also one. I am forced to use two colors under certain conditions. I haven't tried it, but it might be possible if the structure of the final frame is a list of scatter plots for the red group and a scatter plot for the blue group. – r-beginners Nov 25 '22 at 08:28
  • yes , and here i wanted to have 2 conditions if point > 80 color it red and point < 20 color it yellow , and rest will be blue – Sonali Shelke Nov 25 '22 at 08:52
  • I added a scatter plot in the last frame with a list but it did not work. As an alternative, I created a separate graph object and added a graph above the threshold and then a graph below the threshold. The resulting graph data was used to update the last frame. This works, but only some of the scatter plots are displayed at the end of the animation. (I'm trying to figure out what's going on, but so far I haven't found a solution. Please check the contents. If you have any further questions, please make a separate new question or ask the community. – r-beginners Nov 25 '22 at 12:33
  • See this: [Colab](https://colab.research.google.com/drive/1XQb-dIJCDDVEuxr0j3Qk5RhsoDm0P2Et?usp=sharing) If you review this comment, I will disable the link. – r-beginners Nov 25 '22 at 12:34