Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly add_hline() doesn't work with simple_white template

Tags:

plotly

I was attempting to add a horizontal line to a Plotly graph using Plotly's add_hline(). It works perfectly fine so long as I do not use the simple_white template. The MWE is here:

import plotly.express as px

df = px.data.iris()
fig = px.scatter(df, x="petal_length", y="petal_width")
fig.add_hline(y=0.9, line_dash='dash', line_color='Red')
fig.update_layout(width=400, height=400,
                  # template='simple_white'
                  )
fig.show()

Without the template='simple_white' line, the add_hline() works as expected. With that template though, and with no other changes, the plot is the same but without the horizontal line.

enter image description here enter image description here

like image 580
rsgny Avatar asked Mar 15 '26 09:03

rsgny


1 Answers

I can't say why this happens, but you can get around it by explicitly specifying line_width and opacity for the horizontal line, without messing with the template:

fig.add_hline(y=0.9, opacity=1, line_width=2, line_dash='dash', line_color='Red')

If you only specify line width, the line looks washed out, possibly some of the themes (seaborn and ggplot2 in addition to plotly_white) change these two parameters?

like image 100
Peter Avatar answered Mar 20 '26 10:03

Peter



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!