Plotly: How to set marker symbol shapes for multiple traces using plotly express?

We Are Going To Discuss About Plotly: How to set marker symbol shapes for multiple traces using plotly express?. So lets Start this Python Article.

Plotly: How to set marker symbol shapes for multiple traces using plotly express?

  1. How to solve Plotly: How to set marker symbol shapes for multiple traces using plotly express?

    The answer:
    fig = px.scatter(df, x='Date', y='Metric1', color = 'Color', hover_data = ["Color", "Marker"], symbol = df['Marker'], symbol_sequence= ['circle-open', 'circle', 'circle-open-dot', 'square'], color_discrete_sequence = ['blue', 'orange', 'green', 'brown'])
    Some details:
    Using plotly express, all you have to do is specify:
    symbol = df['Marker'] # or simply symbol = 'Marker' if df is specified in px.scatter
    …where marker is not the marker type but rather a value that separates the data categories from eachother. Much like in your example with:
    11 4 12 1 13 2 14 3 15 4
    This will give you:

    If you're not happy with the designated symbols, you can assign your own through:
    symbols = ['square', 'circle-dot', 'circle', 'circle-open'] fig.px.scatter([...], symbol_sequence = symbols)
    You probably already know that you can find a complete symbols list on plotly.com/python/marker-style/. And to replicate your original figure, only improved with a fully working legend, all you have to do to assign your colors of choice is:
    colors = ['blue', 'orange', 'green', 'brown'] px.scatter([...], color_discrete_sequence = colors)
    Plot:

    Complete code:
    import pandas as pd import plotly.express as px d = {'Date': ['01/01/1990','01/01/1990','01/01/1990','01/01/1990', '01/01/2000','01/01/2000','01/01/2000','01/01/2000', '01/01/2010','01/01/2010','01/01/2010','01/01/2010', '01/01/2020','01/01/2020','01/01/2020','01/01/2020' ]} df = pd.DataFrame(data=d) df['Metric1']=[100, 110, 120, 130, 200, 210, 220, 230, 300, 310, 320, 330, 400, 410, 420, 430] df['Marker']=[1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4] df['Color']=['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'] symbols = ['circle-open', 'circle', 'circle-open-dot', 'square'] colors = ['blue', 'orange', 'green', 'brown'] fig = px.scatter(df, x='Date', y='Metric1', color = 'Color', hover_data = ["Color", "Marker"], symbol = df['Marker'], symbol_sequence=symbols, color_discrete_sequence = colors ) fig.show()

  2. Plotly: How to set marker symbol shapes for multiple traces using plotly express?

    The answer:
    fig = px.scatter(df, x='Date', y='Metric1', color = 'Color', hover_data = ["Color", "Marker"], symbol = df['Marker'], symbol_sequence= ['circle-open', 'circle', 'circle-open-dot', 'square'], color_discrete_sequence = ['blue', 'orange', 'green', 'brown'])
    Some details:
    Using plotly express, all you have to do is specify:
    symbol = df['Marker'] # or simply symbol = 'Marker' if df is specified in px.scatter
    …where marker is not the marker type but rather a value that separates the data categories from eachother. Much like in your example with:
    11 4 12 1 13 2 14 3 15 4
    This will give you:

    If you're not happy with the designated symbols, you can assign your own through:
    symbols = ['square', 'circle-dot', 'circle', 'circle-open'] fig.px.scatter([...], symbol_sequence = symbols)
    You probably already know that you can find a complete symbols list on plotly.com/python/marker-style/. And to replicate your original figure, only improved with a fully working legend, all you have to do to assign your colors of choice is:
    colors = ['blue', 'orange', 'green', 'brown'] px.scatter([...], color_discrete_sequence = colors)
    Plot:

    Complete code:
    import pandas as pd import plotly.express as px d = {'Date': ['01/01/1990','01/01/1990','01/01/1990','01/01/1990', '01/01/2000','01/01/2000','01/01/2000','01/01/2000', '01/01/2010','01/01/2010','01/01/2010','01/01/2010', '01/01/2020','01/01/2020','01/01/2020','01/01/2020' ]} df = pd.DataFrame(data=d) df['Metric1']=[100, 110, 120, 130, 200, 210, 220, 230, 300, 310, 320, 330, 400, 410, 420, 430] df['Marker']=[1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4] df['Color']=['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'] symbols = ['circle-open', 'circle', 'circle-open-dot', 'square'] colors = ['blue', 'orange', 'green', 'brown'] fig = px.scatter(df, x='Date', y='Metric1', color = 'Color', hover_data = ["Color", "Marker"], symbol = df['Marker'], symbol_sequence=symbols, color_discrete_sequence = colors ) fig.show()

