Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly line opacity with plotly.express

I am doing some line plots with plotly.express. As MWE we can consider any of the examples in the documentation, for example this one which is:

import plotly.express as px

df = px.data.gapminder().query("continent=='Oceania'")
fig = px.line(df, x="year", y="lifeExp", color='country', facet_row='country')
fig.show()

How would you change the opacity of the lines, without altering their color or any other of their properties?

I have tried to use fig.update_traces(line=dict(width=1, opacity=.4)), the width property works but opacity raises an error.

In the past with plotly.graph_objects I have used the answers to this question but this requires me to choose the color, this time I want plotly.express to do that for me. So I don't know the color of each trace.

like image 332
user171780 Avatar asked Dec 31 '25 19:12

user171780


1 Answers

More than a year later I ended up with the same issue, and Google brought me here. Posting here the solution for quick reference:

fig.update_traces(opacity=.4)
like image 150
user171780 Avatar answered Jan 06 '26 04:01

user171780



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!