import matplotlib.pyplot as plt # this gives a "matlab"-like interface to matplotlib
import pandas as pd
import numpy as np
= pd.read_csv("data/penguins-raw.csv")
penguins = penguins.drop("Comments", axis=1)
penguins
penguins.dropna()
= np.linspace(-5, 5)
x = x**2 y
Plotting (in Python)
Fundamentals of Data Science
The main tool for plotting in R is ggplot
, which we have talked about a little and which will be covered in detail in other courses.
The purpose of this lesson is to talk about tools for plotting in Python. Here the situation is more diverse, and there are a number of different plotting packages with different capabilities. The most notable ones are:
matplotlib
the most fundamental package. Very capable but old-fashioned and the output isn’t of the highest graphical quality.bokeh
which offers high quality visuals and interactivity.
altair
which is a package that has similar syntax toggplot
and follows the Wilkinson “Grammar of Graphics” philosophyplotly
which offers even more opportunities for interactivityseaborn
which has very high quality visuals and is very common in publications especially in genomics.
We’ll discuss matplotlib
, bokeh
, and seaborn
and you can explore the others on your own to see what you like best.
We’ll work with the penguins
dataset.
The matplotlib
package is organized around figures
and axes
. Essentially, a set of axes
is a single graph, and a figure
is a collection of axes organized into a single picture. To work with matplotlib
one first creates a figure and then adds axes to it.
= plt.figure(figsize=(3, 4))
fig = fig.add_subplot(
axes 1, 1, 1
# here we are saying the figure will have 1 row, 1 column, and this is plot number 1.
) # plot y vs x axes.plot(x, y)
You can plot multiple things on on set of axes. We also add a grid.
1 - 2 * y)
axes.plot(x, True)
axes.grid( fig
If we want a 2x2 array of plots, we could proceed like this.
= plt.figure(figsize=(4, 3))
fig = fig.add_subplot(2, 2, 1)
axes1 = fig.add_subplot(2, 2, 2)
axes2 = fig.add_subplot(2, 2, 3)
axes3 = fig.add_subplot(2, 2, 4)
axes4
axes1.plot(x, y)2 * y + 1)
axes2.plot(x, -y)
axes3.plot(x, -y + 2) axes4.plot(x,
There are lots of types of plots, as always.
= plt.figure(figsize=(6, 8))
fig = fig.add_subplot(2, 2, 1)
axes1 = fig.add_subplot(2, 2, 2)
axes2 = fig.add_subplot(2, 2, 3)
axes3 = fig.add_subplot(2, 2, 4)
axes4
axes1.plot(x, y)"Body Mass (g)"], penguins["Flipper Length (mm)"], s=0.1)
axes2.scatter(penguins[-y, color="green", linestyle="dashed")
axes3.plot(x, -y + 2, color="blue", linestyle="--", linewidth=3) axes4.plot(x,
You need titles (for the figure and the individual plots) and axis labels.
12, 12)
fig.set_size_inches("Demonstration Plot")
fig.suptitle("A Parabola")
axes1.set_title("x")
axes1.set_xlabel("y")
axes1.set_ylabel(True)
axes1.grid("A scatter plot")
axes2.set_title("A green\n upside-down parabola")
axes3.set_title("A blue parabola")
axes4.set_title( fig
Let’s look at a fully developed scatter plot.
= plt.figure(figsize=(10, 10))
fig # fig.suptitle("Flipper Length vs Body Mass")
= fig.add_subplot(1, 1, 1)
axes True)
axes.grid("Body Mass (g)")
axes.set_xlabel("Flipper Length (mm)")
axes.set_ylabel("Flipper Length vs Body Mass")
axes.set_title(# axes.set_xlim(0,5500)
# axes.set_ylim(0,300)
= penguins[penguins["Sex"] == "MALE"]
Males = penguins[penguins["Sex"] == "FEMALE"]
Females = axes.scatter(
male_plot =Males["Body Mass (g)"], y=Males["Flipper Length (mm)"], c="blue", label="Male"
x
)= axes.scatter(
female_plot =Females["Body Mass (g)"],
x=Females["Flipper Length (mm)"],
y="red",
c="Female",
label
)"Male", "Female"]) axes.legend([
Finally we can generate a multiple plot.
= plt.figure(figsize=(10, 30))
fig = penguins[penguins["Sex"] == "MALE"]
Males = penguins[penguins["Sex"] == "FEMALE"]
Females for i, x in enumerate(
"Culmen Length (mm)", "Culmen Depth (mm)", "Flipper Length (mm)"]
[
):= fig.add_subplot(3, 1, i + 1)
axes True)
axes.grid(f"{x} vs Body Mass")
axes.set_title(= axes.scatter(
male_plot =Males["Body Mass (g)"], y=Males[x], c="blue", label="Male"
x
)= axes.scatter(
female_plot =Females["Body Mass (g)"], y=Females[x], c="red", label="Female"
x
)"Male", "Female"]) axes.legend([
Matplotlib also offers a histogram command.
= plt.figure(figsize=(5, 5))
fig = fig.add_subplot(1, 1, 1)
axes True)
axes.grid("Distribution of Body Mass (Male and Female)")
axes.set_title(
axes.hist("Body Mass (g)"],
Males[="forestgreen",
color=50,
bins=True,
density="Male",
label=0.5,
alpha
)
axes.hist("Body Mass (g)"],
Females[="orange",
color=50,
bins=True,
density="Female",
label=0.5,
alpha
)"Male", "Female"])
axes.legend([ fig
There are millions of others…..
Seaborn
Seaborn is based on matplotlib but the graphics are of higher quality (IMHO) and many of the plots published in scientific journals in biology are recognizably seaborn.
Seaborn is also better at handling data sources than matplotlib and has built in statistical capabilities (box plots, density curves, fitted lines…)
Ultimately to make seaborn really work you need to know matplotlib well.
import seaborn as sns
sns.set_theme()
We can declare a data source for our plots.
A scatter plot is called a relplot
for “relationship plot”.
=penguins, x="Body Mass (g)", y="Flipper Length (mm)") sns.relplot(data
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
sns.relplot(=penguins, col="Sex", hue="Species", x="Body Mass (g)", y="Flipper Length (mm)"
data )
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
= sns.relplot(
ax =penguins, hue="Species", x="Body Mass (g)", y="Flipper Length (mm)"
data )
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
Seaborn has some built-in statistical stuff, like ggplot
does.
=penguins, x="Body Mass (g)", y="Flipper Length (mm)", hue="Species") sns.lmplot(data
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
Histograms are displots
.
# displot is a facetgrid object with multiple axes within it; you need to
# get at those to mess with titles, etc.
= sns.displot(data=penguins, x="Body Mass (g)", hue="Species")
ax 0, 0].set_title("Distribution of Body Mass") ax.axes[
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
Text(0.5, 1.0, 'Distribution of Body Mass')
Here are some examples of what you can do with seaborn. Let’s clean up the species names and the sex field first.
"SpeciesS"] = penguins["Species"].apply(lambda x: x.split(" ")[0])
penguins["Sex"] = penguins["Sex"].apply(
penguins[lambda x: x if type(x) != str else x[0] + x[1:].lower()
)
You can split the histograms by species.
= sns.displot(data=penguins, x="Body Mass (g)", col="SpeciesS")
ax "{col_name}") ax.set_titles(
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
You can split the histograms by species and sex, and add density curves if you want.
= sns.displot(data=penguins, x="Body Mass (g)", col="SpeciesS", row="Sex", kde=True)
ax "{col_name}|{row_name}") ax.set_titles(
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
You can add colors if you want.
= sns.displot(data=penguins, x="Body Mass (g)", row="Sex", kde=True, hue="SpeciesS")
ax "{row_name}") ax.set_titles(
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
Among the other types of plots you can get are:
Bar Plots
= sns.catplot(data=penguins, x="SpeciesS", kind="count", hue="Sex") ax
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
Box Plots
= sns.boxplot(data=penguins, x="Body Mass (g)", y="SpeciesS")
ax = ax.set_ylabel("Species") label
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
Violin Plots
= sns.violinplot(data=penguins, x="Body Mass (g)", y="SpeciesS")
ax = ax.set_ylabel("Species") label
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
/home/jet08013/anaconda3/envs/torch/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
if pd.api.types.is_categorical_dtype(vector):
There are many other things you can do. See the seaborn plot gallery.
Bokeh
Bokeh is an open source plotting package that is not derived from matplotlib. It has an underlying javascript engine that provides interactivity.
import bokeh
from bokeh.plotting import figure, output_file, output_notebook, show
from bokeh.models import ColumnDataSource
output_notebook()
We setup a ColumnDataSource from our penguins dataframe.
= ColumnDataSource(penguins) penguins_source
We plot by creating a figure
and adding things to it.
= figure()
F ="Body Mass (g)", y="Culmen Length (mm)", source=penguins_source)
F.scatter(x show(F)
One of the nice featues of Bokeh is that we can add interactive tools to it.
= "Body Mass (g)"
x = "Culmen Length (mm)"
y = [
tooltips "Mass", "@{Body Mass (g)}"),
("Length", "@{Culmen Length (mm)}"),
("Sex", "@Sex"),
(
]= figure(x_axis_label=x, y_axis_label=y, title=f"{y} vs {x}", tooltips=tooltips)
F =x, y=y, source=penguins_source)
F.scatter(x show(F)
To color something, we create a color mapper function.
from bokeh.transform import factor_cmap
= factor_cmap(
color_mapper "SpeciesS",
=["red", "green", "blue"],
palette=["Adelie", "Gentoo", "Chinstrap"],
factors
)= figure(x_axis_label=x, y_axis_label=y, title=f"{y} vs {x}", tooltips=tooltips)
F
F.scatter(=x,
x=y,
y=7,
size=factor_cmap(
fill_color"SpeciesS",
=["red", "green", "blue"],
palette=["Adelie", "Gentoo", "Chinstrap"],
factors
),=penguins_source,
source
) show(F)