0% found this document useful (0 votes)
2 views

Preprocessing1.ipynb - Colab

The document is a Jupyter notebook that demonstrates various types of boxplots and violin plots using the Matplotlib library in Python. It includes code snippets for creating simple, black and white, horizontal, filled, and custom color boxplots, as well as violin plots, with explanations of the components of boxplots. Additionally, it provides installation instructions for the watermark package and displays the current environment's Python and library versions.

Uploaded by

Rumi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Preprocessing1.ipynb - Colab

The document is a Jupyter notebook that demonstrates various types of boxplots and violin plots using the Matplotlib library in Python. It includes code snippets for creating simple, black and white, horizontal, filled, and custom color boxplots, as well as violin plots, with explanations of the components of boxplots. Additionally, it provides installation instructions for the watermark package and displays the current environment's Python and library versions.

Uploaded by

Rumi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1/5/25, 12:09 AM Preprocessing1.

ipynb - Colab

!pip install watermark

Collecting watermark
Downloading watermark-2.5.0-py2.py3-none-any.whl.metadata (1.4 kB)
Requirement already satisfied: ipython>=6.0 in /usr/local/lib/python3.10/dist-packages (from watermark) (7.34.0)
Requirement already satisfied: importlib-metadata>=1.4 in /usr/local/lib/python3.10/dist-packages (from watermark) (8.5.0)
Requirement already satisfied: setuptools in /usr/local/lib/python3.10/dist-packages (from watermark) (75.1.0)
Requirement already satisfied: zipp>=3.20 in /usr/local/lib/python3.10/dist-packages (from importlib-metadata>=1.4->watermark) (3.21
Collecting jedi>=0.16 (from ipython>=6.0->watermark)
Downloading jedi-0.19.2-py2.py3-none-any.whl.metadata (22 kB)
Requirement already satisfied: decorator in /usr/local/lib/python3.10/dist-packages (from ipython>=6.0->watermark) (4.4.2)
Requirement already satisfied: pickleshare in /usr/local/lib/python3.10/dist-packages (from ipython>=6.0->watermark) (0.7.5)
Requirement already satisfied: traitlets>=4.2 in /usr/local/lib/python3.10/dist-packages (from ipython>=6.0->watermark) (5.7.1)
Requirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from ipython
Requirement already satisfied: pygments in /usr/local/lib/python3.10/dist-packages (from ipython>=6.0->watermark) (2.18.0)
Requirement already satisfied: backcall in /usr/local/lib/python3.10/dist-packages (from ipython>=6.0->watermark) (0.2.0)
Requirement already satisfied: matplotlib-inline in /usr/local/lib/python3.10/dist-packages (from ipython>=6.0->watermark) (0.1.7)
Requirement already satisfied: pexpect>4.3 in /usr/local/lib/python3.10/dist-packages (from ipython>=6.0->watermark) (4.9.0)
Requirement already satisfied: parso<0.9.0,>=0.8.4 in /usr/local/lib/python3.10/dist-packages (from jedi>=0.16->ipython>=6.0->waterm
Requirement already satisfied: ptyprocess>=0.5 in /usr/local/lib/python3.10/dist-packages (from pexpect>4.3->ipython>=6.0->watermark
Requirement already satisfied: wcwidth in /usr/local/lib/python3.10/dist-packages (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0
Downloading watermark-2.5.0-py2.py3-none-any.whl (7.7 kB)
Downloading jedi-0.19.2-py2.py3-none-any.whl (1.6 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.6/1.6 MB 20.5 MB/s eta 0:00:00
Installing collected packages: jedi, watermark
Successfully installed jedi-0.19.2 watermark-2.5.0

%load_ext watermark

%watermark -u -v -d -p matplotlib,numpy

Last updated: 2024-12-27

Python implementation: CPython


Python version : 3.10.12
IPython version : 7.34.0

matplotlib: 3.8.0
numpy : 1.26.4

More info about the %watermark extension

%matplotlib inline

Boxplots in matplotlib

keyboard_arrow_down Sections
Simple Boxplot

Black and white Boxplot

Horizontal Boxplot

Filled and cylindrical boxplots

Boxplots with custom fill colors

Violin plots

https://colab.research.google.com/drive/1Q5fiq-P-0FtUFLMVr4emtHGQ3RpEH-gP#printMode=true 1/6
1/5/25, 12:09 AM Preprocessing1.ipynb - Colab

keyboard_arrow_down Simple Boxplot

import matplotlib.pyplot as plt


import numpy as np

all_data = [np.random.normal(0, std, 100) for std in range(5, 8)]

fig = plt.figure(figsize=(4,3))

plt.boxplot(all_data,
notch=False, # box instead of notch shape
sym='rs', # red squares for outliers
vert=True) # vertical box aligmnent

plt.xticks([y+1 for y in range(len(all_data))], ['x1', 'x2', 'x3'])


plt.xlabel('measurement x')
plt.title('Box plot')
plt.show()

https://colab.research.google.com/drive/1Q5fiq-P-0FtUFLMVr4emtHGQ3RpEH-gP#printMode=true 2/6
1/5/25, 12:09 AM Preprocessing1.ipynb - Colab

Median: The middle value of the dataset.

Quartiles: Indicate the spread of the middle 50% of the data.

Whiskers: Extend to show the range of the data, excluding outliers.

Outliers: Data points that fall outside the whiskers, marked as red squares ('rs').

keyboard_arrow_down Black and white Boxplot


import matplotlib.pyplot as plt
import numpy as np

all_data = [np.random.normal(0, std, 100) for std in range(1, 4)]

fig = plt.figure(figsize=(8,6))

bplot = plt.boxplot(all_data,
notch=False, # box instead of notch shape
sym='rs', # red squares for outliers
vert=True) # vertical box aligmnent

plt.xticks([y+1 for y in range(len(all_data))], ['x1', 'x2', 'x3'])


plt.xlabel('measurement x')
print(bplot.keys())
for components in bplot.keys():
for line in bplot[components]:
line.set_color('black') # black lines

t = plt.title('Black and white box plot')

plt.show()

Show hidden output

keyboard_arrow_down Horizontal Boxplot


import matplotlib.pyplot as plt
import numpy as np

all_data = [np.random.normal(0, std, 100) for std in range(1, 4)]

fig = plt.figure(figsize=(8,6))

plt.boxplot(all_data,
notch=False, # box instead of notch shape
sym='rs', # red squares for outliers
vert=False) # horizontal box aligmnent

plt.yticks([y+1 for y in range(len(all_data))], ['x1', 'x2', 'x3'])


plt.ylabel('measurement x')

https://colab.research.google.com/drive/1Q5fiq-P-0FtUFLMVr4emtHGQ3RpEH-gP#printMode=true 3/6
1/5/25, 12:09 AM Preprocessing1.ipynb - Colab
t = plt.title('Horizontal Box plot')
plt.show()

keyboard_arrow_down Filled and cylindrical boxplots


import matplotlib.pyplot as plt
import numpy as np

all_data = [np.random.normal(0, std, 100) for std in range(1, 4)]

fig = plt.figure(figsize=(8,6))

plt.boxplot(all_data,
notch=True, # notch shape
sym='bs', # blue squares for outliers
vert=True, # vertical box aligmnent
patch_artist=True) # fill with color

plt.xticks([y+1 for y in range(len(all_data))], ['x1', 'x2', 'x3'])


plt.xlabel('measurement x')
t = plt.title('Box plot')
plt.show()

https://colab.research.google.com/drive/1Q5fiq-P-0FtUFLMVr4emtHGQ3RpEH-gP#printMode=true 4/6
1/5/25, 12:09 AM Preprocessing1.ipynb - Colab

keyboard_arrow_down Boxplots with custom fill colors


import matplotlib.pyplot as plt
import numpy as np

all_data = [np.random.normal(0, std, 100) for std in range(1, 4)]

fig = plt.figure(figsize=(8,6))

bplot = plt.boxplot(all_data,
notch=False, # notch shape
vert=True, # vertical box aligmnent
patch_artist=True) # fill with color

colors = ['pink', 'lightblue', 'lightgreen']


for patch, color in zip(bplot['boxes'], colors):
patch.set_facecolor(color)

plt.xticks([y+1 for y in range(len(all_data))], ['x1', 'x2', 'x3'])


plt.xlabel('measurement x')
t = plt.title('Box plot')
plt.show()

https://colab.research.google.com/drive/1Q5fiq-P-0FtUFLMVr4emtHGQ3RpEH-gP#printMode=true 5/6
1/5/25, 12:09 AM Preprocessing1.ipynb - Colab

keyboard_arrow_down Violin plots


Violin plots are closely related to Tukey's (1977) box plots but add useful information such as the distribution of the sample data (density trace).

Violin plots were added in matplotlib 1.4.

import matplotlib.pyplot as plt


import numpy as np

fig, axes = plt.subplots(nrows=1,ncols=2, figsize=(12,5))

all_data = [np.random.normal(0, std, 100) for std in range(6, 10)]

#fig = plt.figure(figsize=(8,6))

axes[0].violinplot(all_data,
showmeans=False,
showmedians=True
)
axes[0].set_title('violin plot')

axes[1].boxplot(all_data,
)
axes[1].set_title('box plot')

# adding horizontal grid lines


for ax in axes:
ax.yaxis.grid(True)
ax.set_xticks([y+1 for y in range(len(all_data))], )
ax.set_xlabel('xlabel')
ax.set_ylabel('ylabel')

plt.setp(axes, xticks=[y+1 for y in range(len(all_data))],


xticklabels=['x1', 'x2', 'x3', 'x4'],
)

plt.show()

https://colab.research.google.com/drive/1Q5fiq-P-0FtUFLMVr4emtHGQ3RpEH-gP#printMode=true 6/6

You might also like