✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚡️ Concerned about the grid? Kyle Baranko teaches how to predicting peak loads using XGBoost. Register for the August webinar!

Strange Boxplots with Plotly Express Scatter

Hello everyone,

I had no trouble with Plotly until this incident.
For some of my plots, whenever I use Plotly express scatter plot with box plots on axes the strange boxplots in the first image happens.
Also, when I plot them using Plotly express box plot, it seems that there is no problem as can be seen in 2nd and 3rd images.
I attached the minimal code to reproduce it.

I hope if someone can explain to me what is going wrong and how to fix it.
Thanks!

# Imports
import plotly.express as px
import pandas as pd

# Minimal data
x = [0, 17.5, 35, 52.5, 70, 87.5, 105, 122.5]
y = [16.10142557, 27.15387483, 31.48277778, 47.98240741, 123.2, 44.97916667, 61.06666667, 122.37222222]

df = pd.DataFrame({'x':x, 'y':y})

fig = px.scatter(df, x='x', y='y', marginal_x='box', marginal_y='box', trendline='ols')

# Tried different combinations of them to see if anything changes
fig.update_layout(showlegend=False)
fig.update_yaxes(automargin=True)
fig.update_xaxes(automargin=True)

fig.show()

# Boxplot for x
fig = px.box(df, y='x')
fig.show()

# Boxplot for y
fig = px.box(df, y='y')
fig.show()



@bilal2

You get such a plot because in Box definition it is set notched=True (I found this setting printing fig.data). To get box plots like in your last two figures, update boxes as follows:

fig.update_traces(notched=False, selector=dict(type='box'))

To get information on notched boxplots print:

help(go.Box.notched):

Help on property:

Determines whether or not notches are drawn. Notches displays a
confidence interval around the median. We compute the
confidence interval as median +/- 1.57 * IQR / sqrt(N), where
IQR is the interquartile range and N is the sample size. If two
boxes' notches do not overlap there is 95% confidence their
medians differ. See
https://sites.google.com/site/davidsstatistics/home/notched-
box-plots for more info. Defaults to False unless `notchwidth`
or `notchspan` is set.

The 'notched' property must be specified as a bool
(either True, or False)

Returns
-------
bool

Hi @empet ,
I think this solves my problem.
Thanks for the help and the explanation.