Black Lives Matter. Please consider donating to Black Girls Code today.

Changing Line from Solid to Dashed after Certain Number

Hi everyone, I am working on a simple line plot off an imported Pandas CSV file which contains years and GDP values. I’m trying to get the solid line to change from a solid line to a dashed line after a certain year.

This is an example of one of the traces which works great in its current state:

trace_ukraine = go.Scatter(
x = gdp.Year,
y = gdp.Ukraine,
mode = ‘lines’,
name = “Ukraine”,

I want to add something like:

if x > 2018:
dash = “dash”

It’s just a matter of changing the line type after a certain year is met. I’ve tried different approaches inside the trace itself as well as after all of the traces in the code. If anyone understands how to do this, I would be very appreciative.


Hi @nymosis,

You won’t be able to change the dash style within a single trace, but you should be able to do this with two separate traces.

Here’s a starting point for you

import pandas as pd

gdp = pd.DataFrame({'Year': [1990, 1991, 1992, 1993, 1994], 'Ukraine': [5, 4, 7, 4, 6]})
gdp1 = gdp.loc[gdp.Year <= 1992]
gdp2 = gdp.loc[gdp.Year >= 1992]

fig = go.FigureWidget(data=[
    go.Scatter(x=gdp1.Year, y=gdp1.Ukraine, mode='lines', line={'dash': 'solid', 'color': 'green'}),
    go.Scatter(x=gdp2.Year, y=gdp2.Ukraine, mode='lines', line={'dash': 'dash', 'color': 'green'}),

Hope that helps!

Jon, that’s immensely helpful. I really appreciate your quick response.