melolilili
melolilili

Reputation: 227

How to set marker symbol using plotly express?

I want to label tumor samples with red square marker and normal samples with green circle.

My code generates circle symbols for both tumor and marker. How do I label the markers correctly?

import plotly.express as px

X_meth_kipan = meth_450_10k_kipan.iloc[:,9:-1].dropna(axis=1)

pca = PCA(n_components=2)
components = pca.fit_transform(X_meth_kipan)

fig = px.scatter(components, x=0, y=1, color=meth_450_10k_kipan["type"], color_discrete_sequence=["red", "green"], symbol_sequence=["square", "circle-open-dot"], width=600, height=600)
fig.update_xaxes(automargin=True)
fig.update_yaxes(automargin=True)
fig.update_layout({'plot_bgcolor': 'rgb(240,240,240)','paper_bgcolor': 'rgb(240,240,240)',})
fig.show()

enter image description here

Upvotes: 3

Views: 1695

Answers (1)

Baron Legendre
Baron Legendre

Reputation: 2188

First you specify symbol for the column you want, then symbol_map to map your marker.

As you can see, plotly apply the same rule to color.

import plotly.express as px
df = px.data.iris()
fig = px.scatter(df,
                 x="sepal_width",
                 y="sepal_length",

                 # color
                 color="species",
                 color_discrete_map={"setosa": "rgb(0,0,255)", "versicolor": "rgb(255,0,0)", "virginica": "rgb(0,255,0)"},
                 
                 # symbol
                 symbol="species",
                 symbol_map={"setosa": "circle", "versicolor": "triangle-up", "virginica": "diamond"},

                 width=800,
                 height=800
                 )

fig.show()

enter image description here

Upvotes: 4

Related Questions