如何更改 Plotly 中的默认符号序列?

How to change default symbol sequence in Plolty?

如何更改 Plotly 中的默认符号序列?我正在尝试使用以下 MWE

import plotly.express as px
import plotly.io as pio
import plotly.graph_objects as go

my_template = pio.templates['ggplot2'] # Copy this template.
# Now modify some specific settings of the template.
my_template.data.scatter = [
    go.Scatter(
        marker = dict(size=22),
        symbol_sequence = ['circle', 'square', 'diamond', 'cross', 'x', 'star'],
    )
]
pio.templates.default = my_template # Set my template as default.

fig = px.line(
    data_frame = px.data.gapminder(),
    x = "year",
    y = "lifeExp",
    color = "country",
    symbol = "continent",
)

fig.show()

这会引发错误

ValueError: Invalid property specified for object of type plotly.graph_objs.Scatter: 'symbol'

我有 plotly.__version__ == 5.3.1.

我还注意到,在这个 MWE 中,默认标记大小由于某种原因不起作用,我插入的值被忽略了。

仔细查看模板,模板本身似乎没有标记大小或顺序。为什么不直接用.update_traces()调整标记大小,把符号序列放在px.line()?

import plotly.express as px
import plotly.io as pio
import plotly.graph_objects as go

my_template = pio.templates['ggplot2'] # Copy this template.
pio.templates.default = my_template # Set my template as default.

fig = px.line(
    data_frame = px.data.gapminder(),
    x = "year",
    y = "lifeExp",
    color = "country",
    symbol = "continent",
    symbol_sequence = ['circle', 'square', 'diamond', 'cross', 'x', 'star']

)

fig.update_traces(marker=dict(size=22))
fig.show()

原文:

添加附加参数后:

creating themes可以看出:

If a trace type property is set to a list of more than one trace, then the default properties are cycled as more traces are added to the figure.

因此您可以设置多个 go.Scatter() 并用不同的替换 "diamond" 作为 symbol in:

symbol_template.data.scatter = [
    go.Scatter(marker=dict(symbol="diamond", size=10)),
    go.Scatter(marker=dict(symbol="square", size=10)),
    go.Scatter(marker=dict(symbol="circle", size=10)),
]

或者您可以像这样使用具有指定序列的列表理解:

new_sequence = ['pentagon', 'hexagram', 'star', 'diamond', 'hourglass', 'bowtie', 'square']

my_template.data.scatter = [
    go.Scatter(marker=dict(symbol=s, size=12)) for s in new_sequence
]

剧情:

完整代码:

import plotly.express as px
import plotly.io as pio
import plotly.graph_objects as go

my_template = pio.templates['ggplot2'] 

new_sequence = ['pentagon', 'hexagram', 'star', 'diamond', 'hourglass', 'bowtie', 'square']

my_template.data.scatter = [
    go.Scatter(marker=dict(symbol=s, size=12)) for s in new_sequence
]

pio.templates.default = my_template

df = px.data.gapminder()
df = df.tail(100)

fig = px.line(
    data_frame = df,
    x = "year",
    y = "lifeExp",
    color = "country",
    symbol = "continent",
)

fig.show()