2

I have been trying all day to add max value annotation to a plotly plot. I figured out how to do it manually but I am more interested in a more automatic solution. I added my code in but omitted anything unrelated.

Is there a way to add a label to the max points of my plotly scatterplot?

P.S. Please be nice this is my first posting here.

The relevant part of the code:

import plotly.express as px
import pandas as pd

m = df['values'].max()

day_order = ['Monday',
             'Tuesday',
             'Wednesday',
             'Thursday',
             'Friday',
             'Saturday',
             'Sunday']

fig = px.scatter(df,
                 x='day-of-week',
                 y ='values',
                 category_orders = {
                     'day-of-week': day_order})

fig.add_annotation(x='Sunday', 
                   y=m,
                   text=m)
5
  • 1
    Trying all day, but no code to show for your efforts? Please share the attempt that you were most convinced about that it would work, and explain what the problem is. stackoverflow.com/help/how-to-ask Commented Oct 6, 2021 at 23:34
  • Thanks for the suggestion! I added some code, again my annotation method works but I would like to know if there is a more streamlined solution. Commented Oct 6, 2021 at 23:54
  • It is also possible to specify the day of the week and the maximum value on the y-axis. It is also possible to specify the day of the week and the maximum value on the y-axis, but the one shown below specifies the first day of the week when there is more than one maximum value, so that needs to be considered. my_chart.add_annotation(x=df[df['values']==max(df['values'])]['day-of-week'].head(1),y=max(df['values']),text=max(df['values'])) Commented Oct 7, 2021 at 0:48
  • Hi @vvv welcome to SO! You should try to add a sample of your df. It will be great if you can have a look at How to Ask and then try to produce a minimal reproducible example. Commented Oct 7, 2021 at 2:45
  • I edited your code. If you are using pandas it is better to use df['values'].max() instead of max(df['values']) Commented Oct 7, 2021 at 2:51

2 Answers 2

3

I generate a data sample for you.

Data

import plotly.express as px
import pandas as pd
import numpy as np

n = 14
df = pd.DataFrame(
    {"date": pd.date_range(
        start=pd.Timestamp.today().date(),
        periods=n),
     "values": np.arange(n)})

# date to day-of-week
df["day-of-week"] = df["date"].dt.day_name()

day_order = ['Monday',
             'Tuesday',
             'Wednesday',
             'Thursday',
             'Friday',
             'Saturday',
             'Sunday']

m = df["values"].max()

Plot

fig = px.scatter(df,
                 x='day-of-week',
                 y ='values',
                 category_orders = {
                     'day-of-week': day_order})

fig.add_annotation(x=df.iloc[df["values"].idxmax()]["day-of-week"], 
                   y=m,
                   text=str(m))

In the last part is important that text is a str

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

Used data

enter image description here

Code: find max value and day(s) with max value and add their annotations

max_value = df['values'].max()
max_days = df[df['values'] == max_value]['day-of-week'].values

day_order = ['Monday',
             'Tuesday',
             'Wednesday',
             'Thursday',
             'Friday',
             'Saturday',
             'Sunday']

fig = px.scatter(df,
                 x='day-of-week',
                 y ='values',
                 category_orders = {
                     'day-of-week': day_order})

for day in max_days:
  fig.add_annotation(x=day,
                    y=max_value,
                    text=max_value)

fig.show()

enter image description here

1 Comment

Thanks this is perfect!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.