Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.7k
Description
According toTheming and templates in Python, a figure can be created using a predefinedtemplate
or updated to match it. However, the way updates happen is confusing if not inconsistent. Consider the examples below forplotly[express] == 6.2.0
.
Plotly Express
importplotly.expressaspxdf=px.data.iris()# uses the default `plotly` templatefig=px.scatter(df,x="sepal_width",y="sepal_length",color="petal_length",title="Figure 1")fig.show()# update to use another template, note that the colour scale is unchangedfig.update_layout(template="seaborn",title="Figure 2")fig.show()# this is not equivalent to Figure 2fig=px.scatter(df,x="sepal_width",y="sepal_length",color="petal_length",template="seaborn",title="Figure 3")fig.show()
So Figure 3 is not the same as Figure 2, at least because the colour scales don't match. For custom themes, I can imagine that there may be other components of the figure object that would differ. As far as I know, there is no way to update traces using a template, e.g.,fig.update_traces(template="seaborn")
, to adjust non-layout settings.
Plotly Graph Object
To make things more confusing, there is aSpecifying themes in graph object figures example in the documentation that does the same, but it actually works as expected. Adjusting the example we get:
importplotly.expressaspximportplotly.graph_objectsasgodf=px.data.iris()# uses the default `plotly` templatefig=go.Figure(data=go.Scatter(x=df["sepal_width"],y=df["sepal_length"],mode="markers",marker={"color":df["petal_length"],"showscale":True, }, ))fig.update_layout(title="Figure 4")fig.show()# update to use another template, note that the colour scale has changed as expectedfig.update_layout(template="seaborn",title="Figure 5")fig.show()
However, specifying an explicitcolorscale
will prevent a template update from setting its own colour scheme:
# same as Figure 4 but with an explicit `colorscale`fig=go.Figure(data=go.Scatter(x=df["sepal_width"],y=df["sepal_length"],mode="markers",marker={"color":df["petal_length"],"colorscale":"Viridis",# or any other colour scale"showscale":True, }, ))fig.update_layout(title="Figure 6")fig.show()# no longer matches Figure 5fig.update_layout(template="seaborn",title="Figure 7")fig.show()
Suggestions
- I would expect
fig.update_layout(template="seaborn")
to have the same effect, regardless of whetherpx
orgo
interface is used. From this perspective, to result for Figure 2 is a bug (the colour scale should have changed forseaborn
template). - When an existing figure is updated, the settings from a template should take precedence over any existing settings. So Figure 5 and Figure 7 should produce the same result. This would entail that an update in Figure 7 overwrites whatever the
colorscale
setting Figure 6 has with that of a template provided inupdate_layout
.