matplotlib – Data Visualization in Python – Introduction _ Machine Learning Maverick

matplotlib – Data Visualization in Python – Introduction

matplotlib – Data Visualization in Python – Introduction

ml-maverick

Share this:

matplotlib – Data Visualization in Python – Introduction _ Machine Learning Maverick

What is matplotlib

Matplotlib is a popular Python library for creating static, interactive, and animated visualizations. It provides tools to plot graphs like line charts, bar graphs, scatter plots, and histograms, which are essential for data analysis, and feature exploration. With functions like plot(), scatter(), and imshow(), it enables visualization of data distributions, trends, and decision boundaries.

Why matplotlib

Its extensibility and integration with libraries like NumPy and pandas make it a versatile choice for machine learning workflows. pandas uses matplotlib for data visualization in DataFrames.

Import the library

# importing the matplotlib library & checking its version

import matplotlib
import matplotlib.pyplot as plt

print(f"Matplotlib version: {matplotlib.__version__}")

Know how to create plots

  1. Using Arrays and NumPy: Generate data using numerical arrays, typically with NumPy, and pass these arrays to Matplotlib plotting functions.
  2. Using DataFrame and Pandas: Use structured data from a Pandas DataFrame, referencing column names directly in Matplotlib plotting functions.
  • matplotlib.pyplot.plot() – recommended for simple plots (e.g., x, y).
  • matplotlib.pyplot.XX – recommended for more complex plots:
    • XX can be one of many available functions,
    • also known as the object-oriented API,
    • e.g., plt.subplots() allows for creating multiple plots on the same figure.

Simple plots

# Create a simple plot without a data

plt.plot();
# Create a plot with simple data
plt.plot([1, 2, 3, 4]);
# Create a plot with a data for x and y axes
x = [1, 2, 3, 4]
y = [11, 22, 33, 44]

plt.plot(x, y);

Plots using object-oriented API

# Create a plot using the object-oriented API

# create a figure
fig = plt.figure()

# add a plot
ax = fig.add_subplot()

# display the figure
plt.show()
# Create a Figure and multiple potential Axes and add some data
x = [1, 2, 3, 4]
y = [11, 22, 33, 44]

fig, ax = plt.subplots()
ax.plot(x, y);

Working with Figure, Axes, and Axis

Terminology explanation

  • Figure – the final drawing in matplotlib, which can contain one or more plots.
    • The basic canvas for all matplotlib plots.
    • The general entity that is drawn using matplotlib.
    • Typically abbreviated as fig.
  • Axes – individual plots on the figure or canvas.
    • A single figure can have one or more plots.
    • A figure with multiple plots might have, for example, 4 axes (2 rows and 2 columns).
    • Typically abbreviated as ax.
  • Axis – individual plot axes, e.g., x (horizontal), y (vertical), z (depth).

Why object-oriented API

The term “object-oriented API” is derived from the fact that figures and plots in Matplotlib are implemented as Python classes. Using this API, we create objects that represent individual figures and plots.

# Why object-oriented API?
fig, ax = plt.subplots()
type(fig), type(ax)
(matplotlib.figure.Figure, matplotlib.axes._axes.Axes)

Using matplotlib – workflow

  1. Import the library — e.g., import matplotlib.pyplot as plt.
  2. Prepare the data — data may come from an existing dataset or the results of a machine learning model.
  3. Configure the plot — create the figure and various axes.
  4. Plot the data on the axes — place the appropriate data on the target axes.
  5. Customize the plot — add a title, change colors, and label each axis.
  6. Save (optional) and display — show the final plot and save it to a file if needed.
# Workflow for Using matplotlib

# Older versions of Jupyter Notebook required the magic command %matplotlib inline
# 1. Import the library
import matplotlib.pyplot as plt

# 2. Prepare the data
x = [1, 2, 3, 4]
y = [11, 22, 33, 44]

# 3. Configure the plot (Figure and Axes)
fig, ax = plt.subplots(figsize=(10, 10))

# 4. Plot the data on the axes
ax.plot(x, y)

# 5. Customize the plot
ax.set(title="Simple Example Plot", xlabel="X-axis", ylabel="Y-axis")

# 6. Save and display
fig.savefig("simple-example-plot.png")

Matplotlib Plot Types

  1. Line Plot (ax.plot()):
    • Use Case: Display trends or changes over time (e.g., loss curves, accuracy trends).
    • Syntax: ax.plot(x, y, label='Label')
    • Example: Plot training loss over epochs.
  2. Scatter Plot (ax.scatter()):
    • Use Case: Visualize relationships or patterns between variables (e.g., clustering, feature analysis).
    • Syntax: ax.scatter(x, y, c='color', label='Label')
    • Example: Plot data points in 2D space after dimensionality reduction.
  3. Bar Chart (ax.bar()):
    • Use Case: Compare categorical data or feature importances.
    • Syntax: ax.bar(categories, values, color='color')
    • Example: Display model performance metrics by category.
  4. Histogram (ax.hist()):
    • Use Case: Analyze data distribution (e.g., feature values, residual errors).
    • Syntax: ax.hist(data, bins=n_bins, color='color', alpha=0.7)
    • Example: Show the distribution of feature values.

