Search code examples
pythonplotly

Change mode for plotly.express


I have a bunch of plotly plots. I am giving example from the plotly site.

I want to plot all of them in dark mode. I have to manually enter template="plotly_dark" for each plot.

How can I change the default setting of my display so that I do not have to manually enter template="plotly_dark" for each plot?

import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species", marginal_y="violin",
           marginal_x="box", trendline="ols", template="plotly_dark")
fig.show()

import plotly.express as px
df = px.data.iris()
df["e"] = df["sepal_width"]/100
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species", error_x="e", error_y="e", template="plotly_dark")
fig.show()

Solution

  • You can set it as default as below

    import plotly.io as pio
    import plotly.express as px
    
    pio.templates.default = "plotly_dark"
    
    df = px.data.iris()
    fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species", marginal_y="violin",
               marginal_x="box", trendline="ols")
    fig.write_image('im1.png')
    
    df["e"] = df["sepal_width"]/100
    fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species", error_x="e", error_y="e")
    fig.write_image('im2.png')
    

    Ref: https://plotly.com/python/templates/