[英]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 中,默认标记大小由于某种原因不起作用,我插入的值被忽略。
您可以设置您想要的任何符号序列,然后使用 itertools cycle and next 与 fig.for_each_trace() 将它们应用于您的图形:
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)))
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()
查看模板,模板本身似乎没有标记大小或序列。 为什么不直接使用.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()
原来的:
添加附加参数后:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.