python sns boxplot code example

Example 1: seaborn orient

>>> import seaborn as sns
>>> sns.set_theme(style="whitegrid")
>>> tips = sns.load_dataset("tips")
>>> ax = sns.boxplot(x=tips["total_bill"])

Example 2: python how to make boxplots with pandas and seaborn

# Load packages:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# Import data:
data_url = 'http://bit.ly/2cLzoxH'
gapminder = pd.read_csv(data_url) # Read data from url as pandas dataframe
print(gapminder.head(3)) # View top 3 rows

# Filter data:
gapminder_2007 = gapminder[gapminder['year']==2007]
gapminder_2007.head(3) # Veiw top 3 rows after filtering
# Note how data is organized in pandas dataframe and how it gets called
#	in the following boxplot

# Example usage 1 (simple boxplot): 
bplot = sns.boxplot(y='lifeExp', x='continent', 
                 data=gapminder_2007, 
                 width=0.5,
                 palette="colorblind")

# Example usage 2 (boxplot with stripplot overlay):
bplot = sns.boxplot(y='lifeExp', x='continent', 
                 data=gapminder_2007, 
                 width=0.5,
                 palette="colorblind")

bplot = sns.stripplot(y='lifeExp', x='continent', 
                   data=gapminder_2007, 
                   jitter=True, 
                   marker='o', 
                   alpha=0.5,
                   color='black')

# Example usage 3 (boxplot with swarmplot overlay):
bplot = sns.boxplot(y='lifeExp', x='continent', 
                 data=gapminder_2007, 
                 width=0.5,
                 palette="colorblind")
 
bplot = sns.swarmplot(y='lifeExp', x='continent',
              data=gapminder_2007, 
              color='black',
              alpha=0.75)