How to create line-dash for specific columns in a dataframe in plotly?

Viewed 182

I have a pandas dataframe that has 4 columns and would resemble something like this-

a = [19.1, 18.4, 19.2, 18.2, 17.4]
b = [62.28, 57.07, 55.61,54.09,54.6]
c = [18.68,17.79,17.03,18.62,18.62]
d = [73.03,71.63,70.22,71.77,71.77]

I am plotting a line graph to visualize each of the columns using Plotly express. I want to plot line dash (dashed lines) for columns a and c alone, and regular lines for columns b and d. How can I create line dash for columns a and c using Plotly express?

1 Answers

You can use update_traces to pick which traces to change from the default style:

import plotly.express as px
import pandas as pd

a = [19.1, 18.4, 19.2, 18.2, 17.4]
b = [62.28, 57.07, 55.61, 54.09, 54.6]
c = [18.68, 17.79, 17.03, 18.62, 18.62]
d = [73.03, 71.63, 70.22, 71.77, 71.77]

df = pd.DataFrame({"a": a, "b": b, "c": c, "d": d})
px.line(df).update_traces(
    selector={"name": "a"}, 
    line={"dash": "dash"}
).update_traces(
    selector={"name": "c"}, 
    line={"dash": "dash"}
)

enter image description here

Related