Using numpy with Matplotlib


Martin McBride, 2019-03-03
Tags numeric python, linspace, function plot
Categories matplotlib, numpy
In section Python libraries



The previous article showed how to create a plot using Python lists to store the data. Matplotlib also works with numpy, and this can often simplify things.

Here we will look at how to use numpy to create the x values and y values more easily.

Creating the x values

We want to create a set of 100 x values, equally spaced over the range 0 to 12. We can do this using Python lists by scaling the loop variable.

However, numpy has a function linspace that is designed to do this job. It has two advantages:

  • it is more readable
  • it covers the exact range 0 to 12, with the intermediate values equally spaced

If you recall, our previous code went from 0 to almost 12, which wasn't really much of a problem, but it wasn't quite correct.

Here is how we use linspace to create our range:

xa = np.linspace(0, 12, 100)

Creating the y values

numpy supports universal functions. These allow you to operate on entire arrays in one go:

ya = np.sin(xa)*np.exp(-xa/4)

This performs the same calculation on each of the 100 elements of xa, creating a new 100 element array ya containing the results. This avoids a loop, which makes the code more readable, but also more efficient.

Complete code

Here is the complete code:

from matplotlib import pyplot as plt
import numpy as np

xa = np.linspace(0, 12, 100)
ya = np.sin(xa)*np.exp(-xa/4)

plt.plot(xa, ya)
plt.show()

This code is shorter and more readable, as well as being more efficient. This becomes more important with large data sets, and especially 2 dimensional data.

Here is the output, which look exactly the same as before:

Visit the PythonInformer Discussion Forum for numeric Python.

If you found this article useful, you might be interested in the book Computer Graphics in Python, or other books, by the same author.

<<Prev

Tag cloud

2d arrays abstract data type alignment and array arrays bezier curve built-in function close closure colour comparison operator comprehension context conversion data types design pattern device space dictionary duck typing efficiency encryption enumerate filter font font style for loop function function composition function plot functools generator gif gradient greyscale higher order function html image processing imagesurface immutable object index inner function input installing iter iterator itertools lambda function len linspace list list comprehension logical operator lru_cache mandelbrot map monad mutability named parameter numeric python numpy object open operator optional parameter or partial application path positional parameter print pure function radial gradient range recipes recursion reduce rgb rotation scaling sequence slice slicing sound spirograph str stream string subpath symmetric encryption template text text metrics transform translation transparency tuple unpacking user space vectorisation webserver website while loop zip

Copyright (c) Axlesoft Ltd 2020