Last Updated : 15 Jul, 2025
Boxplot is used to see the distribution of numerical data and identify key stats like minimum and maximum values, median, identifying outliers, understanding how data is distributed and can compare the distribution of data across different categories or variables. In Seaborn the seaborn.boxplot() function is used to plot it and in this article we will learn about it.
Lets see a example: We will use the tips dataset which is an inbuilt dataset. This dataset contains information about restaurant tips, total bill amount, tip amount, customer details like sex and day of the week etc. Also we will be using Seaborn and Matplotlib libraries for this.
Python
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset("tips")
sns.boxplot(x="day", y="tip", data=df)
plt.show()
Output:
Syntax:
seaborn.boxplot(x=None, y=None, hue=None, data=None, color=None, palette=None, linewidth=None,**kwargs)
Parameters:
Returns: It returns Axes object with the plot drawn on it.
Example 1: Horizontal Boxplot of Total BillBy changing the axis to x, we can plot distribution of the total bill in a horizontal format. This makes it easy to view data horizontally.
Python
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset("tips")
sns.boxplot(x=df["total_bill"])
plt.show()
Output:
Horizontal boxplot Example 2: Boxplot with HueWe will use hue parameter to color-code the boxplots based on the smoker status. This makes it easier to get a difference between smokers and non-smokers.
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset("tips")
sns.boxplot(x="day", y="total_bill", hue="smoker", data=df)
plt.show()
Output:
Boxplot with Hue Example 3: Custom Colors PaletteWe use hue and palette parameters to color-code the boxplot based on gender. This helps in making the difference between male and female customers. We will define a custom color palette as skyblue and lightpink for male and female respectively.
Python
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset("tips")
palette = {'Male': 'skyblue', 'Female': 'lightpink'}
sns.boxplot(x="day", y="tip", hue="sex", data=df, palette=palette)
plt.show()
Output:
Custom Colors with Palette Example 4: Increase Outline ThicknessThe linewidth parameter controls thickness of the boxplot lines. By increasing it plot’s boundaries become more thick.
Python
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset("tips")
sns.boxplot(x="day", y="tip", data=df, linewidth=2)
plt.show()
Output:
Boxplot with linewidth=2 Example 5: Horizontal Boxplot for Multiple ColumnsIn this example we plot multiple variables horizontally by setting the orient parameter to "h". This helps in comparing distributions of multiple numerical columns.
Python
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset("tips")
sns.boxplot(data=df[["total_bill", "tip", "size"]], orient="h")
plt.show()
Output:
Horizontal Boxplot for Multiple Columns Example 6: Set Single ColorWe can use color parameter to set a single color for the entire boxplot which ensures a uniform color.
Python
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset("tips")
sns.boxplot(x="day", y="tip", data=df, color="green")
plt.show()
Output:
Single ColorWith Seaborn's boxplot() we can easily visualize and compare data distributions which helps us to gain valuable insights into our dataset in a clear and effective manner.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4