import pandas as pd
import plotnine as p9
from plotnine import ggplot, geom_point, aes, stat_smooth, facet_wrap, labs
from palmerpenguins import load_penguins
1 Set up
Install Python on your machine.
Install Quarto
Go to cmd and type
py --version
py -m pip install pandas plotly statsmodels
py -m pip install palmerpenguins
py -m pip install plotnine
Note: The reason for typing
py -m
instead of justpip
install is to avoid issues where you might have multiple versions of Python installed, and pip could be tied to the wrong version. By runningpy -m pip
, you’re explicitly telling the Python launcher to use the right Python interpreter and its associated pip.
2 Loading packages
3 Data
= load_penguins()
penguins penguins.head()
species | island | bill_length_mm | bill_depth_mm | flipper_length_mm | body_mass_g | sex | year | |
---|---|---|---|---|---|---|---|---|
0 | Adelie | Torgersen | 39.1 | 18.7 | 181.0 | 3750.0 | male | 2007 |
1 | Adelie | Torgersen | 39.5 | 17.4 | 186.0 | 3800.0 | female | 2007 |
2 | Adelie | Torgersen | 40.3 | 18.0 | 195.0 | 3250.0 | female | 2007 |
3 | Adelie | Torgersen | NaN | NaN | NaN | NaN | NaN | 2007 |
4 | Adelie | Torgersen | 36.7 | 19.3 | 193.0 | 3450.0 | female | 2007 |
4 Data Visualisation with plotnine
4.1 Step 1: Take data and obtain a canvas for plotting
ggplot(penguins)
4.2 Step 2: Defining Aesthetics
"bill_length_mm", "bill_depth_mm", color="factor(species)")) ggplot(penguins, aes(
4.3 Step 3: Add Gemetries
"bill_length_mm", "bill_depth_mm", color="factor(species)")) + geom_point() ggplot(penguins, aes(
C:\Users\DELL\AppData\Local\Programs\Python\Python312\Lib\site-packages\plotnine\layer.py:364: PlotnineWarning: geom_point : Removed 2 rows containing missing values.
4.4 Step 4: Create the plot with customized axis labels
"bill_length_mm", "bill_depth_mm", color="factor(species)")) + geom_point() + labs(x="Bill Length (mm)", y="Bill Depth (mm)") ggplot(penguins, aes(
C:\Users\DELL\AppData\Local\Programs\Python\Python312\Lib\site-packages\plotnine\layer.py:364: PlotnineWarning: geom_point : Removed 2 rows containing missing values.
5 Change themes
5.1 Without customized themes
=penguins,
p9.ggplot(data=p9.aes(x='factor(species)')) + p9.geom_bar() mapping
5.2 With customized themes
=penguins,
p9.ggplot(data=p9.aes(x='factor(species)')) + p9.geom_bar() + p9.theme_bw() + p9.theme(axis_text_x = p9.element_text(angle=90)) mapping
6 Faceting
6.1 Without faceting
"bill_length_mm", "bill_depth_mm", color="factor(species)")) + geom_point() + labs(x="Bill Length (mm)", y="Bill Depth (mm)") ggplot(penguins, aes(
C:\Users\DELL\AppData\Local\Programs\Python\Python312\Lib\site-packages\plotnine\layer.py:364: PlotnineWarning: geom_point : Removed 2 rows containing missing values.
6.2 With faceting
"bill_length_mm", "bill_depth_mm", color="factor(species)")) + geom_point() + labs(x="Bill Length (mm)", y="Bill Depth (mm)") + facet_wrap("species") ggplot(penguins, aes(
C:\Users\DELL\AppData\Local\Programs\Python\Python312\Lib\site-packages\plotnine\layer.py:364: PlotnineWarning: geom_point : Removed 2 rows containing missing values.
7 Other geoms
7.1 geom_boxplot
=penguins,
p9.ggplot(data=p9.aes(x='factor(species)', y='bill_length_mm')) + p9.geom_boxplot() mapping
C:\Users\DELL\AppData\Local\Programs\Python\Python312\Lib\site-packages\plotnine\layer.py:284: PlotnineWarning: stat_boxplot : Removed 2 rows containing non-finite values.
7.2 geom_jitter
=penguins,
p9.ggplot(data=p9.aes(x='factor(species)', y='bill_length_mm')) + p9.geom_jitter() mapping
C:\Users\DELL\AppData\Local\Programs\Python\Python312\Lib\site-packages\plotnine\layer.py:364: PlotnineWarning: geom_jitter : Removed 2 rows containing missing values.
7.3 geom_boxplot + geom_jitter
=penguins,
p9.ggplot(data=p9.aes(x='factor(species)', y='bill_length_mm', color='factor(species)')) + p9.geom_boxplot()+ p9.geom_jitter() mapping
C:\Users\DELL\AppData\Local\Programs\Python\Python312\Lib\site-packages\plotnine\layer.py:284: PlotnineWarning: stat_boxplot : Removed 2 rows containing non-finite values.
C:\Users\DELL\AppData\Local\Programs\Python\Python312\Lib\site-packages\plotnine\layer.py:364: PlotnineWarning: geom_jitter : Removed 2 rows containing missing values.
7.4 To remove outliers from the boxplot in your ggplot code
=penguins,
p9.ggplot(data=p9.aes(x='factor(species)', y='bill_length_mm', color='factor(species)')) + p9.geom_boxplot(outlier_shape = "None")+ p9.geom_jitter() mapping
C:\Users\DELL\AppData\Local\Programs\Python\Python312\Lib\site-packages\plotnine\layer.py:284: PlotnineWarning: stat_boxplot : Removed 2 rows containing non-finite values.
C:\Users\DELL\AppData\Local\Programs\Python\Python312\Lib\site-packages\plotnine\layer.py:364: PlotnineWarning: geom_jitter : Removed 2 rows containing missing values.
8 Working with chunk options
8.1 echo: false
C:\Users\DELL\AppData\Local\Programs\Python\Python312\Lib\site-packages\plotnine\layer.py:284: PlotnineWarning: stat_boxplot : Removed 2 rows containing non-finite values.
C:\Users\DELL\AppData\Local\Programs\Python\Python312\Lib\site-packages\plotnine\layer.py:364: PlotnineWarning: geom_jitter : Removed 2 rows containing missing values.
8.2 fig-cap
C:\Users\DELL\AppData\Local\Programs\Python\Python312\Lib\site-packages\plotnine\layer.py:284: PlotnineWarning: stat_boxplot : Removed 2 rows containing non-finite values.
C:\Users\DELL\AppData\Local\Programs\Python\Python312\Lib\site-packages\plotnine\layer.py:364: PlotnineWarning: geom_jitter : Removed 2 rows containing missing values.
8.3 fig width
C:\Users\DELL\AppData\Local\Programs\Python\Python312\Lib\site-packages\plotnine\layer.py:284: PlotnineWarning: stat_boxplot : Removed 2 rows containing non-finite values.
C:\Users\DELL\AppData\Local\Programs\Python\Python312\Lib\site-packages\plotnine\layer.py:364: PlotnineWarning: geom_jitter : Removed 2 rows containing missing values.
8.4 Suprress warnings and messages
8.5 Fenced Echo
```{python}
1 + 1
```
2
1 + 1
2
8.6 output
and code-overflow
```{python}
#| output: false
#| code-overflow: wrap
1 + 1
```
1 + 1
9 Highlighting
1 + 1
10 Themes
format:
html:
theme: united
format:
html:
theme: cosmo
fontsize: 1.1em
linestretch: 1.7
format:
html:
theme: darkly
11 Folding Code
Code
=penguins,
p9.ggplot(data=p9.aes(x='factor(species)', y='bill_length_mm', color='factor(species)')) + p9.geom_boxplot() mapping
C:\Users\DELL\AppData\Local\Programs\Python\Python312\Lib\site-packages\plotnine\layer.py:284: PlotnineWarning: stat_boxplot : Removed 2 rows containing non-finite values.
12 Folding Code with code-summary
Show the code
=penguins,
p9.ggplot(data=p9.aes(x='factor(species)', y='bill_length_mm', color='factor(species)')) + p9.geom_boxplot() mapping
C:\Users\DELL\AppData\Local\Programs\Python\Python312\Lib\site-packages\plotnine\layer.py:284: PlotnineWarning: stat_boxplot : Removed 2 rows containing non-finite values.
13 Code-overflow
=penguins,
p9.ggplot(data=p9.aes(x='factor(species)', y='bill_length_mm', color='factor(species)')) + p9.geom_boxplot() mapping
C:\Users\DELL\AppData\Local\Programs\Python\Python312\Lib\site-packages\plotnine\layer.py:284: PlotnineWarning: stat_boxplot : Removed 2 rows containing non-finite values.
14 Table of content
toc: true-depth: 2 toc
16 Word document output
format:
docx:-sections: true
number-offset: 2
number-depth: 3
number-style: github highlight