Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to plot points in matplotlib

Understanding the Basics of Matplotlib

Before we delve into plotting points, let's briefly understand what Matplotlib is. Matplotlib is a popular Python library used for creating static, interactive, and animated visualizations in Python. Think of it as a tool that lets you paint a variety of graphs and charts with data. It's like having a digital canvas where you can plot your numbers and see them come to life as visual representations.

Setting Up Your Canvas

To start plotting, you need to set up your 'canvas' or 'figure' in Matplotlib terms. This is where your plot will live.

Here's how you can do it:

import matplotlib.pyplot as plt

# This creates a figure object, which can be thought of as an empty canvas.
fig = plt.figure()

# Adding a subplot will give us an area on the canvas where we can draw our plot.
ax = fig.add_subplot(1, 1, 1)  # The arguments (1, 1, 1) indicate a single subplot.

Plotting Your First Point

Now, let's plot a single point. A point in a two-dimensional space has two coordinates, usually referred to as x (horizontal axis) and y (vertical axis).

# Coordinates of the point
x = 5
y = 10

# Plotting the point
ax.plot(x, y, 'ro')  # 'ro' stands for red circle

# Showing the plot
plt.show()

When you run this code, you'll see a red dot at the position (5, 10) on your canvas.

Plotting Multiple Points

Most of the time, you'll want to plot more than one point. Let's see how you can plot multiple points.

# List of x coordinates and y coordinates
x_points = [1, 2, 3, 4, 5]
y_points = [1, 4, 9, 16, 25]

# Plotting the points
ax.plot(x_points, y_points, 'bo')  # 'bo' stands for blue circles

# Showing the plot
plt.show()

This will show you blue dots at the given x and y coordinates. It's like plotting stars in the night sky on your digital canvas.

Connecting the Dots

What if you want to connect these points with lines? That's simple too. By default, the plot function connects the points with lines.

# Plotting the points with a line
ax.plot(x_points, y_points)

# Showing the plot
plt.show()

Customizing Your Plot

Matplotlib allows you to customize your plots in various ways. You can change the color and style of your points and lines, add labels, and much more.

Changing Point Markers

You can change the marker style by using different marker codes, like 's' for square, '*' for star, etc.

# Plotting with star markers
ax.plot(x_points, y_points, 'g*')  # 'g*' stands for green stars

# Showing the plot
plt.show()

Adding Titles and Labels

Adding titles and labels to your axes helps others understand what your plot represents.

# Setting the title and labels
ax.set_title('My First Plot')
ax.set_xlabel('X Axis Label')
ax.set_ylabel('Y Axis Label')

# Plotting the points
ax.plot(x_points, y_points, 'g*')

# Showing the plot
plt.show()

Understanding Axes and Grids

Think of axes like the lines of a grid in a notebook that help you locate the points. Matplotlib allows you to customize these as well.

# Adding grid
ax.grid(True)

# Setting axis limits
ax.set_xlim(0, 6)
ax.set_ylim(0, 30)

# Plotting the points
ax.plot(x_points, y_points, 'g*')

# Showing the plot
plt.show()

Now, you have a grid background, and your plot is confined within the limits you set for the x and y axes.

Saving Your Plot

If you want to save your plot as an image file, you can do so with the savefig method.

# Plotting the points
ax.plot(x_points, y_points, 'g*')

# Saving the figure
plt.savefig('my_first_plot.png')

# Showing the plot
plt.show()

This will save your plot as a PNG file named 'my_first_plot.png' in the current working directory.

Conclusion

Congratulations! You've just learned how to plot points in Matplotlib and customize them to tell a visual story. Remember, the key to mastering data visualization is practice and exploration. Don't hesitate to play around with the settings, colors, and styles. Each plot you create is a unique narrative of the data you're working with, and with Matplotlib, you have the power to tell that story in a clear and impactful way. So keep plotting, and let your data visualization journey continue to unfold one point at a time.