Color change by range in line chart


#1

How can i change color of the line chart. For example i have row of data and i would like to plot a line graph which is black when y value is above 50 and white when y value is below 25.


#2

Hi @gridiron9999,

It’s not possible to change the color of a line within a single scatter trace. But if you only have a few discrete colors you could probably make something work with multiple traces.

Something like

import numpy as np
import pandas as pd
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode()

xs = np.linspace(0, 20, 100)
ys = np.sin(xs) + xs*0.1
df = pd.DataFrame({'x': xs, 'y': ys})
theshhold = 1.0

fig = go.Figure()
# Full line
fig.add_scattergl(x=xs, y=df.y, line={'color': 'black'})

# Above threshhgold
fig.add_scattergl(x=xs, y=df.y.where(df.y >= 1), line={'color': 'red'})

iplot(fig)

Hope that helps!
-Jon


#3

Dear @jmmease i used exactly the same code for another data. In some range it gives right but sometimes it does not plot it correctly

import numpy as np
import pandas as pd
import telebot
import plotly.graph_objs as go
import plotly.io as pio
import datetime as dy
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode()

df = pd.DataFrame({‘x’: list2, ‘y’: list1})
theshhold = 1.0

fig = go.Figure()

Full line

fig.add_scattergl(x=xs, y=df.y, line={‘color’: ‘black’})

Above threshhgold

fig.add_scattergl(x=xs, y=df.y.where(df.y >= 50), line={‘color’: ‘red’})

fig.add_scattergl(x = xs, y = df.y.where(df.y <= 35), line ={‘color’ : ‘green’})

iplot(fig)


#4

For example here it gives me red for y axis which is greater than roughly 65 but i give it 50 as reference point. Could you explain why. And i also want to ask about takin plot as a picture. Picture which is demonstrated below is the depiction of the plot as a picture but using code. First one is by using snipping tool in the windows. There is enormous difference right? So what i am asking you is to give me best way of capturing graph as a picture by using code, not capturing it manually. I would highly appreciate it if you give me some instructions


#5

Hi @gridiron9999,

You can export plots as static images using the orca image conversion utility. See https://plot.ly/python/static-image-export/ for instructions.

I think the reason you’re seeing the red line start at ~65 is that this is the first point along your dataset that is above 50. If you look closely at your dataset, I expect that the point right before the one valued 65 is less than 50.

-Jon