Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly box plot turn off outlier detection

In Plotly (Python), box plots detect outlier by default, and if there are what it decides to be outliers, the whiskers are not extended to the outliers. However, I know that none of my data points should be treated as outliers. Is it possible to turn off outlier detection in box plots, and have the whole dataset treated as inliers?

By the way, I still want to show all of the points next to the box plots, so I don't want to use the option boxpoints=False to force the box plot to include all points.

like image 946
Ray Avatar asked Oct 29 '25 06:10

Ray


1 Answers

It seems that the only way to do this at the time being is to use mutliple traces and adjust them to the same position like the plot and snippet below will show. If you'd like some details, take a look at the snippets and plots at the end.

In the following snippet, I'm using go.Box(x=x0) for two different traces with the same data but different settings for the markers and lines to achieve this:

Plot:

enter image description here

Code:

# imports
import plotly
from plotly import tools
import pandas as pd
import numpy as np
import plotly.graph_objs as go

# setup
np.random.seed(123)

# data
y0 = np.random.randn(50)-1
x0 = y0
x0 = [0 for y in y0]

# include an outlier
y0[-1] = 4

# traces
trace0 = go.Box(x=x0,
                y=y0, boxpoints = False, pointpos = 0,
                marker = dict(color = 'rgb(66, 167, 244)'),
)

trace1 = go.Box(x=x0,
                y=y0, boxpoints = 'all', pointpos = 0,
                marker = dict(color = 'rgb(66, 66, 244)'),
                line = dict(color = 'rgba(0,0,0,0)'),
                fillcolor = 'rgba(0,0,0,0)'
)

data=[trace0, trace1]

# figure
fig = go.Figure(data)
fig.show()

Details about the default behaviour:

If Boxpoints are not specifed, the lines will not include the outlier:

Plot: Default

enter image description here

Code:

# imports
import plotly
from plotly import tools
import pandas as pd
import numpy as np
import plotly.graph_objs as go

# setup
np.random.seed(123)

# data
y0 = np.random.randn(50)-1
y0[-1] = 4

# traces
trace0 = go.Box(y=y0, pointpos = 0,
                marker = dict(color = 'rgb(66, 167, 244)'),

)

# figure
fig = go.Figure(trace0)
fig.show()

The only way you can make the lines inlcude the outlier, is to remove all boxpoints by setting boxpoints = False

Plot:

enter image description here


Code:

# imports
import plotly
from plotly import tools
import pandas as pd
import numpy as np
import plotly.graph_objs as go

# setup
np.random.seed(123)

# data
y0 = np.random.randn(50)-1
y0[-1] = 4

# traces
trace0 = go.Box(y=y0, pointpos = 0,
                marker = dict(color = 'rgb(66, 167, 244)'),
                boxpoints = False
)

# figure
fig = go.Figure(trace0)
fig.show()

And of course, this is not what you're aiming to do.

I hope this was helpful. If not, then don't hesitate to let me know.

like image 67
vestland Avatar answered Oct 31 '25 18:10

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!