# Marginal\_y/x hover data in px.scatter broken when used with hovertemplate

**URL:** <https://community.plotly.com/t/marginal-y-x-hover-data-in-px-scatter-broken-when-used-with-hovertemplate/38408>\
**Category:** 📊 Plotly Python\
**Created:** [April 25, 2020, 10:46pm UTC](https://community.plotly.com/t/marginal-y-x-hover-data-in-px-scatter-broken-when-used-with-hovertemplate/38408 "2020-04-25T22:46:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![adamschroeder](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/adamschroeder/32/17280_2.png) [@adamschroeder](https://community.plotly.com/u/adamschroeder)\
**Post date:** [April 25, 2020, 10:46pm UTC](https://community.plotly.com/t/marginal-y-x-hover-data-in-px-scatter-broken-when-used-with-hovertemplate/38408/1 "2020-04-25T22:46:22Z")

</div>

Hi All,  
The hover data on the right gray box plot is broken when the scatter plot code includes certain aspects of hovertemplate.

```auto
fig.update_traces(hovertemplate=.....)

```

Here’s the [data](https://drive.google.com/file/d/1mZSURA-YgrRqWqQCtFL2NFWUuBP68ZrT/view?usp=sharing) and the code to replicate:

```auto
import pandas as pd #(version 1.0.0)
import plotly #(version 4.5.4)
import plotly.express as px
import plotly.io as pio

df = pd.read_csv("final_covid.csv")
df.dropna(subset=['women'], inplace=True)
df['total_employed'] = pd.to_numeric(df['total_employed'])
df['total_employed'] = df['total_employed']*1000

fig = px.scatter(
    data_frame=df,
    x="proximity",
    y="exposure",
    size="total_employed",
    color="Majority Employed",
    opacity=0.8,
    color_discrete_map={"Women":"blue","Men":"gray"},
    custom_data=['occupation'],

    marginal_x='box',
    marginal_y='box',

    title='Workers at Highest Risk of Coronavirus',
    template='ggplot2',
)

fig.update_traces(hovertemplate=
        "<b>%{customdata}</b><br><br>" +
        "Exposure: %{y}<br>" +
        "Proximity: %{x}<br>" +
        "Total Employed: %{marker.size:,}" +
        "<extra></extra>",
)

fig.update_layout(legend=dict(x=.8, y=1))
pio.show(fig)

```

**As you can see, `%{y}` and `%{x}` work fine, but not `%{customdata}` or `%{marker.size:,}`.**

Maybe we can fix this in the next update.

Thank you,  
Adam

---

<div class="post-metadata">

**Author:** ![Emmanuelle](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/emmanuelle/32/7853_2.png) [@Emmanuelle](https://community.plotly.com/u/Emmanuelle)\
**Post date:** [April 26, 2020, 8:49am UTC](https://community.plotly.com/t/marginal-y-x-hover-data-in-px-scatter-broken-when-used-with-hovertemplate/38408/2 "2020-04-26T08:49:50Z")

</div>

Hi @adamschroeder, this is not really a bug here, because what you should do here is to pass the columns to the `hover_data` argument of `px.scatter`, and then the columns are passed to traces in the marginals too. If you do this you will have all the information in the hover of the different subplots. without the need to change the hovertemplate. If you wish to customize the hovertemplate of course you can as in the code below; I’ve used the `selector` attribute of `fig.update_traces` in order to select a different hovertemplate for the scatter and box plots (if you had outliers in the `marginal_x` you would need to use a different selector, such as the `xaxis` and `yaxis`)

```auto
import pandas as pd #(version 1.0.0)
import plotly #(version 4.5.4)
import plotly.express as px
import plotly.io as pio

df = pd.read_csv("~/Downloads/final_covid.csv")
df.dropna(subset=['women'], inplace=True)
df['total_employed'] = pd.to_numeric(df['total_employed'])
df['total_employed'] = df['total_employed']*1000

fig = px.scatter(
    data_frame=df,
    x="proximity",
    y="exposure",
    size="total_employed",
    color="Majority Employed",
    opacity=0.8,
    color_discrete_map={"Women":"blue","Men":"gray"},
    hover_data=['occupation', 'total_employed'],

    marginal_x='box',
    marginal_y='box',

    title='Workers at Highest Risk of Coronavirus',
    template='ggplot2',
)

fig.update_traces(hovertemplate=
        "<b>%{customdata[0]}</b><br><br>" +
        "Exposure: %{y}<br>" +
        "Proximity: %{x}<br>" +
        "Total Employed: %{customdata[1]}" +
        "<extra></extra>",
                  selector={'type':'scatter'}
)
fig.update_traces(hovertemplate=
        "<b>%{customdata[0]}</b><br><br>" +
        "Exposure: %{y}<br>" +
        "Total Employed: %{customdata[1]}" +
        "<extra></extra>",
                  selector={'type':'box'}
)

fig.update_layout(legend=dict(x=.8, y=1))
fig.show()

```

---

<div class="post-metadata">

**Author:** ![Emmanuelle](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/emmanuelle/32/7853_2.png) [@Emmanuelle](https://community.plotly.com/u/Emmanuelle)\
**Post date:** [April 26, 2020, 8:51am UTC](https://community.plotly.com/t/marginal-y-x-hover-data-in-px-scatter-broken-when-used-with-hovertemplate/38408/3 "2020-04-26T08:51:06Z")

</div>

Of course feel free to suggest improvements to the documentation if you think additional examples or explanations could help.

---

<div class="post-metadata">

**Author:** ![adamschroeder](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/adamschroeder/32/17280_2.png) [@adamschroeder](https://community.plotly.com/u/adamschroeder)\
**Post date:** [April 26, 2020, 2:41pm UTC](https://community.plotly.com/t/marginal-y-x-hover-data-in-px-scatter-broken-when-used-with-hovertemplate/38408/4 "2020-04-26T14:41:55Z")

</div>

> [@Emmanuelle](#):
>
> over\_data=[‘occupation’, ‘total\_employed’],

Hi @Emmanuelle,  
thank you for the clarification. My mistake was to use `custom_data` within the px.scatter(). Using `hover_data` makes it work. I used your code (copy-paste) and it works great.

However, when I add the rug plot to the marginal x instead of the original box plot I had there before:

```auto
marginal_x='rug',

```

And I add the following trace to your code

```auto
scatterplot.update_traces(hovertemplate=
        "<b>%{customdata[0]}</b><br><br>" +
        "Proximity: %{x}<br>" +
        "Total Employed: %{customdata[1]:,}" +
        "<extra></extra>",
        selector={'type':'rug'}
)

```

**I get an error, in the hover, where the Exposure=men or women instead of the xaxis proximity value**.

Do you know why that is?

---

<div class="post-metadata">

**Author:** ![Emmanuelle](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/emmanuelle/32/7853_2.png) [@Emmanuelle](https://community.plotly.com/u/Emmanuelle)\
**Post date:** [April 26, 2020, 3:55pm UTC](https://community.plotly.com/t/marginal-y-x-hover-data-in-px-scatter-broken-when-used-with-hovertemplate/38408/5 "2020-04-26T15:55:33Z")

</div>

It’s because the rug plot is made with a `box` trace so the `selector` which you are using is not used for the rug plot and instead the one of the box plot is used. (You can do a `print(fig)` to check the content of the px-generated figure).  
You should instead select according to the axis like

```auto
fig.update_traces(hovertemplate=
        "<b>%{customdata[0]}</b><br><br>" +
        "Proximity: %{x}<br>" +
        "Total Employed: %{customdata[1]:,}" +
        "<extra></extra>",
        selector={'xaxis':'x3', 'yaxis':'y3'}
)

```

---

<div class="post-metadata">

**Author:** ![adamschroeder](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/adamschroeder/32/17280_2.png) [@adamschroeder](https://community.plotly.com/u/adamschroeder)\
**Post date:** [April 26, 2020, 4:00pm UTC](https://community.plotly.com/t/marginal-y-x-hover-data-in-px-scatter-broken-when-used-with-hovertemplate/38408/6 "2020-04-26T16:00:54Z")

</div>

That’s what you meant with the xaxis and yaxis in your first post 😁 .  
Thank you for your help @Emmanuelle . I appreciate all your help.

---

<div class="post-metadata">

**Author:** ![Emmanuelle](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/emmanuelle/32/7853_2.png) [@Emmanuelle](https://community.plotly.com/u/Emmanuelle)\
**Post date:** [April 26, 2020, 4:01pm UTC](https://community.plotly.com/t/marginal-y-x-hover-data-in-px-scatter-broken-when-used-with-hovertemplate/38408/7 "2020-04-26T16:01:31Z")

</div>

Always a pleasure to help super-users 😉 !
