简体   繁体   中英

Change default symbol sequence in plolty

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.

You can set up which ever sequence of symbols you'd like, and then use itertools cycle and next together with fig.for_each_trace() to apply them to your figure:

new_symbols =  cycle(['pentagon', 'hexagram', 'star', 'diamond', 'hourglass', 'bowtie', 'square', 'diamond', 'cross', 'x'])
fig.for_each_trace(lambda t: t.update(marker_symbol = next(new_symbols)))

Plot:

在此处输入图片说明

Complete code:

import plotly.express as px
import plotly.graph_objects as go
from itertools import cycle

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

fig.update_traces(marker_size = 12)

new_symbols =  cycle(['pentagon', 'hexagram', 'star', 'diamond', 'hourglass', 'bowtie', 'square', 'diamond', 'cross', 'x'])
fig.for_each_trace(lambda t: t.update(marker_symbol = next(new_symbols)))

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:

在此处输入图片说明

After adding additional parameters:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM