I have a data set with
y value pairs, plus
upper_limit values for
I want to plot
y in a plot.ly scatter plot, and colour the marker in green if
upper_limit, else in red.
I know that I could use 2 traces, or add a
color column in the DataFrame. However, I’d like to generate these colour on the fly and use one trace only.
Consider this data set:
x y lower_limit upper_limit 0 1 13 10 15 1 2 13 15 20 2 3 17 15 20
The first marker (
y=13) should be green, because
upper_limit (10 ≤ 13 ≤ 15), just like the third one.
However the second should be red, because
import pandas as pd import plotly.graph_objs as go import plotly.plotly as py import plotly.offline as po data = [ [1, 13, 10, 15], [2, 13, 15, 20], [3, 17, 15, 20] ] df = pd.DataFrame( data, columns=['x', 'y', 'lower_limit', 'upper_limit'] ) trace = go.Scatter( x=df['x'], y=df['y'], mode='markers', marker=dict( size=42, # I want the color to be green if # lower_limit ≤ y ≤ upper_limit # else red color='green', ) ) po.plot([trace])