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

我想用圆开放标记绘制“线条+标记”图,而不让线条穿过它。


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


眼眸繁星
浏览 104回答 1
1回答

郎朗坤

使用几个属性的正确组合应该可以帮助您实现这一目标:marker_symbol='circle'marker_color = 'white'marker = dict(line = dict(color='green', width = 2))阴谋完整代码: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',        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()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python