Hide lines' parts overlapping with markers

Hey, I have a plot like this:

Is there any way to hide lines’ parts when they overlap with markers? It doesn’t matter when the symbol is full, but it does matter when it is open and even more so when it is open-doted.

Code
import plotly.graph_objects as go

x1, y1 = [1, 10, 12], [6, 7, 8]
x2, y2 = [x + 3 for x in x1], [(y + 2) / 1.33 for y in y1]
x3, y3 = [2, 4, 6], [8, 7, 4.5]

xs = [x1, x2, x3]
ys = [y1, y2, y3]
colors = ["blue", "red", "green"]
symbols = ["triangle-up", "circle-open-dot", "square-open"]
names = ["full", "open-dot", "open"]

fig = go.Figure()

for x, y, color, symbol, name in zip(xs, ys, colors, symbols, names):
    fig.add_trace(
        go.Scatter(
            x=x,
            y=y,
            mode="markers",
            marker=dict(color=color, size=12, symbol=symbol),
            name=name
        ),
    )
    fig.add_trace(
        go.Scatter(
            x=x,
            y=y,
            mode="lines",
            line=dict(color=color, width=1.5),
            opacity=0.5,
            showlegend=False
        ),
    )

fig.update_layout(template="simple_white", width=800, height=400)

fig.show()

Any help would be highly appreciated :slight_smile:

Hi @KaparaNewbie, since you are using a white background, you could use full white symbols and set the line color of the markes:

import plotly.graph_objects as go

fig=go.Figure(
    data=
        go.Scatter(
            x=[1,2,3], 
            y=[1,2,3], 
            mode='markers+lines', 
            marker={
                'symbol': 'circle', 
                'color': 'white',
                'size': 20,
                'line':{
                    'width': 2, 
                    'color': 'red'
                }
            },
            line={
                'color': 'red',
                'width': 2
            }
        ),
    layout={'plot_bgcolor': 'white'}
)
fig.show()

creates:

Hey @AIMPED , thx!
However, it doesn’t work for open or open-dot symbols, which I also need to use.

1 Like

Why do you think this would not work? Obviously you can’t use “open” symbols, that’s the reason for this workaround :thinking:

import plotly.graph_objects as go

fig=go.Figure(
    data=
        go.Scatter(
            x=[1,2,3], 
            y=[1,2,3], 
            mode='markers+lines', 
            marker={
                'symbol': 'circle-dot', 
                'color': 'white',
                'size': 20,
                'line':{
                    'width': 2, 
                    'color': 'red'
                }
            },
            line={
                'color': 'red',
                'width': 2
            },
            name='open-dot',
            showlegend=True
        ),
    layout={'plot_bgcolor': 'white'}
)
fig.add_scatter(
    x=[2,4,9], 
    y=[6,5,8],
    mode='markers+lines', 
            marker={
                'symbol': 'square', 
                'color': 'white',
                'size': 20,
                'line':{
                    'width': 2, 
                    'color': 'green'
                }
            },
            line={
                'color': 'green',
                'width': 2
            },
            name='open',
            showlegend=True
)
fig.add_scatter(
    x=[6,8,9],
    y=[2,1,5],
    mode='markers+lines', 
            marker={
                'symbol': 'triangle-up',
                'color': 'blue',
                'size': 20,
                'line':{
                    'width': 2, 
                    'color': 'blue'
                }
            },
            line={
                'color': 'blue',
                'width': 2
            },
            name='full',
            showlegend=True
)
fig.show()

creates:

2 Likes

Oh, now I get it. You replaced -open-dot with -dot. Now everything works, thank you very much!

Systemic implementation for future reference
import plotly.graph_objects as go

x1, y1 = [1,2,3], [1,2,3]
x2, y2 = [2,4,9], [6,5,8]
x3, y3 = [6,8,9], [2,1,5]

xs = [x1, x2, x3]
ys = [y1, y2, y3]
symbols = ["circle-dot", "square", "triangle-up"]
names = ["open-dot", "open", "full"]
fill_colors = ["white", "white", "blue"]
line_colors = ["red", "green", "blue"]

fig = go.Figure()

for x, y, fill_color, line_color, symbol, name in zip(xs, ys, fill_colors, line_colors, symbols, names):
    
    fig.add_scatter(
        x=x,
        y=y,
        mode="markers+lines",
        marker=dict(
            color=fill_color, 
            size=20, 
            symbol=symbol,
            line=dict(
                width=2,
                color=line_color
            )
        ),
        line=dict(
            width=2,
            color=line_color
        ),
        name=name,
        showlegend=True
    )
    
fig.update_layout(
    template="simple_white", 
    width=800, 
    height=400,
)

fig.show()
1 Like