boxplot python code example

Example 1: python boxplot show mean

import matplotlib.pyplot as plt
import numpy as np

data_to_plot = np.random.rand(100,5)

fig = plt.figure(1, figsize=(9, 6))
ax = fig.add_subplot(111)    
bp = ax.boxplot(data_to_plot, showmeans=True)

plt.show()

Example 2: python matplotlib boxplot

import numpy as np
import matplotlib.pyplot as plt

# Fixing random state for reproducibility
np.random.seed(19680801)

# fake up some data
spread = np.random.rand(50) * 100
center = np.ones(25) * 50
flier_high = np.random.rand(10) * 100 + 100
flier_low = np.random.rand(10) * -100
data = np.concatenate((spread, center, flier_high, flier_low))

fig1, ax1 = plt.subplots()
ax1.set_title('Basic Plot')
ax1.boxplot(data)

Example 3: boxplot python

import numpy as np
import matplotlib.pyplot
matrix=np.random.rand(10,10)
plt.boxplot(matrix)

Example 4: sns.boxplot code

sns.boxplot("var")

Example 5: boxplot code

sns.boxplot("var")

Example 6: 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)

Tags:

R Example