Search code examples
python-3.xplotlyscatter-plot

[ScatterPlot][Potly][Python] How to have marker of heart shape in scatter plot


enter image description here I was looking into the documents but I didn't find a marker with shape of heart. Is it possible to have markers in scatter plot(plotly) to have markers of the shape of heart as shown in below image?

Thanks in advance


Solution

  • A simple way is to use unicode symbols https://www.w3schools.com/charsets/ref_utf_symbols.asp

    To color, pass an array/list to textfont_color

    import plotly.graph_objects as go
    import numpy as np
    
    go.Figure(
        go.Scatter(
            x=np.random.randint(1, 5, 10),
            y=np.random.randint(1, 5, 10),
            mode="text",
            text="♥",
            textfont_size=20,
            textfont_color=np.random.choice(["red","yellow","blue"], 10)
        )
    )
    

    enter image description here

    with borders

    import plotly.graph_objects as go
    import numpy as np
    
    x = np.random.randint(1, 5, 10)
    y = np.random.randint(1, 5, 10)
    
    fig = go.Figure(
        [
            go.Scatter(
                x=x,
                y=y,
                mode="text",
                text="♥",
                textfont_size=80,
                textfont_color="white",
                textposition="middle center",
            ),
            go.Scatter(
                x=x,
                y=y,
                mode="text",
                text="♥",
                textfont_size=40,
                textfont_color=np.random.choice(["red", "yellow", "blue"], 10),
                textposition="middle center",
            ),
        ]
    )