Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to highlight a single data point on a scatter plot using plotly express

In a scatter plot created using px.scatter, how do I mark one data point with a red star?

fig = px.scatter(df, x="sepal_width", y="sepal_length")
# Now set a single data point to color="red", symbol="star".
like image 487
HappyLifelongLearner Avatar asked Apr 21 '26 13:04

HappyLifelongLearner


1 Answers

This isn't really highlighting an already existing data point within a trace you've already produced, but rather adding another one with a different visual appearance. But it does exactly what you're looking for:

fig.add_trace(go.Scatter(x=[3.5], y=[6.5], mode = 'markers',
                         marker_symbol = 'star',
                         marker_size = 15))

Plot:

enter image description here

Complete code:

import plotly.express as px
import pandas as pd
import plotly.graph_objects as go
df = px.data.iris() # iris is a pandas DataFrame
fig = px.scatter(df, x="sepal_width", y="sepal_length")
fig.add_trace(go.Scatter(x=[3.5], y=[6.5], mode = 'markers',
                         marker_symbol = 'star',
                         marker_size = 15))
fig.show()
like image 105
vestland Avatar answered Apr 23 '26 03:04

vestland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!