Monday, January 20, 2020

The PyPlot (scripting) layer

The PyPlot module was developed, to provide a simpler and more familiar interface to those coming from software packages like MATLAB®, or simply for those who are new to Python programming.
Consider, for example, our first plotting script of a sine function :

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 4.*np.pi, 33)
y = np.sin(x)
plt.plot(x, y)
plt.show()

After importing NumPy and PyPlot, we simply define the x-y arrays in two lines, make a plot in the next line, and then display it in the last line: the epitome of simplicity. There is no mention of a backend in this syntax. Nevertheless, one must be used so that matplotlib can communicate with the computer. So how does a backend get loaded using PyPlot? And which backend?

First, a backend is loaded when PyPlot is imported. Second, which backend is loaded is determined by how you launch IPython. If you launch it from a terminal or from a Jupyter notebook, the default
backend is set by a matplotlib configuration file named matplotlibrc on your computer. The file was put on your computer, perhaps without your knowledge, when matplotlib was installed, for example using the Anaconda or Enthought installations of Python. Alternatively, if you are using a Python IDE like Spyder, the backend that is used is set in a Preferences menu and loaded when the IDE is
launched.Thus, using PyPlot frees you up from thinking about the backend—how matplotlib is connected to your computer’s hardware—when writing matplotlib routines.

For very simple plots, you can proceed using the syntax where the only matplotlib prefix used is plt.
You can do most of the things you want to do with matplotlib working in this way. You can make single plots or plots with multiple subplots. When working in this mode with PyPlot, we are working in a “state-machine” environment, meaning that where on a canvas Py-Plot adds features (i.e.,Artists) depends on the state of the program.
Share:

0 comments:

Post a Comment