为了账号安全,请及时绑定邮箱和手机立即绑定

Plotly:如何仅使用一条迹线来定义一个开放标记而不需要一条线穿过它?

Plotly:如何仅使用一条迹线来定义一个开放标记而不需要一条线穿过它?

眼眸繁星 2023-08-08 16:30:35
我想用圆开放标记绘制“线条+标记”图,而不让线条穿过它。import plotly.graph_objects as goimport numpy as npx = np.arange(10)fig = go.Figure()fig.add_trace(    go.Scatter(        x=x, y=x**2,        mode='lines+markers',        line=dict(color='green'),        marker_size=16,        marker_symbol='circle-open'    ))fig.update_layout(    plot_bgcolor='white')fig.show()这会产生一条穿过开放标记的线。然后我尝试在线条顶部添加背景色标记 - 但随后在图例中我只得到标记或线条,而不是两者组合。有没有办法以这种方式获得带有标记和线条的图例?fig = go.Figure()fig.add_trace(    go.Scatter(        x=x, y=x**2,        mode='lines',        line=dict(color='red'),        showlegend=False,        legendgroup='legend'    ))fig.add_trace(    go.Scatter(        x=x, y=x**2,        mode='markers',        marker_color='white',        # line=dict(color='green'),        marker_size=12,        marker_symbol='circle',        marker_line=dict(            width=3,            color='red'        ),    legendgroup='legend'    ))fig.update_layout(    plot_bgcolor='white',)fig.show()
查看完整描述

1 回答

?
郎朗坤

TA贡献1921条经验 获得超9个赞

使用几个属性的正确组合应该可以帮助您实现这一目标:

  1. marker_symbol='circle'

  2. marker_color = 'white'

  3. marker = dict(line = dict(color='green', width = 2))

阴谋

https://img1.sycdn.imooc.com//64d1fd5000013e0112200405.jpg

完整代码:

import plotly.graph_objects as go

import numpy as np

x = np.arange(10)


fig = go.Figure()

fig.add_trace(

    go.Scatter(

        x=x, y=x**2,

        mode='lines+markers',

        line=dict(color='green'),

        marker_size=16,

        marker_symbol='circle',

        name = 'no line through this',

        showlegend = True,

        

        marker_color = 'white',

        marker = dict(line = dict(color='green', width = 2))

    )

)

fig.update_layout(

    plot_bgcolor='white'

)

fig.show()


查看完整回答
反对 回复 2023-08-08
  • 1 回答
  • 0 关注
  • 65 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信