8

I am using the following code to create a simple plotly line chart in Python. I have two variables (at the bottom of the code):

ctime
amount

ctime just using the current time for each element in amount; there are 10 times amount is a contained of amounts ranging from 0-1000; the are ten amounts

I want to color my plot markers in the following way:

amount is less than 300; that specific marker for that value will be green amount is between 300 and 400; that specific marker for that value will be yellow amount is greater than 400; that specific marker for that value will be red

Is there any way I can build in a conditional type handler for this?

layout = Layout(
    title='Current Amount',
    titlefont=Font(
        family='"Open sans", verdana, arial, sans-serif',
        size=17,
        color='#444'
    ),
    font=Font(
        family='"Open sans", verdana, arial, sans-serif',
        size=12,
        color='#444'
    ),
    showlegend=True,
    autosize=True,
    width=803,
    height=566,
    xaxis=XAxis(
        title='Time',
        titlefont=Font(
            family='"Open sans", verdana, arial, sans-serif',
            size=14,
            color='#444'
        ),
        range=[1418632334984.89, 1418632334986.89],
        domain=[0, 1],
        type='date',
        rangemode='normal',
        autorange=True,
        showgrid=False,
        zeroline=False,
        showline=True,
        autotick=True,
        nticks=0,
        ticks='inside',
        showticklabels=True,
        tick0=0,
        dtick=1,
        ticklen=5,
        tickwidth=1,
        tickcolor='#444',
        tickangle='auto',
        tickfont=Font(
            family='"Open sans", verdana, arial, sans-serif',
            size=12,
            color='#444'
        ),
        mirror='allticks',
        linecolor='rgb(34,34,34)',
        linewidth=1,
        anchor='y',
        side='bottom'
    ),
    yaxis=YAxis(
        title='GHI (W/m2)',
        titlefont=Font(
            family='"Open sans", verdana, arial, sans-serif',
            size=14,
            color='#444'
        ),
        range=[-5.968375815056313, 57.068375815056314],
        domain=[0, 1],
        type='linear',
        rangemode='normal',
        autorange=True,
        showgrid=False,
        zeroline=False,
        showline=True,
        autotick=True,
        nticks=0,
        ticks='inside',
        showticklabels=True,
        tick0=0,
        dtick=1,
        ticklen=5,
        tickwidth=1,
        tickcolor='#444',
        tickangle='auto',
        tickfont=Font(
            family='"Open sans", verdana, arial, sans-serif',
            size=12,
            color='#444'
        ),
        exponentformat='B',
        showexponent='all',
        mirror='allticks',
        linecolor='rgb(34,34,34)',
        linewidth=1,
        anchor='x',
        side='left'
    ),
    legend=Legend(
        x=1,
        y=1.02,
        traceorder='normal',
        font=Font(
            family='"Open sans", verdana, arial, sans-serif',
            size=12,
            color='#444'
        ),
        bgcolor='rgba(255, 255, 255, 0.5)',
        bordercolor='#444',
        borderwidth=0,
        xanchor='left',
        yanchor='auto'
    )
)


new_data = Scatter(x=ctime, y=amount)
data = Data( [ new_data ] )

1 Answer 1

9

So for your use case, you need to use the property marker.color under line chart, which is given in the official documentation as.

color (color)
Sets the marker color. It accepts either a specific color or an array of numbers that are mapped to the colorscale relative to the max and min values of the array or relative to cmin and cmax if set.

Read more here

Below is a simple working example demonstrating your use case, please apply it to your solution and let me know if your issue is resolved.

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs,init_notebook_mode,plot,iplot
init_notebook_mode(connected=True)

x = [1,2,3,4,5,6,7,8,9]
y = [100,200,300,400,500,600,700,800,900]

# function below sets the color based on amount
def SetColor(x):
    if(x < 300):
        return "green"
    elif(x >= 300 | x <= 400):
        return "yellow"
    elif(x > 400):
        return "red"

# Create a trace
trace = go.Scatter(
    x = x,
    y = y,
    marker = dict(color=list(map(SetColor, y)))
)

iplot([trace], filename='basic-line')

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.