How to change default symbol sequence in Plolty?

Viewed 463

How do I change the default symbol sequence in Plotly? I am trying with the following 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()

This raises the error

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

I have plotly.__version__ == 5.3.1.

I have also noticed that in this MWE the default marker size is not working for some reason, the value I insert is ignored.

2 Answers

From creating themes you can see that:

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.

So you can either set up multiple go.Scatter() with different replacements for "diamond" as 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)),
]

Or you can use a list comprehension with a specified sequence like so:

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
]

Plot:

enter image description here

Complete code:

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()

Looking through the templates, there doesn't seem to be a marker size or sequence in the templates themselves. Why not just use the .update_traces() to adjust the marker size, and put the symbol sequence in the 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()

Original:

enter image description here

After adding additional parameters:

enter image description here

Related