You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 23 Next »


To visualize data in Python we will use the library Matplotlib. Matplotlib is a Python 2D plotting library with a variety of vizualisation tools. To see the full gallery of possibilities inluding tutorials, we highly recommend you to visit the offical Matplotlib page. In the following examples we will only cover some of the basic and most often used tools when visualizing data.


If you have not already installed Matplotlib, visit the Matplotlib installation instructions. As NumPy is used a lot when working with Matplotlib, we also recommend checking it out.

Simple plot

Visit this page for full documentation on simple plots using pyplot.


Simple plot code
import numpy as np
import matplotlib.pyplot as plt

# Evenly sampled time from 0s to 10s at 200ms intervals
t = np.arange(0.0, 10.0, 0.2)

# Plotting t at x-axis and sin(t) at y-axis
plt.plot(t, np.sin(t))

# Naming the title and both axis
plt.title('Sinus function')
plt.ylabel('sin(t)')
plt.xlabel('t [s]')

# Need to call the show() function at the end to display my figure
plt.show()

Multiple plots in same figure
import numpy as np
import matplotlib.pyplot as plt


# Evenly sampled time at 200ms intervals
t = np.arange(0.0, 5.0, 0.2)

# plot() can plot several lines in the same figure. To seperate the different lines 
# from eachother, we may change the line style and format strings.
# See the plot() documentation for a complete list of line styles and format strings.
# The following lines have red dashes, blue squares and green triangles
plt.plot(t, t, 'r--', label='Linear line')
plt.plot(t, t**2, color='blue', linestyle='none', marker='s', label='Second degree polynom')
plt.plot(t, t**3, 'g^', label='Third degree polynom')

# To describe our plot even more detailed we can draw the labels we previously gave our lines using legend.
plt.legend(loc='upper left')

# The function axis() sets the axis sizes, and takes the argument [xmin, xmax, ymin, ymax]
plt.axis([0, 5, 0, 100])

plt.title('Mulitple polynoms')
plt.show()

Multiple figures and subplots

A very good and more detailed guide on subplots and figures can be found here.

import matplotlib.pyplot as plt
import numpy as np

# Some example data to display
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

A single plot

subplots() without arguments return a Figure and a single Axes.

fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('A single plot')



Quiver plot

More in-depth quiver documentation and functions.

Simple quiver plot
import numpy as np
import matplotlib.pyplot as plt

# X and Y define the arrow locations
X, Y = np.meshgrid(np.arange(0, 2 * np.pi, .2), np.arange(0, 2 * np.pi, .2))

# U and V define the arrow directions, respectively in x- and y-direction
U = np.cos(X)
V = np.sin(Y)

# Call siganture: quiver([X, Y], U, V, [C]), where C optionally sets the color
plt.quiver(X, Y, U, V)
plt.title('Simple quiver plot')
plt.show()

The plot autoscaling does not take into account the arrows, so those on the boundaries may reach out of the picture. This is not an easy problem to solve in a perfectly general way. The recommended workaround is to manually set the Axes limits in such a case. An example showing with only autoscaling vs manually is shown below.

Quiver autoscaling vs manually set axes
import numpy as np
import matplotlib.pyplot as plt

# X and Y define the arrow locations
# This setup gives us 10 arrows in width and height, as our interval is from -5 to 5 with step 1
X = np.arange(-5, 5, 1)
Y = np.arange(-5, 5, 1)

# U and V define the arrow directions, respectively in x- and y-direction
U, V = np.meshgrid(3*X, 3*Y)

plt.figure()

plt.subplot(121)
plt.quiver(X, Y, U, V)
plt.title('Only autoscaling')

plt.subplot(122)
plt.quiver(X, Y, U, V)
# Here we specify the axes. How much extra space you need depends on the arrow size and direction,
# and must therefor be adapted each time
plt.axis([-6.5, 5.5, -6.5, 5.5])
plt.title('Manually set axes')

plt.show()

  • No labels