Introduction to Matplotlib

Technocrat
CoderHack.com
Published in
4 min readSep 16, 2023

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. It can be used in Python scripts, the Python and IPython shell, web application servers, and various graphical user interface toolkits.

Matplotlib was created by John D. Hunter. It was initially released in 2002 and has since become the de facto standard for data visualization in Python. Some of the main features of Matplotlib are:

  • Produces publication quality figures
  • Supports a variety of graphics including:
  • Line plots
  • Bar plots
  • Histograms
  • Scatter plots
  • Contour plots
  • Images
  • Animation
  • Has a MATLAB-like syntax, making it easy to get started if you are familiar with MATLAB
  • Works with Python, SciPy and NumPy

To install Matplotlib, use pip:

pip install matplotlib

Then you can import it in Python:

import matplotlib.pyplot as plt

Simple Line Plot

Let’s start with a simple line plot. We’ll plot the values of x and y on a line graph:

x = [1, 2, 3, 4, 5]
y = [2, 3, 1, 5, 4]

plt.plot(x, y) # Plot the data
plt.xlabel('Time') # Add an x-label
plt.ylabel('Value') # Add a y-label
plt.title('Line Plot') # Add a title
plt.show() # Display the plot

Bar Plot

Next, let’s create a bar plot. We can plot a basic bar graph as follows:

x = [1, 2, 3, 4, 5]
y = [2, 3, 1, 5, 4]

plt.bar(x, y) # Plot a bar graph
plt.xlabel('Time')
plt.ylabel('Value')
plt.title('Bar Plot')
plt.show()

We can also create horizontal bar plots by using the plt.barh() function:

plt.barh(x, y)

Histograms

A histogram shows the frequency of values in a data set. Let’s generate some random data and plot a histogram:

import numpy as np

x = np.random.randn(1000) # 1000 random values

plt.hist(x) # Plot a histogram
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Histogram')
plt.show()

We can adjust the number of bins in the histogram using the bins parameter:

plt.hist(x, bins=30)  # Increase the number of bins

Scatter Plots

A scatter plot shows the relationship between two quantitative variables. Let’s create a basic scatter plot:

x = [1, 2, 3, 4, 5]
y = [2, 3, 1, 5, 4]

plt.scatter(x, y)
plt.xlabel('Time')
plt.ylabel('Value')
plt.title('Scatter Plot')
plt.show()

We can use the c parameter to specify color values for each point:

colors = x  # Use x values for color
plt.scatter(x, y, c=colors)

This uses a color map to color code the points by the x value.

Contour Plots

Contour plots are useful for visualizing 3D meshes or matrices. We can create a simple contour plot as follows:

import numpy as np

x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 2, 1]

X, Y = np.meshgrid(x, y) # Create meshgrid
Z = X * np.sin(Y) + Y * np.cos(X) # Function for contour levels

plt.contour(X, Y, Z) # Plot contour
plt.xlabel('x')
plt.ylabel('y')
plt.title('Contour Plot')
plt.show()

We can also use plt.contourf() to plot filled contours:

plt.contourf(X, Y, Z)

The number of contour levels can be adjusted using the levels parameter.

Conclusion

This article introduced you to some of the most commonly used data visualization capabilities of Matplotlib. We looked at how to create line plots, bar plots, histograms, scatter plots, and contour plots. Matplotlib is a powerful library with many other features like:

  • Polar plots
  • 3D plotting
  • Geographical projections
  • And much more!

I highly recommend checking out the Matplotlib documentation to explore all of its capabilities. Let me know if you have any other questions!

--

--