Skip to content
Home » matplotlib Part 2 – A Basic Matplotlib Example

matplotlib Part 2 – A Basic Matplotlib Example

Spread the love

In the previous part of the Matplotlib series we created the matplotlib folder in Jupyter Notebook. Let’s add a notebook now. With Jupyter Notebook launched, first select the matplotlib folder on the left (A) and then click on New (B) and select Python 3 (C):

new notebook

This will create a new notebook. To rename it click on Untitled:

rename notebook

When the popup window appears, go ahead and type in the new name (A). As mentioned in the previous lecture, I’m going to use a naming convention based on the number of the lecture. Then hit Rename (B).

rename popup

Now you will see the new title:

renamed notebook

And now we’re ready to create a first basic graph. First we need to import the modules that we will need. Here’s the code with comments. Just type it in into the first cell. Don’t worry if you don’t understand what’s going on, it will be all explained in the following articles.

Here’s the code:

In [1]:
# This magic command is only used in the IPython environment,
# so in the IPython console and in Jupyter Notebook. 
# It should be used only once per session, just like the imports 
# further down. If you use this command, all graphs will
# be embedded directly in the notebook rather than displayed
# in a separate window.
%matplotlib inline

# Here are the imports. Here you can also see the commonly
# used aliases for matplotlib and matplotlib.pyplot.
import matplotlib as mpl
import matplotlib.pyplot as plt

# We will also use numpy for this example.
import numpy as np

# And here's the example.
# First let's create an array of 100 evenly spaced numbers
# between -10 and 10. We can use the numpy linspace method
# to do that. These are going to be the X-coordinates.
# The method takes three parameters: the lower and upper
# range and the number of items.
x = np.linspace(-10, 10, 100)

# Now we're going to plot the following quadratic function:
# y = x ** 2 - 2 * x + 3, so we have:
y = x ** 2 - 2 * x + 3

# Now we have to generate the Figure instance and an Axes
# instance. The terms FIGURE and AXES are among the basic
# Matplotlib terms. A figure is the canvas area where one
# or more Axes instances can be added. An Axes instance is
# a region of the figure with its own individual coordinate
# system. There may be one area instance, like in the example
# below, but there may also be more.
# We can use the plt.subplots function to create a figure
# and an axis at the same time.
fig, ax = plt.subplots()

# And now let's finally plot the graph. Most functions we're 
# going to use are Axes methods.
ax.plot(x, y)

# Now hit Ctrl + Enter to execute the code.
Out[1]:
[<matplotlib.lines.Line2D at 0x28de4830208>]

This is just a very basic graph and we should definitely add some elements to it. We’ll learn how to do it in the following parts of the series.

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.

Here’s the video version of the article:


Spread the love

Leave a Reply