Inconsistent theme updates using `template`
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 18.8k
- 派生
- 2.8k
- 平均合并
- 16 小时 26 分钟
- 30 天内合并 PR
- 21
描述
According to Theming and templates in Python, a figure can be created using a predefined template or updated to match it. However, the way updates happen is confusing if not inconsistent. Consider the examples below for plotly[express] == 6.2.0.
Plotly Express
import plotly.express as px
df = px.data.iris()
# uses the default `plotly` template
fig = 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 unchanged
fig.update_layout(template="seaborn", title="Figure 2")
fig.show()
# this is not equivalent to Figure 2
fig = 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 a Specifying themes in graph object figures example in the documentation that does the same, but it actually works as expected. Adjusting the example we get:
import plotly.express as px
import plotly.graph_objects as go
df = px.data.iris()
# uses the default `plotly` template
fig = 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 expected
fig.update_layout(template="seaborn", title="Figure 5")
fig.show()
However, specifying an explicit colorscale 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 5
fig.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 whetherpxorgointerface is used. From this perspective, to result for Figure 2 is a bug (the colour scale should have changed forseaborntemplate). - 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
colorscalesetting Figure 6 has with that of a template provided inupdate_layout.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先使用 template 和 update_layout 重现 Plotly Express 和 graph-object 示例,包括显式 colorscale 的情况。比较 Figure 2/3 和 Figure 5/7 如何解析模板设置,然后明确提出的两种行为中哪一种是预期行为。当接受的模板更新语义在两个接口之间保持一致,且文档中的示例不再存在差异时,即视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- data-visualization
- Issue 类型
- 缺陷
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 25/100