简体   繁体   中英

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

I have been trying to change the marker shape in plotly scatter plot but I can't seem to find the correct options. The plotly document goes over the size and opacity, but not the marker shape. Here's my sample 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']
fig = px.scatter(df, x='Date', y='Metric1', color = 'Color', hover_data = ["Color", "Marker"])
fig.show()

Is it possible to implement what I am trying to do with Plotly scatter plot?I tried the follow the discussion in this forum- https://community.plotly.com/t/getting-different-markers-shapes/9944 But I am having a difficult time relating it to my problem I am using Python 3.8 and Plotly 4.11.0

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM