Change your plot theme for plotly or plotly_express

I made a scatter plot with plotly_express.

It didn’t look good. So I wondered how to change the theme.

It took me way too long to figure out.

First I looked through the documentation here. I didn’t seem to see any relevant source code. Instead was a bunch of stuff about creating a new theme. Not useful.

Finally I found code like fig.layout.template = 'plotly_dark' buried in an image. Turns out that’s how you do it.

So. It’s a two step process:

  1. Save your plot to a variable: e.g. fig = px.scatter(df, x='x', y='y', color='clr').
  2. Write fig.layout.template = 'theme_here'. Obviously change theme_here to one of the plotly themes: ggplot2, plotly_dark, seaborn, plotly, plotly_white, presentation, or xgridoff.

That’s it.