How to plot categorical variables in Matplotlib?

To plot categorical variables in Matplotlib, we can use different chart types like bar plots, scatter plots, and line plots. Categorical data represents discrete groups or categories rather than continuous numerical values.

Steps to Plot Categorical Variables

  • Set the figure size and adjust the padding between and around the subplots.
  • Create a dictionary with categorical data.
  • Extract the keys and values from the dictionary.
  • Create a figure and subplots for different plot types.
  • Plot using bar, scatter and plot methods with categorical names and values.
  • Display the figure using show() method.

Example

Let's create three different visualizations for categorical fruit data ?

import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

data = {'apple': 10, 'orange': 15, 'lemon': 5}
names = list(data.keys())
values = list(data.values())

fig, axs = plt.subplots(1, 3)

axs[0].bar(names, values)
axs[0].set_title('Bar Plot')

axs[1].scatter(names, values)
axs[1].set_title('Scatter Plot')

axs[2].plot(names, values, marker='o')
axs[2].set_title('Line Plot')

plt.show()

Common Plot Types for Categorical Data

Bar Plot

Bar plots are the most common way to visualize categorical data ?

import matplotlib.pyplot as plt

categories = ['Product A', 'Product B', 'Product C', 'Product D']
sales = [120, 85, 200, 150]

plt.figure(figsize=(8, 5))
plt.bar(categories, sales, color=['red', 'blue', 'green', 'orange'])
plt.title('Sales by Product')
plt.xlabel('Products')
plt.ylabel('Sales')
plt.xticks(rotation=45)
plt.show()

Horizontal Bar Plot

For better readability with long category names, use horizontal bars ?

import matplotlib.pyplot as plt

departments = ['Human Resources', 'Marketing', 'Sales', 'Engineering']
employees = [25, 40, 60, 85]

plt.figure(figsize=(8, 5))
plt.barh(departments, employees, color='skyblue')
plt.title('Employees by Department')
plt.xlabel('Number of Employees')
plt.ylabel('Departments')
plt.show()

Comparison of Plot Types

Plot Type Best For Use Case
Bar Plot Comparing categories Sales by region, survey results
Horizontal Bar Long category names Country names, department names
Scatter Plot Showing relationships Category vs continuous variable
Line Plot Trends over categories Time series with categorical labels

Conclusion

Bar plots are the most effective way to visualize categorical data in Matplotlib. Use horizontal bars for long category names and add colors, titles, and labels to make your plots more informative and visually appealing.

Updated on: 2026-03-25T22:34:50+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements