How to plot categorical variables as color with Matplotlib and Pandas

1 min

Sometimes it is useful to add an extra dimension to a plot, especially to see whether there are some clusters forming.

To do so, you will first have to transform the categories into numbers and then set the color as the category number.

Here is the example

# For our DataFrame
import pandas as pd

# In order to plot
import matplotlib.pyplot as plt

# We get our sample data from github
df = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')

# We transform text categorical variables into numerical variables
df["species_codes"] = pd.Categorical(df["species"]).codes

# We setup our figure
fig, axes = plt.subplots(1,1, figsize=(6,5))

# We plot the scatter with c = our categorical variables 
axes.scatter(data=df,
            x="petal_length",
            y="sepal_length",
            c="species_codes")
            
# We plot the grid            
axes.grid()

# We add better labels
axes.set_xlabel("Petal Length")
axes.set_ylabel("Sepal Length")

# We set the title
axes.set_title("How to plot categorical variable as color with Matplotlib")

# We tidy things up
plt.tight_layout()

# We plot our data
plt.show()
The code

Here is the result

The results

More on plots

If you want to know more about how to add labels, plot different types of plots, etc... check out the other articles I wrote on the topic, just here :

Matplotlib - The Python You Need
We gathered the only Python essentials that you will probably ever need.