Box Plot using Plotly in Python - GeeksforGeeks

20 Sep, 2021
Plotly is a Python library which is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library.
A box plot is a demographic representation of numerical data through their quartiles. The end and upper quartiles are represented in box,  while the median (second quartile) is notable by a line inside the box. Plotly.express is convenient,high-ranked interface to plotly which operates on variet of data and produce a easy-to-style figure.Box are much beneficial for comparing the groups of data. Box plot divide approx. 25% of section data into sets which helps ion quickly identifying  values, the dispersion of the data set, and signs of skewness.
Syntax: plotly.express.box(data_frame=None, x=None, y=None, color=None, facet_row=None, facet_col=None, facet_col_wrap=0, hover_name=None, hover_data=None, custom_data=None, animation_frame=None, animation_group=None, category_orders={}, labels={}, color_discrete_sequence=None, color_discrete_map={}, orientation=None, boxmode=None, log_x=False, log_y=False, range_x=None, range_y=None, points=None, notched=False, title=None, template=None, width=None, height=None)
Parameters:
Example 1: Using Iris Dataset
Python3
import plotly.express as px df = px.data.iris() fig = px.box(df, x="sepal_width", y="sepal_length") fig.show()
Output:
Example 2: Using Tips Dataset
Python3
import plotly.express as px df = px.data.tips() fig = px.box(df, x = "sex", y="total_bill")fig.show()
Output: 
 
In the above examples, let’s take the first box plot of the figure and understand these statistical things:
Bottom horizontal line of box plot is minimum value
First horizontal line of rectangle shape of box plot is First quartile or 25%
Second horizontal line of rectangle shape of box plot is Second quartile or 50% or median.
Third horizontal line of rectangle shape of box plot is third quartile or 75%
Top horizontal line of rectangle shape of box plot is maximum value.
Small diamond shape of blue box plot is outlier data or erroneous data.
The algorithm to choose quartiles can also be selected in plotly. It is computed by using linear algorithm by default. However, it provides two more algorithms for doing the same i.e. inclusive and exclusive.
Example 1: Using inclusive algorithm
Python3
import plotly.express as px df = px.data.tips() fig = px.box(df, x = "sex", y="total_bill", points="all")fig.update_traces(quartilemethod="inclusive") fig.show()
Output:
Example 2: Using exclusive algorithm
Python3
import plotly.express as px df = px.data.tips() fig = px.box(df, x = "sex", y="total_bill", points="all")fig.update_traces(quartilemethod="exclusive") fig.show()
Output:
Underlying data can be shows using the points arguments. The value of this argument can be of three types – 
all for all points
outliers for outliers only
false for none of the above
Example 1: Passing all as argument
Python3
import plotly.express as px df = px.data.tips() fig = px.box(df, x = "sex", y="total_bill", points="all")fig.show()
Output:
Example 2:
Python3
import plotly.express as px df = px.data.tips() fig = px.box(df, x = "sex", y="total_bill", points="outliers")fig.show()
Output: 
Boxplot comes with various styling options. Let’s see one such option in the below example.
Example:
Python3
import plotly.express as px df = px.data.tips() fig = px.box(df, x = "sex", y="total_bill", points="all", notched=True)fig.update_traces(quartilemethod="exclusive") fig.show()
Output:
sooda367
akshaysingh98088
Python-Plotly
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe
Python Dictionary
Taking input in Python
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python