For more plot types, refer to the documentation.

Creating plots using NumPy and arrays

The matplotlib the library works with and relies on arrays from the NumPy library. Info about the most popular plots can be found in the Matplotlib Plot Types section.

Line plot

Line plots are excellent for observing trends over time. A line plot is the default type in matplotlib. If no other plot type is specified, a line plot will be created.

# Preparing data for the plot

# Create array with linspace() function
x = np.linspace(0, 10, 100)

# Display the first 10 elements of the array
x[:10]
array([0.        , 0.1010101 , 0.2020202 , 0.3030303 , 0.4040404 ,
       0.50505051, 0.60606061, 0.70707071, 0.80808081, 0.90909091])
# Create line plot
fig, ax = plt.subplots()
ax.plot(x, x**2);

Scatter plot

Scatter plots can be useful when we have multiple distinct data points and want to observe how they interact with each other without connecting them.

# Plot data
x = np.linspace(0, 10, 100)

# Create scatter plot
fig, ax = plt.subplots()
ax.scatter(x, np.sin(n));

Bar chart

Bar charts are useful for visualizing different quantities of items related to a similar theme.

# Plot data
fruits_prices = {
    "Apple": 1.5,
    "Orange": 3.0,
    "Banana": 2.5
}

# Create bar chart - vertical
fig, ax = plt.subplots()

# Plotting the data
ax.bar(fruits_prices.keys(), fruits_prices.values());
# Plot data
fruits_prices = {
    "Apple": 1.5,
    "Orange": 3.0,
    "Banana": 2.5
}

# Create bar chart - horizontal
fig, ax = plt.subplots()

# Plotting the data
ax.barh(list(fruits_prices.keys()), list(fruits_prices.values()));

Histogram

Histograms are excellent for showing the distribution of data.

For example, presenting the distribution of a population’s ages or salaries in a city.

# Data for the plot - normal distribution
x = np.random.randn(1000)

# Create histogram
fig, ax = plt.subplots()
ax.hist(x);

Creating Multiple Nested Subplots – Figures, Axes, Subplots

Using the plt.subplots() function, you can create multiple nested subplots on the same figure. Start with one plot, but you can add more as needed.

For example, let’s create a subplot that displays several of the above plots on the same figure.

By using the plt.subplots() function, we create a figure that allows for multiple plots. We utilize the function parameters nrows (number of rows) and ncols (number of columns), which determines the number of plots.

The nrows and ncols parameters are multiplicative, meaning plt.subplots(nrows=2, ncols=2) will create a total of 2*2=4 nested subplots.

For more information on nested subplots, refer to the documentation.

# Creating 4 nested subplots - each with a separate variable

fig, ((line_plot, ax2), (ax3, ax4)) = plt.subplots(ncols=2, nrows=2, figsize=(10, 10))

# Plotting data
line_plot.plot(x, x/2)
line_plot.set(title="Simple Example Plot", xlabel="X-axis", ylabel="Y-axis")

ax2.scatter(np.random.random(10), np.random.random(10))
ax2.set(title="Scatter Plot")

ax3.bar(fruits_prices.keys(), fruits_prices.values())
ax3.set(title="Bar Chart", ylabel="Price (USD)")

ax4.hist(np.random.randn(1000))
ax4.set(title="Histogram")

Creating plots using pandas and DataFrame

The matplotlib library integrates closely with the pandas library, enabling the creation of plots directly from a DataFrame using DataFrame.plot(). Info about the most popular plots can be found in the Matplotlib Plot Types section.

Official pandas documentation – Chart visualization.

NOTE: I will briefly describe creating plots with pandas and DataFrame, in this section. It’s a topic for a separate blog post.

We can create any type of plot using pandas DataFrame using the two methods described below. Both methods provide the same functionality, with the first being more flexible for dynamic plot type selection and the second offering shorter, type-specific syntax.

  • Using DataFrame.plot(kind=<plot_type>) – parameter ‘kind’.
data_frame.plot(kind="<plot_type>")

Replace <plot_type> with the desired plot type (e.g., "line", "bar", "scatter", "hist", etc.). This method allows dynamic selection of the plot type by setting the kind parameter.

  • Using DataFrame.plot.<plot_type>() – a specific method.
data_frame.plot.<plot_type>()

Replace <plot_type> with the specific plot method (e.g., .line(), .bar(), .scatter(), .hist(), etc.). This approach is more concise and directly tied to the specific plot type.

# Importing the pandas library
import pandas as pd

# Loading the data
car_sales = pd.read_csv("car-sales.csv")

