Kernel: Python 3 (ipykernel)
--- title: Palmer Penguins ---
In [1]:
import pandas as pd import altair as alt import seaborn as sns from matplotlib import pyplot as plt
Data from Palmer Penguins R package
In [2]:
penguins = pd.read_csv("https://pos.it/palmer-penguins-github-csv")
In [3]:
#| label: species-counts penguins.groupby("species").size().reset_index(name = "count")
Out[3]:
In [4]:
colors = ["#FF8C00", "#A020F0", "#008B8B"] sns.set_palette(colors, n_colors = 3)
In [5]:
#| fig-alt: A density plot of bill ratio by species. penguins["bill_ratio"] = ( penguins["bill_length_mm"] / penguins["bill_depth_mm"] ) sns.displot(penguins, x = "bill_ratio", hue = "species", kind = "kde", fill = True, aspect = 2, height = 3) plt.show()
Out[5]:
In [6]:
#| label: fig-bill-marginal #| fig-cap: Marginal distributions of bill dimensions #| fig-subcap: #| - Gentoo penguins tend to have thinner bills, #| - and Adelie penguins tend to have shorter bills. #| fig-alt: #| - Density plot of bill depth by species. #| - Density plot of bill length by species. #| layout-ncol: 2 sns.displot(penguins, x = "bill_depth_mm", hue = "species", kind = "kde", fill = True, aspect = 2, height = 3) plt.show() sns.displot(penguins, x = "bill_length_mm", hue = "species", kind = "kde", fill = True, aspect = 2, height = 3) plt.show()
Out[6]:
In [7]:
scale = alt.Scale(domain = ['Adelie', 'Chinstrap', 'Gentoo'], range = colors)
In [8]:
#| label: fig-bill-scatter #| fig-cap: > #| A scatterplot of bill dimensions for #| penguins, made with Altair. alt.Chart(penguins).mark_circle(size=60).encode( alt.X('bill_length_mm', scale=alt.Scale(zero=False) ), alt.Y('bill_depth_mm', scale=alt.Scale(zero=False) ), color = alt.Color('species', scale = scale), tooltip=['species', 'sex', 'island'] )
Out[8]: