03.04 Matplotlib Exercises

Exercises rating:

★☆☆ - You should be able to do it based on Python knowledge plus the text.

★★☆ - You will need to do extra thinking and some extra reading/searching.

★★★ - The answer is difficult to find by a simple search, requires you to do a considerable amount of extra work by yourself (feel free to ignore these exercises if you're short on time).

Let's first import and configure matplotlib with sensible defaults:

In [ ]:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
%matplotlib inline

1. Create subplots. (★☆☆)

Create six plots in the same figure, using a two column and three column configuration. Plot np.sin(x) and np.cos(x) or any custom function in these plots.

Please do the same exercise for the MATLAB (plt) interface and for the Pythonic (ax) interface.

In [ ]:
In [ ]:

2. Plot on the same figure. (★☆☆)

Create four plots in the same figure, for any four custom functions $f()$. Your $x$ should have at least 10000 samples. The plots should be correctly labeled and have the appropriate limits for easy viewing.

Please do the same exercise for the MATLAB (plt) interface and for the Pythonic (ax) interface

In [ ]:
In [ ]:

3. Scatter plot. (★★☆)

Using the digits dataset from sklearn, plot four dimensions at random, in the same manner we did for the iris dataset.

In [ ]:
from sklearn.datasets import load_digits
digits = load_digits()
In [ ]:

4. Plot a saddle. (★★☆)

Plot the typical saddle function:

$$ z = x^2 - y^2 $$

in a three dimensional plot.

In [ ]:

5. Fill areas. (★★☆)

Using the following example data, please use fill_between to fill regions between these lines in different combinations. Fill between y1 and y2, between y2 and y3, and between y1 and y3. For each area use a different color and use a good alpha (by trial and errors) to make all areas visible.

In [ ]:
x  = np.linspace(0, 3, 200)
y1 = x ** 2 + 3
y2 = np.exp(x) + 2
y3 = np.cos(x)
In [ ]:

6. Plot normal distributions. (★☆☆)

Using np.random.normal with different centers and std, please plot (on the same plot), 10 different distributions. Each distribution should have at least 2048 samples and you should use 100 bins for that. (Make it look appealing visually: a good choice is to select a small range of centres and a small range of standard deviations, and use a low alpha= parameter.)

In [ ]: