Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change linewidth in python plotly px figure

Tags:

python

plotly

I'm unclear how to style a line in a plotly express figure, altering color and width. The plotly documentation offers suggestions to style lines using go, but I do not see information for px.

Example

import plotly.express as px

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

I tried putting line=dict(color='firebrick', width=4) as an argument of px.line but that throws the error line() got an unexpected keyword argument 'line' as it is code for go rather than px.

like image 873
ncraig Avatar asked May 08 '26 17:05

ncraig


1 Answers

Plotly px line styles can be updated with update_traces function see documentation for further information. The following example modifies the prior figure making all of the lines black and thin.

import plotly.express as px

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

# This styles the line
fig.update_traces(line=dict(color="Black", width=0.5))

fig.show()
like image 84
ncraig Avatar answered May 10 '26 05:05

ncraig



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!