# Displaying the data
car_sales
MakeColourOdometer (KM)DoorsPrice
0ToyotaWhite1500434$4,000.00
1HondaRed878994$5,000.00
2ToyotaBlue325493$7,000.00
3BMWBlack111795$22,000.00
4NissanWhite2130954$3,500.00
5ToyotaGreen992134$4,500.00
6HondaBlue456984$7,500.00
7HondaBlue547384$7,000.00
8ToyotaWhite600004$6,250.00
9NissanWhite316004$9,700.00
# Creating bar chart - with parameter 'kind'
car_sales.plot(
    x="Make",
    y="Odometer (KM)",
    kind="bar");
# Creating bar chart - with a method 'bar()'
car_sales.plot.bar(
    x="Make",
    y="Odometer (KM)");

Both code examples produce the same results shown below.

Customize plots

Matplotlib provides extensive customization options to enhance the appearance and readability of plots. In the context of machine learning, data visualization is a crucial element in understanding patterns, relationships, and model performance.

Styles

Matplotlib provides predefined styles that can be quickly applied to plots. To view the available styles, use plt.style.available.

# Display available styles
plt.style.available

# Apply the style
plt.style.use('fivethirtyeight')

# Create bar plot with a new style
car_sales.plot(
    x="Make",
    y="Odometer (KM)",
    kind="bar");

Labels

We can add titles, axis labels, and a legend to make the plot more informative.

ax = car_sales.plot(
        x="Make",
        y="Odometer (KM)",
        kind="bar")

ax.set(
    title="Mileage vs. Make",
    xlabel="Car make",
    ylabel="Mileage in KM"
);

Colors

We can customize plot colors using named colors or hex codes.

ax = car_sales.plot(
        x="Make",
        y="Odometer (KM)",
        kind="bar",
        color="orange")

Ranges – xlim, ylim

In many cases, we may want to focus on specific ranges of data by limiting the X and Y axes.

# 
ax = car_sales.plot(
        x="Make",
        y="Odometer (KM)",
        kind="bar")

print(f"Value of xlim: {ax.get_xlim()} \nValue of ylim: {ax.get_ylim()}")
Value of xlim: (-0.5, 9.5) 
Value of ylim: (0.0, 223749.75)
# 
ax = car_sales.plot(
        x="Make",
        y="Odometer (KM)",
        kind="bar")

ax.set(
    xlim=[1, 3],
    ylim=[2, 100000]
)

print(f"Value of xlim: {ax.get_xlim()} \nValue of ylim: {ax.get_ylim()}")
Value of xlim: (1.0, 3.0) 
Value of ylim: (2.0, 100000.0)

Saving plots

When we have a visually appealing plot, we can save it in your preferred file format and share it.

Depending on the chosen method of creating the plot we can save the resulting plot in two different ways:

  • Using a function plt.savefig() directly on a matplotlib library.
  • Using a function fig.savefig() on an object returned by plt.subplots().

plt.savefig()

# Saving plot using arrays and NumPy

# Plot data
x = [1, 2, 3, 4]
y = [11, 22, 33, 44]

# Create plot
plt.plot(x, y);

# Save the plot to a file named 'simple-plot.png'
plt.savefig(fname="simple-plot.png")
# Saving plot using DataFrame and pandas

# Importing the pandas library
import pandas as pd

# Loading the data
car_sales = pd.read_csv("car-sales.csv")

# Create plot
car_sales.plot(
    x="Make",
    y="Odometer (KM)",
    kind="bar");

# Save the plot to a file named 'simple_bar-plot_odometer-vs-make.png'
plt.savefig(fname="pandas_bar-plot_odometer-vs-make.png")

fig.savefig()

# Saving plot using object-oriented API

import pandas as pd
car_sales = pd.read_csv("car-sales.csv")

# Create plot
fig, ax = plt.subplots()
ax.scatter(car_sales["Odometer (KM)"], car_sales["Total Sales"]);

# Save the plot to a file named 'simple_scatter-plot_milage-vs-sales.png'
fig.savefig(fname="simple_scatter-plot_milage-vs-sales.png")

NOTE: In this article, I’m just barely scratching the surface. This topic needs more reading and research on your own. I’m still at the beginning of my learning process of AI & ML!

Leave a Reply

Your email address will not be published. Required fields are marked *

ml-maverick

Hi, I’m Jacek. 𝗔𝗜-𝗠𝗶𝗻𝗱𝗲𝗱 𝗚𝘂𝘆 – I work with and teach about machine learning – self-taught. I’m interested in machine learning from the technical and mathematical perspective. 𝗠𝗲𝗻𝘁𝗼𝗿, 𝗧𝗿𝗮𝗶𝗻𝗲𝗿 𝗳𝗼𝗿 𝗦𝗼𝗳𝘁𝘄𝗮𝗿𝗲 𝗗𝗲𝘃𝗲𝗹𝗼𝗽𝗲𝗿𝘀 – Helping others to change their current occupation to Junior Java Developer. I hope you will find something interesting on my blog.

RECENT POST


RECENT COMMENT