Figure 5-2. Plot given data as 1D array
NUMPY ARRAYS AND MATPLOTLIB
You can simply pass NumPy ndarray objects to matplotlib functions. It is able to interpret the data structure for
simplified plotting. However, be careful to not pass a too large and/or complex array.
Since the majority of the ndarray methods return again an ndarray object, you can also
pass your object with a method (or even multiple methods, in some cases) attached. By
calling the cumsum method on the ndarray object with the sample data, we get the
cumulative sum of this data and, as to be expected, a different output (cf. Figure 5-3):
In [ 5 ]: plt.plot(y.cumsum())
Figure 5-3. Plot given a 1D array with method attached
In general, the default plotting style does not satisfy typical requirements for reports,
publications, etc. For example, you might want to customize the font used (e.g., for
compatibility with LaTeX fonts), to have labels at the axes, or to plot a grid for better
readability. Therefore, matplotlib offers a large number of functions to customize the
plotting style. Some are easily accessible; for others one has to go a bit deeper. Easily
accessible, for example, are those functions that manipulate the axes and those that add
grids and labels (cf. Figure 5-4):
In [ 6 ]: plt.plot(y.cumsum())
plt.grid(True) # adds a grid
plt.axis(‘tight’) # adjusts the axis ranges