Open In Colab

Let's start plotting

Basic Plot

A very basic plot can be created using the .plot() operator. This creates a simple graph with an x and an y-axis.

In [ ]:
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
In [ ]:
# Create plot with x and y-axis
plt.plot()
Out[ ]:
[]

Looks okay, but not really useful so far. Let's try adding some data to our empty plot. On a still, very basic level, we could try to plot a character "o".

Note: For this to work we must specify some arbitrary x- and y-values first. The "o" character will therefore be the third argument of the plot.The first two values (5,5) are the maximum x and y values that we want to visualize, the third argument ("o") is called a format string character (=fmt) which is used for testing out the basic properties of a graph (not practical in real life).

In [ ]:
plt.plot(5,5,"o")
Out[ ]:
[<matplotlib.lines.Line2D at 0x7f99439cfe90>]

Awesome! At this point you may wonder how you are supposed to know which potential parameters you can enter into a basic plot. This is where documentation comes in. Simply type in plt.plot? and you will get a list of all possible operations of the respective method.

In [ ]:
plt.plot?

As you can see, there are countless possible options to choose from. This is actually one of the main issues with matplotlib. Due to its huge flexibility, countless options of working with the library exist - this, unfortunately, may become confusing.

However, noone expects you to know this stuff by heart. For working with matplotlib, Googling and looking at the documentation is key - even the pros do it all the time.

Bacic Plot - Figures and Axes

While the .plot() option (as shown in the lecture), is a very straight forward approach to creating a simple graph - we often use figures and axes instead. In their simplest form, a figure and axes can be created as follows:

In [ ]:
fig = plt.figure()
ax = plt.axes()

In Matplotlib, the figure (an instance of the class plt.Figure) can be thought of as a single container that contains all the objects representing axes, graphics, text, and labels. The axes (an instance of the class plt.Axes) is what we see above: a bounding box with ticks and labels, which will eventually contain the plot elements that make up our visualization.

Once we have created an axes, we can use the ax.plot function to plot some data. Let's plot the same graph again.

In [ ]:
fig = plt.figure()
ax = plt.axes()
ax.plot(5,5, "o")
Out[ ]:
[<matplotlib.lines.Line2D at 0x7f994380ef10>]

Summing up, we can either use the combination of plt.figureand plt.axes to plot a graph or use plt.plot and let the figure and axes be created for us in the background (see Two Interfaces for the Price of One for a discussion of these two alternatives).

Basic Plot - Get Current Axes

A third alternative to creating plots is by using the .gca (= get current axes) parameter. As stated by its name, this method gets the current Axes instance of the current figure and allows us to store them in a variable.

This makes our code much cleaner and allows us to apply further operations on our plot. One common addition to the .gca operator, is the use of the .axis() operator, which allows us to change the properties of the axes we just "got". .axis() allows us to specify the minimum and maximum x & y values of our respective graph. Let's look at an example:

In [ ]:
plt.figure()
# plot the point (3,2) using the circle marker
plt.plot(3, 2, 'o')
# get the current axes (we store our plot in the variable "ax")
ax = plt.gca()
# Show our plot
ax
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f99439924d0>

So far so good. Let's now use .axis() to change the shown axis to our liking.

In [ ]:
# plot the point (3,2) using the circle marker
plt.plot(3, 2, 'o')
# get the current axes (we store our plot in the variable "ax")
ax = plt.gca()
# Set axis properties [xmin, xmax, ymin, ymax] of our plot (which is stored in "ax")
ax.axis([0,6,0,10])
Out[ ]:
(0.0, 6.0, 0.0, 10.0)

Exercise For You
Look at the following code snippet and try to guess how the resulting plot may look. Then, copy the code, paste it in the empty cell below and check whether you were correct.

# plot the point (1.5, 1.5) using the circle marker
plt.plot(1.5, 1.5, 'o')
# plot the point (2, 2) using the circle marker
plt.plot(2, 2, 'o')
# plot the point (2.5, 2.5) using the circle marker
plt.plot(2.5, 2.5, 'o')
In [ ]:
# test your answer