Solution 1

The answer:

fig = px.scatter(df, x='Date', y='Metric1',
                 color = 'Color', hover_data = ["Color", "Marker"],
                 symbol = df['Marker'],
                 symbol_sequence= ['circle-open', 'circle', 'circle-open-dot', 'square'],
                 color_discrete_sequence = ['blue', 'orange', 'green', 'brown'])

Some details:

Using plotly express, all you have to do is specify:

symbol = df['Marker']
# or simply symbol = 'Marker' if df is specified in px.scatter

…where marker is not the marker type but rather a value that separates the data categories from eachother. Much like in your example with:

11    4
12    1
13    2
14    3
15    4

This will give you:

enter image description here

If you’re not happy with the designated symbols, you can assign your own through:

symbols = ['square', 'circle-dot', 'circle', 'circle-open']
fig.px.scatter([...], symbol_sequence = symbols)

You probably already know that you can find a complete symbols list on plotly.com/python/marker-style/. And to replicate your original figure, only improved with a fully working legend, all you have to do to assign your colors of choice is:

colors = ['blue', 'orange', 'green', 'brown']
px.scatter([...], color_discrete_sequence = colors)

Plot:

enter image description here

Complete code:

import pandas as pd
import plotly.express as px

d = {'Date': ['01/01/1990','01/01/1990','01/01/1990','01/01/1990',
             '01/01/2000','01/01/2000','01/01/2000','01/01/2000',
             '01/01/2010','01/01/2010','01/01/2010','01/01/2010',
             '01/01/2020','01/01/2020','01/01/2020','01/01/2020'
             ]}
df = pd.DataFrame(data=d)

df['Metric1']=[100, 110, 120, 130,
                 200, 210, 220, 230,
                 300, 310, 320, 330,
                 400, 410, 420, 430]
df['Marker']=[1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4]
df['Color']=['a', 'b', 'c', 'd',
             'a', 'b', 'c', 'd',
             'a', 'b', 'c', 'd',
             'a', 'b', 'c', 'd']

symbols = ['circle-open', 'circle', 'circle-open-dot', 'square']
colors = ['blue', 'orange', 'green', 'brown']

fig = px.scatter(df, x='Date', y='Metric1', color = 'Color', hover_data = ["Color", "Marker"],
                 symbol = df['Marker'],
                 symbol_sequence=symbols,
                 color_discrete_sequence = colors
                )
fig.show()

Original Author vestland Of This Content

Solution 2

I was able to figure it out-

import plotly.graph_objects as go
from plotly.validators.scatter.marker import SymbolValidator
import plotly.offline as pyo
vals = SymbolValidator().values
def Setcolor (x):
    if x == "a":
        return "blue"
    elif x == "b":
        return "orange"
    elif x == "c":
        return "green"
    else:
        return "brown"

def Setshape (x):
        vals = SymbolValidator().values
        return vals[3*x]
plot1 = go.Scatter(
                    x=df['Date'], y=df['Metric1'], 
                    marker = dict(color=list(map(Setcolor, df['Color'])), symbol = list(map(Setshape, df['Marker']))),
                    mode='markers',name='Show1', showlegend = True
                   )

fig = go.Figure(data=[plot1])

pyo.plot(fig,filename='final_plot.html')

Now my plot is not showing a legend. I am pretty sure it’s an easy fix, but can’t figure out what I am missing!

Original Author edited Jan 29, 2021 at 8:17 Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment