Plotly box plot turn off outlier detection - python

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.

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:
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
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:
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.

Related

Plotly scatter not drawing line of markers above certain number of data points

I am using Plotly's scatter. I want to have lines surrounding the markers, like in this plot (the black contour):
I want this to happen by default, so I am setting a template like in the below MWE:
import plotly.express as px
import plotly.io as pio
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import numpy as np
MARKERS = ['circle', 'cross', 'x', 'triangle-up', 'star', 'hexagram', 'square', 'diamond', 'hourglass', 'bowtie', 'pentagon', 'triangle-down', 'triangle-left', 'triangle-right', 'star-triangle-up', 'star-triangle-down', 'star-square', 'star-diamond', 'diamond-tall', 'diamond-wide', 'triangle-ne', 'triangle-se', 'triangle-sw', 'triangle-nw', 'hexagon', 'hexagon2', 'octagon']
my_template = pio.templates['plotly']
my_template.data.scatter = [
go.Scatter(
marker = dict(
symbol = s,
line = dict(
width = .5,
),
),
error_y = dict(
width = 1,
thickness = .8
)
) for s in MARKERS
]
pio.templates['my_template'] = my_template
pio.templates.default = 'my_template'
import numpy
import pandas
N_SAMPLES = 99 # Set to 9999 and it fails.
fig = px.scatter(
pandas.DataFrame(
{
'x': numpy.random.randn(N_SAMPLES),
'y': numpy.random.exponential(size=N_SAMPLES),
}
),
x = "x",
y = "y",
)
fig.show()
This works perfectly but if the number of points goes beyond certain value, it stops drawing the lines, like this:
This is what happens to me when I change N_SAMPLES to e.g. 9999. How can I get it to work independently of the number of points?
I have Python 3.8.10 and Plotly 5.11.0.
To cope with the large amount of data, WebGL is available, so I used it to draw a scatterplot with blue markers and a line width of 1. N number is 100,000.
Update:
To set the line width of a marker by default, create a dedicated template and set the line width as its content.
import plotly.graph_objects as go
import numpy as np
my_template = go.layout.Template()
my_template.data.scattergl = [go.Scattergl(marker=dict(line_width=0.5))]
N = 100000
fig = go.Figure()
fig.update_layout(template=my_template)
fig.add_trace(go.Scattergl(
x = np.random.randn(N),
y = np.random.exponential(size=N),
mode='markers',
# marker=dict(
# color='blue',
# line_width=1
# )
))
fig.show()

How do I resize my Plotly bar height and show only bar’s edge (in subplot)?

this is my first foray into Plotly. I love the ease of use compared to matplotlib and bokeh. However I'm stuck on some basic questions on how to beautify my plot. First, this is the code below (its fully functional, just copy and paste!):
import plotly.express as px
from plotly.subplots import make_subplots
import plotly as py
import pandas as pd
from plotly import tools
d = {'Mkt_cd': ['Mkt1','Mkt2','Mkt3','Mkt4','Mkt5','Mkt1','Mkt2','Mkt3','Mkt4','Mkt5'],
'Category': ['Apple','Orange','Grape','Mango','Orange','Mango','Apple','Grape','Apple','Orange'],
'CategoryKey': ['Mkt1Apple','Mkt2Orange','Mkt3Grape','Mkt4Mango','Mkt5Orange','Mkt1Mango','Mkt2Apple','Mkt3Grape','Mkt4Apple','Mkt5Orange'],
'Current': [15,9,20,10,20,8,10,21,18,14],
'Goal': [50,35,21,44,20,24,14,29,28,19]
}
dataset = pd.DataFrame(d)
grouped = dataset.groupby('Category', as_index=False).sum()
data = grouped.to_dict(orient='list')
v_cat = grouped['Category'].tolist()
v_current = grouped['Current']
v_goal = grouped['Goal']
fig1 = px.bar(dataset, x = v_current, y = v_cat, orientation = 'h',
color_discrete_sequence = ["#ff0000"],height=10)
fig2 = px.bar(dataset, x = v_goal, y = v_cat, orientation = 'h',height=15)
trace1 = fig1['data'][0]
trace2 = fig2['data'][0]
fig = make_subplots(rows = 1, cols = 1, shared_xaxes=True, shared_yaxes=True)
fig.add_trace(trace2, 1, 1)
fig.add_trace(trace1, 1, 1)
fig.update_layout(barmode = 'overlay')
fig.show()
Here is the Output:
Question1: how do I make the width of v_current (shown in red bar) smaller? As in, it should be smaller in height since this is a horizontal bar. I added the height as 10 for trace1 and 15 for trace2, but they are still showing at the same heights.
Question2: Is there a way to make the v_goal (shown in blue bar) only show it's right edge, instead of a filled out bar? Something like this:
If you noticed, I also added a line under each of the category. Is there a quick way to add this as well? Not a deal breaker, just a bonus. Other things I'm trying to do is add animation, etc but that's for some other time!
Thanks in advance for answering!
Running plotly.express wil return a plotly.graph_objs._figure.Figure object. The same goes for plotly.graph_objects running go.Figure() together with, for example, go.Bar(). So after building a figure using plotly express, you can add lines or traces through references directly to the figure, like:
fig['data'][0].width = 0.4
Which is exactly what you need to set the width of your bars. And you can easily use this in combination with plotly express:
Code 1
fig = px.bar(grouped, y='Category', x = ['Current'],
orientation = 'h', barmode='overlay', opacity = 1,
color_discrete_sequence = px.colors.qualitative.Plotly[1:])
fig['data'][0].width = 0.4
Plot 1
In order to get the bars or shapes to indicate the goal levels, you can use the approach described by DerekO, or you can use:
for i, g in enumerate(grouped.Goal):
fig.add_shape(type="rect",
x0=g+1, y0=grouped.Category[i], x1=g, y1=grouped.Category[i],
line=dict(color='#636EFA', width = 28))
Complete code:
import plotly.express as px
from plotly.subplots import make_subplots
import plotly as py
import pandas as pd
from plotly import tools
d = {'Mkt_cd': ['Mkt1','Mkt2','Mkt3','Mkt4','Mkt5','Mkt1','Mkt2','Mkt3','Mkt4','Mkt5'],
'Category': ['Apple','Orange','Grape','Mango','Orange','Mango','Apple','Grape','Apple','Orange'],
'CategoryKey': ['Mkt1Apple','Mkt2Orange','Mkt3Grape','Mkt4Mango','Mkt5Orange','Mkt1Mango','Mkt2Apple','Mkt3Grape','Mkt4Apple','Mkt5Orange'],
'Current': [15,9,20,10,20,8,10,21,18,14],
'Goal': [50,35,21,44,20,24,14,29,28,19]
}
dataset = pd.DataFrame(d)
grouped = dataset.groupby('Category', as_index=False).sum()
fig = px.bar(grouped, y='Category', x = ['Current'],
orientation = 'h', barmode='overlay', opacity = 1,
color_discrete_sequence = px.colors.qualitative.Plotly[1:])
fig['data'][0].width = 0.4
fig['data'][0].marker.line.width = 0
for i, g in enumerate(grouped.Goal):
fig.add_shape(type="rect",
x0=g+1, y0=grouped.Category[i], x1=g, y1=grouped.Category[i],
line=dict(color='#636EFA', width = 28))
f = fig.full_figure_for_development(warn=False)
fig.show()
You can use Plotly Express and then directly access the figure object as #vestland described, but personally I prefer to use graph_objects to make all of the changes in one place.
I'll also point out that since you are stacking bars in one chart, you don't need subplots. You can create a graph_object with fig = go.Figure() and add traces to get stacked bars, similar to what you already did.
For question 1, if you are using go.Bar(), you can pass a width parameter. However, this is in units of the position axis, and since your y-axis is categorical, width=1 will fill the entire category, so I have chosen width=0.25 for the red bar, and width=0.3 (slightly larger) for the blue bar since that seems like it was your intention.
For question 2, the only thing that comes to mind is a hack. Split the bars into two sections (one with height = original height - 1), and set its opacity to 0 so that it is transparent. Then place down bars of height 1 on top of the transparent bars.
If you don't want the traces to show up in the legend, you can set this individually for each bar by passing showlegend=False to fig.add_trace, or hide the legend entirely by passing showlegend=False to the fig.update_layout method.
import plotly.express as px
import plotly.graph_objects as go
# from plotly.subplots import make_subplots
import plotly as py
import pandas as pd
from plotly import tools
d = {'Mkt_cd': ['Mkt1','Mkt2','Mkt3','Mkt4','Mkt5','Mkt1','Mkt2','Mkt3','Mkt4','Mkt5'],
'Category': ['Apple','Orange','Grape','Mango','Orange','Mango','Apple','Grape','Apple','Orange'],
'CategoryKey': ['Mkt1Apple','Mkt2Orange','Mkt3Grape','Mkt4Mango','Mkt5Orange','Mkt1Mango','Mkt2Apple','Mkt3Grape','Mkt4Apple','Mkt5Orange'],
'Current': [15,9,20,10,20,8,10,21,18,14],
'Goal': [50,35,21,44,20,24,14,29,28,19]
}
dataset = pd.DataFrame(d)
grouped = dataset.groupby('Category', as_index=False).sum()
data = grouped.to_dict(orient='list')
v_cat = grouped['Category'].tolist()
v_current = grouped['Current']
v_goal = grouped['Goal']
fig = go.Figure()
## you have a categorical plot and the units for width are in position axis units
## therefore width = 1 will take up the entire allotted space
## a width value of less than 1 will be the fraction of the allotted space
fig.add_trace(go.Bar(
x=v_current,
y=v_cat,
marker_color="#ff0000",
orientation='h',
width=0.25
))
## you can show the right edge of the bar by splitting it into two bars
## with the majority of the bar being transparent (opacity set to 0)
fig.add_trace(go.Bar(
x=v_goal-1,
y=v_cat,
marker_color="#ffffff",
opacity=0,
orientation='h',
width=0.30,
))
fig.add_trace(go.Bar(
x=[1]*len(v_cat),
y=v_cat,
marker_color="#1f77b4",
orientation='h',
width=0.30,
))
fig.update_layout(barmode='relative')
fig.show()

How to make animated 3D scatter plot in plotly

My goal is to create an animation with my 3D data in plotly.
I have 3 variables x,y,z for simplicity and I plot the 4th value depending on these x,y,z.
I create a 3D scatter plot where the 4th dim sort to speak is the color like this:
from numpy import genfromtxt
import numpy as np
import plotly.io as pio
import plotly.express as px
pio.renderers.default = 'notebook'
import plotly.graph_objects as go
import math
import pandas as pd
data = pd.read_csv("paramtp_1e-05_big.txt")
data.head()
data = data.iloc[::10, :]
color_data = data['gopt'].astype(float).round(decimals=2)
color_data[color_data>= 10] = 10
color_data_nopt = data['nopt'].astype(float).round(decimals=3)
color_data_mc = data['mc'].astype(float).round(decimals=3)
color_data_P= data['P']
color_data_P[color_data_P >= 1] = 1
data= data.replace(np.nan, '', regex=True)
data.tail()
fig = px.scatter_3d(data, x='NpN0', y='s', z='mu',log_x=True, log_z=True,
opacity = 0.5,
color=color_data,color_continuous_scale=px.colors.sequential.Viridis)
fig.add_trace(
go.Scatter(
mode='markers',
marker=dict(
size=1,
opacity=0.5,
),
)
)
fig.show()
Similarly to this wonderful animation: https://plotly.com/python/visualizing-mri-volume-slices/
I would like to slice up my data to isosurfaces with respect to any x,y,z coordinates.
As in the example they use images, I could not wrap my head around to create the same with my raw data.
Thank you in advance.

Dumbbell plots in python with plotly [duplicate]

I want to create a lollipop plot with several horizontal line segments like this - https://python-graph-gallery.com/184-lollipop-plot-with-2-group. I'd like to use plotly since I prefer the graphics (and easy interactivity) but can't find a succint way.
There's both line graphs (https://plot.ly/python/line-charts/) and you can add lines in the layout (https://plot.ly/python/shapes/#vertical-and-horizontal-lines-positioned-relative-to-the-axes), but both of these solutions require each line segment to be added separately, with about 4-8 lines of code each. While I could just for-loop this, would appreciate if anyone can point me to anything with inbuilt vectorization, like the matplotlib solution (first link)!
Edit: Also tried the following code, to first make the plot ala matplotlib, then convert to plotly. The line segments disappear in the process. Starting to think it's just impossible.
mpl_fig = plt.figure()
# make matplotlib plot - WITH HLINES
plt.rcParams['figure.figsize'] = [5,5]
ax = mpl_fig.add_subplot(111)
ax.hlines(y=my_range, xmin=ordered_df['value1'], xmax=ordered_df['value2'],
color='grey', alpha=0.4)
ax.scatter(ordered_df['value1'], my_range, color='skyblue', alpha=1,
label='value1')
ax.scatter(ordered_df['value2'], my_range, color='green', alpha=0.4 ,
label='value2')
ax.legend()
# convert to plotly
plotly_fig = tls.mpl_to_plotly(mpl_fig)
plotly_fig['layout']['xaxis1']['showgrid'] = True
plotly_fig['layout']['xaxis1']['autorange'] = True
plotly_fig['layout']['yaxis1']['showgrid'] = True
plotly_fig['layout']['yaxis1']['autorange'] = True
# plot: hlines disappear :/
iplot(plotly_fig)
You can use None in the data like this:
import plotly.offline as pyo
import plotly.graph_objs as go
fig = go.Figure()
x = [1, 4, None, 2, 3, None, 3, 4]
y = [0, 0, None, 1, 1, None, 2, 2]
fig.add_trace(
go.Scatter(x=x, y=y))
pyo.plot(fig)
Plotly doesn't provide a built in vectorization for such chart, because it can be done easily by yourself, see my example based on your provided links:
import pandas as pd
import numpy as np
import plotly.offline as pyo
import plotly.graph_objs as go
# Create a dataframe
value1 = np.random.uniform(size = 20)
value2 = value1 + np.random.uniform(size = 20) / 4
df = pd.DataFrame({'group':list(map(chr, range(65, 85))), 'value1':value1 , 'value2':value2 })
my_range=range(1,len(df.index)+1)
# Add title and axis names
data1 = go.Scatter(
x=df['value1'],
y=np.array(my_range),
mode='markers',
marker=dict(color='blue')
)
data2 = go.Scatter(
x=df['value2'],
y=np.array(my_range),
mode='markers',
marker=dict(color='green')
)
# Horizontal line shape
shapes=[dict(
type='line',
x0 = df['value1'].loc[i],
y0 = i + 1,
x1 = df['value2'].loc[i],
y1 = i + 1,
line = dict(
color = 'grey',
width = 2
)
) for i in range(len(df['value1']))]
layout = go.Layout(
shapes = shapes,
title='Lollipop Chart'
)
# Plot the chart
fig = go.Figure([data1, data2], layout)
pyo.plot(fig)
With the result I got:

Plotly: How do I remove vertical lines that appear after introducing gaps in annotated heatmap?

When I introduce gaps in between bricks in a plotly annotated heatmap, vertical black lines appear behind the bricks (visible in the gaps). The lines appear to line up with the x-axis labels. Even more oddly, if the x-axis category is numeric, the label "0" will not get a vertical line. I want the vertical lines removed. I've looked at the documentation and can't figure out what these lines are. You'll notice that there are also horizontal vertical and white lines that line up with the x- and y-axis labels. I don't mind those.
import plotly.graph_objs as go
from plotly.figure_factory import create_annotated_heatmap
import numpy as np
fig = go.Figure(create_annotated_heatmap(z = np.arange(12).reshape(3,4),
x = [0,1,2,3],
y = ['A','B','C'],
xgap = 30, ygap = 30
)
)
fig.update_layout(title = 'What are these vertical lines?')
fig.show()
This is not an issue with the standard heatmap:
fig2 = go.Figure(go.Heatmap(z = np.arange(12).reshape(3,4),
x = [0,1,2,3],
y = ['A','B','C'],
xgap = 30, ygap = 30
)
)
fig2.update_layout(title = 'No vertical lines here.')
fig2.show()
Regarding the documentation from help(create_annotated_heatmap), there is a short list of parameters that don't seem to have anything to do with it, and kwargs that go through the standard plotly Heatmap.
The line under the zero is the 'zeroline' while the other lines are the 'gridlines'. They can be removed by setting zeroline=False and showgrid=False in the figure layout.
import plotly.graph_objs as go
from plotly.figure_factory import create_annotated_heatmap
import numpy as np
fig = go.Figure(create_annotated_heatmap(z=np.arange(12).reshape(3,4),
x=[0,1,2,3],
y=['A','B','C'],
xgap=30, ygap=30))
fig.update_layout(xaxis=dict(zeroline=False, showgrid=False),
yaxis=dict(zeroline=False, showgrid=False))
fig.show()
Alternatively, you can change their color to white as in the standard heatmap.
import plotly.graph_objs as go
from plotly.figure_factory import create_annotated_heatmap
import numpy as np
fig = go.Figure(create_annotated_heatmap(z=np.arange(12).reshape(3,4),
x=[0,1,2,3],
y=['A','B','C'],
xgap=30, ygap=30))
fig.update_layout(xaxis=dict(zeroline=False, gridcolor='white'),
yaxis=dict(zeroline=False, gridcolor='white'))
fig.show()

Categories

Resources