mardi 9 novembre 2021

Using go.scatter with conditional statements in python

I am trying to use go.scatter with my conditional statements.

A and df['T_orNonT'] are columns in my dataframe. If a row on "A" is less than or equal to 200, the column df['T_orNonT'] will show 'Non-T', otherwise it is 'T'

I want to plot them using go.scatter with 'T' or 'Non-T' showing up with different color. Here is my code:

import plotly.graph_objects as go
fig = go.Figure()
for i in range (0, length):
    if A[i] <=  200:
        df['T_or_NonT'].iloc[i] = 'Non-T'
        fig = go.Figure()
        fig.add_trace(go.Scatter(
             x = df['Date'],
             y = df['A'],
             mode ='markers',
             name='T',
             marker=dict(color ='red')))
        fig.show()


    else:
        df['T_or_NonT'].iloc[i] = 'T'
        fig = go.Figure()
        fig.add_trace(go.Scatter(
             x = df['Date'],
             y = df['A'],
             mode ='markers',
             name='Non-T',
             marker=dict(color ='green')))
        fig.show()

but I can't make it work. I want to know the right way to code this. by the way i am a python beginner-user.

Aucun commentaire:

Enregistrer un commentaire