# Matplotlib Exercises

Welcome to the exercises for reviewing matplotlib! Take your time with these, Matplotlib can be tricky to understand at first. These are relatively simple plots, but they can be hard if this is your first time with matplotlib, feel free to reference the solutions as you go along.

Also don't worry if you find the matplotlib syntax frustrating, we actually won't be using it that often throughout the course, we will switch to using seaborn and pandas built-in visualization capabilities. But, those are built-off of matplotlib, which is why it is still important to get exposure to it!

NOTE: ALL THE COMMANDS FOR PLOTTING A FIGURE SHOULD ALL GO IN THE SAME CELL. SEPARATING THEM OUT INTO MULTIPLE CELLS MAY CAUSE NOTHING TO SHOW UP.

# Exercises

Follow the instructions to recreate the plots using this data:

## Data

``````

In :

import numpy as np
x = np.arange(0,100)
y = x*2
z = x**2

``````

Import matplotlib.pyplot as plt and set %matplotlib inline if you are using the jupyter notebook. What command do you use if you aren't using the jupyter notebook?

``````

In :

import matplotlib.pyplot as plt
%matplotlib inline

``````

## Exercise 1

• Create a figure object called fig using plt.figure()
• Use add_axes to add an axis to the figure canvas at [0,0,1,1]. Call this new axis ax.
• Plot (x,y) on that axes and set the labels and titles to match the plot below:
``````

In :

fig1 = plt.figure()
ax.plot(x,y)
ax.set_title("title")
ax.set_xlabel("x")
ax.set_ylabel("y")

``````
``````

Out:

<matplotlib.text.Text at 0x1120aed68>

``````

## Exercise 2

Create a figure object and put two axes on it, ax1 and ax2. Located at [0,0,1,1] and [0.2,0.5,.2,.2] respectively.

``````

In :

fig2 = plt.figure()

``````
``````

``````

Now plot (x,y) on both axes. And call your figure object to show it.

``````

In :

ax1.plot(x,y)
ax2.plot(x,y)
fig2

``````
``````

Out:

``````

## Exercise 3

Create the plot below by adding two axes to a figure object at [0,0,1,1] and [0.2,0.5,.4,.4]

``````

In :

fig2 = plt.figure()

``````
``````

``````

Now use x,y, and z arrays to recreate the plot below. Notice the xlimits and y limits on the inserted plot:

``````

In :

axb1.plot(x,z)
axb1.set_xlabel("X")
axb1.set_ylabel("Z")

axb2.plot(x,y)
axb2.set_xlabel("X")
axb2.set_ylabel("Y")
axb2.set_title("zoom")
axb2.set_xlim(20,22)
axb2.set_ylim(30,50)

fig2

``````
``````

Out:

``````

## Exercise 4

Use plt.subplots(nrows=1, ncols=2) to create the plot below.

``````

In :

plt.subplots(1,2)

``````
``````

Out:

(<matplotlib.figure.Figure at 0x1126817b8>,
array([<matplotlib.axes._subplots.AxesSubplot object at 0x112494898>,
<matplotlib.axes._subplots.AxesSubplot object at 0x1128d17b8>], dtype=object))

``````

Now plot (x,y) and (x,z) on the axes. Play around with the linewidth and style

``````

In :

fig3, axn = plt.subplots(1,2)
axn.plot(x,y, color='blue', linestyle='--', linewidth=3)
axn.plot(x,z, color='red', linewidth=3)

``````
``````

Out:

[<matplotlib.lines.Line2D at 0x115705dd8>]

``````

See if you can resize the plot by adding the figsize() argument in plt.subplots() are copying and pasting your previous code.

``````

In :

fig4, axn1 = plt.subplots(1,2, figsize=[10,2])
axn1.plot(x,y, color='blue', marker='.', linewidth=3)
axn1.plot(x,z, color='red', linewidth=3)

``````
``````

Out:

[<matplotlib.lines.Line2D at 0x115630b70>]

``````