You are on page 1of 3

MATPLOTLIB NOTES

import matplotlib.pyplot as plt


%matplotlib inline

0. MOTIVATION

Graphical representation allows us to gain intuition, generate ideas or


perform visual checks of some data. EDA (Exploratory Data Analysis) consists of
plotting data in all possible ways to extract exploitable information from the
data.

1. BASICS

1.1. FRAME

To create the frame for the plot we use 'plt.figure(figsize=(width,height))'


where the width and the height are in inches.

1.2. SUBPLOT

To create a subplot we use 'plt.subplot(i,j,k)' where i and j are the size of


the subplot grid (row and column) and k is the one we want to use.

2. PLOTTING

In the 'plt.plot(x,y,...)' we have lots of characteristics possible. Here x


and y are the list or arrays or listsof each coordinate for the points.

More info with help(plt.plot).

2.1. Data

- Type: a string with one of the existing options. If no type is


selected, it will be a line.
- Size: we use 'markersize='.
- Alpha: value from 'alpha=0' to 1 that sets the blending or
transparency.
- Label: we use 'label='name'' to give a name to the data plotted. For
it to show we use 'plt.legend()'.
- Line: 'linewidth='
- Color: 'color='specificcodeforthecolor''
- When no x is specified, it just uses an index array going from 0 to
N-1 being N the number of values in the y list.

2.2. Labels and axis

- Labels: We simply use 'plt.xlabel('')' and 'plt.ylabel('')'.


- Axis: for semilogaritmic axis, we use 'plt.semilogy(y...)' or
'plt.semilogx(x...)'.
- Limits: we set the limit of the axis with 'plt.xlim(left=,right=)' or
'plt.ylim' with at least using one of them and the other could be default.

2.3. Imshow

'plt.imshow(x)' is used to display data as an image. It is not always


necessary. It is used in the following cases: paint a meshgrid. An example is:
- plt.imshow(np.log(loss_function(P0, P1)), extent=[0, 4, 0, 3],
aspect='auto',
origin='lower', cmap='Greys')

To show the colorbar afterwards, we use 'plt.colorbar(label='log(loss


function)')'. I do not know if it is related, but there is also the part:

'for t in plt.legend().get_texts():
t.set_color('white')'

2.4. Histograms

We can plot a histogram with 'plt.hist(data, bins=, range=, alpha=)'


where 'data' is a list of values, 'bins' is the number of bins, 'range' is the
range of the values of the data that we want to plot and 'alpha' is the
transparency of the plot. 'density=True' normalizes directly.

2.5. Scatter plots

We can plot points in the 2D space but with the feature that we can
choose the size of the marker to be dependent on the position of the grid. We use
'plt.scatter(x,y,s=)'. 's' is the array of the same shape of the data that has the
size for the markers. We can also use the colorbar as it was done in the Imshow,
only putting the label as the argument. To create the color in the plot, we use the
kwarg 'c=,cmap=' where c is the values and cmap the color map used.

2.6. Plot with errorbars

We use 'plt.errorbar(x,y,yerr=,xerr=,...)'.

3. 3D PLOTS

We can also do 3d plots by importing 'from mpl_toolkits import mplot3d'.

We must define 'ax = plt.axes(projection='3d')' and now use 'ax.' instead of


the previous 'plt.'.

We have for example the scatter function 'ax.scatter3D(x,y,z,alpha=,label=',


very similar to the 2D case.

4. MESHGRID

np.meshgrid(array1,array2) returns a list of coordinate matrices from


coordinate vectors. It allows to make N-D coordinate arrays for vectorized
evaluations of N-D scalar/vector fields over N-D grids, given one-dimensional
coordinate arrays x1, x2,…, xn. This means we can plot a function of 2 variables in
3D. The power of this is that when the 2 arrays are not of the same shape we do not
require loops to create the grid of coordinates.

The most basic example is as following:

'''
nx, ny = (3, 2)
x = np.linspace(0, 1, nx) # 3 points for the x axis
y = np.linspace(0, 1, ny) # 2 points for the y axis
xv, yv = np.meshgrid(x, y)
xv # x coordinates of all the points according to the combination of
both x and y points
array([[0. , 0.5, 1. ],
[0. , 0.5, 1. ]])
yv # y coordinates
array([[0., 0., 0.],
[1., 1., 1.]])
'''

The total number of points is 6 in this example and this result is called
coordinate grid. We can print it with using plt.plot(xv,yv) in a normal way. We can
'.ravel()' the two arrays of coordinates and 'zip' them to obtain all the different
points.

The plot can be done with:

ax = fig.gca(projection='3d')
ax.plot_surface(xv, yv, z)

We can change the view with the function: 'ax.view_init(azim=, elev=)'.

In the example from the second lecture he uses the meshgrid to create an
image of the parameter space with the color representing the value they have
associated in the loss function.

You might also like