Can I create a bar trace that goes only up to the lowest point in px.scatter?

Viewed 20

I would like to have a line going up to the lowest scatter point.

enter image description here

For instance, below the point ("ANO3", "EM PRODUÇÃO") there would be a line or bar.

Can this be done in plotly?

1 Answers

Here's one very felxible approach:

fig.add_trace(
    go.Bar(
        x=[df_min['gdpPercap'].iloc[0]],
        y=[df_min['lifeExp'].iloc[0]],
        marker=dict(line=dict(width=4, color = 'blue'))
    ))

enter image description here

Code with sample data

import plotly.express as px
import plotly.graph_objects as go
import pandas as pd

fig = go.Figure()
df = px.data.gapminder().query("year == 2007 & continent == 'Europe'")
df_min = df[df['lifeExp'] == df['lifeExp'].min()]
fig = px.scatter(df, x="gdpPercap", y="lifeExp", hover_name="country")

fig.add_trace(
    go.Bar(
        x=[df_min['gdpPercap'].iloc[0]],
        y=[df_min['lifeExp'].iloc[0]],
        marker=dict(line=dict(width=4, color = 'blue'))
    ))
fig.update_layout(yaxis_range=[70,80])

fig.show()
Related