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

Question:

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?

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

Answered By: Nilo Araujo

This works to update a trace (need a specific line to be a dot). However, intuitively reading the documentation for px.line suggests the parameter ‘line_dash_map’ would allow a dict to specify a line dash style to a column name similar to how ‘color_discrete_map’ works (it doesn’t throw an error if you do but also does not update the px.line object). I’ve seen other posts that say to specify a dash style to a value in a column but 1) I don’t want to unpivot my table and 2) do not want to create the memory increase in a very large dataset by creating a new column.

Answered By: Chris