I am trying to make a line graph in Plotly, with a dotted and marked line. in this case I want the line that represents "Canada" to be dotted. I have figured out how to change the width and color but can't figure out how to make the line dotted. My original DF is much bigger, so go.scatter wouldn't be a solution for me. thanks for the help!

import plotly.express as px# datadf = px.data.gapminder()df = df[df['country'].isin(['Canada', 'USA', 'Norway', 'Sweden', 'Germany'])]# plotlyfig = px.line(df, x='year', y='lifeExp',color='country')# color "Canada" black and change the width of the linefig.update_traces(patch={"line": {"color": "black", "width": 4}},#selector={"legendgroup": "Canada"})#i tried to extend the code with dash and mode, but do not work:#fig.update_traces(patch={"line": {"color": "black", "width": 4, "dash": 'dot', "mode": 'lines+markers'}}, plotly.io.write_image(fig, file='testline.png', format='png')

I expect a dotted black line that will end up looking something like this.enter image description here

1

Best Answer


The error in your code was that you were setting the mode to lines+markers, which is an invalid property in a line chart. The code that worked looks like this:

import plotly.express as px# datadf = px.data.gapminder()df = df[df['country'].isin(['Canada', 'USA', 'Norway', 'Sweden', 'Germany'])]# plotlyfig = px.line(df, x='year', y='lifeExp',color='country')fig.update_traces(patch={"line": {"color": "black", "width": 4}})fig.update_traces(patch={"line": {"color": "black", "width": 4, "dash": 'dot'}}, selector={"legendgroup": "Canada"}) fig.show()

And returned a graph like this:

enter image description here