Codecademy Logo

Introduction to Matplotlib

Pyplot functions

The Python library Matplotlib contains the pyplot module, which provides users with an interface for graphing data. Pyplot contains over 100 functions, from acorr to yticks. You must import the module, and plt is the standard variable name used.

from matplotlib import pyplot as plt

Here are some of the most common pyplot functions:

Function Description
Plot plots y versus x as lines and/or markers
Show displays a figure
Axis sets some axis properties
Xlabel sets the label for the x-axis
Ylabel sets the label for the y-axis
Title sets a title for the axes
Subplot adds a subplot to the current figure
Subplots_adjust tunes the subplot layout
Legend places a legend on the axes
Figure creates a new figure
Savefig saves the current figure

Pyplot-axis

Matplotlib’s pyplot.axis function takes one parameter, which must be a four-item array, and returns the current axes’ limits. The four items should contain enough info to define an x axis and a y axis by minimum and maximum values. The array must order these values as follows: x-axis minimum, x-axis maximum, y-axis minimum, y-axis maximum.

x = range(12)
y = [2,8,20,40,70,300,930,7000,68000,500000,4000000, 2000000]
plt.plot(x, y)
#x-axis minimum is 0, x-axis maximum is 11; y-axis minimum is 300, y-axis maximum is 500000
plt.axis([0,11,300,500000])
plt.show()

Setting Linestyle, Color in Matplotlib

In Python’s Matplotlib, the pyplot.plot() function can accept parameters to set the color(color), linestyle(linestyle) and marker(marker) for line graph. Color values can be HTML color names or HEX codes. Line styles can be dashed('--') or dotted('..'). Markers can be circles('o'), squares('s'), stars('*'), or other shapes.

pyplot.plot(days, money_spent, color='green', linestyle='--')
pyplot.plot(days, money_spent_2, color='#AAAAAA', marker='o')

Adjusting Subplot Margins in Matplotlib

In Python’s Matplotlib, subplots can overlap, either horizontally or vertically. The function pyplot.subplots_adjust() can set better spacing around each subplot in a figure. It can set values for left, right, bottom and top margins, plus the horizontal(wspace) and vertical(hspace) spaces between adjacent subplots.

import matplotlib.pyplot as plt
# Left Plot
plt.subplot(1, 2, 1)
plt.plot([-2, -1, 0, 1, 2], [4, 1, 0, 1, 4])
# Right Plot
plt.subplot(1, 2, 2)
plt.plot([-2, -1, 0, 1, 2], [4, 1, 0, 1, 4])
# Subplot Adjust
plt.subplots_adjust(wspace=1.3)
plt.show()

X-ticks and Y-ticks in Matplotlib

In Python’s Matplotlib, the x-tick and y-tick marks of the plot can be changed using functions ax.set_xticks() and ax.set_yticks(). These functions accepts an array of values representing tick mark positions.

import matplotlib.pyplot as plt
ax = plt.subplot()
plt.plot([0, 1, 2, 3, 4], [0, 1, 4, 9, 16])
plt.plot([0, 1, 2, 3, 4], [0, 1, 8, 27, 64])
ax.set_xticks([1, 2, 4])

Subplots in Matplotlib

In Python, the Matplotlib’s pyplot.subplot() function can be used to create a figure with a grid of subplots. The function accepts number of rows, number of columns, and the current index as arguments.

import matplotlib.pyplot as plt
# Datasets
x = [1, 2, 3, 4]
y = [1, 2, 3, 4]
# First Subplot
plt.subplot(1, 2, 1)
plt.plot(x,y, color='green')
plt.title('First Subplot')
# Second Subplot
plt.subplot(1, 2, 2)
plt.plot(x,y, color='blue')
plt.title('Second Subplot')
# Display both subplots
plt.show()

Figures in Matplotlib

In Python’s Matplotlib, a figure is a container that holds plots. It can hold a single plot or multiple plots. When a figure holds multiple separate plots, those are called subplots.

0