- First look at the gallery to see the capabilities of Matplotlib.

**Gallery**: http://matplotlib.org/gallery.html - Next read the "Usage" page of the Matplotlib FAQ or proceed with the content of this notebook and return to read the FAQ.

**Usage FAQ**: http://matplotlib.org/faq/usage_faq.html - Now you are ready to look at the Pyplot tutorial.

**Pyplot tutorial**: http://matplotlib.org/users/pyplot_tutorial.html - Next stop should be the Pyplot plotting commands summary.

**Pyplot plotting commands summary**: http://matplotlib.org/api/pyplot_summary.html - Only after browsing all of the above should you tackle the full Matplotlib documentation.

**Matplotlib documetation**: http://matplotlib.org/contents.html

Ignore the following line -- it is somethng needed for Jupyter notebooks.

In [2]:

```
%matplotlib inline
```

`show`

command at the end. You need that to actually display anything produced by `matplotlib`

.

In [3]:

```
from math import pi
import matplotlib.pyplot as plt
from numpy import sin, linspace
x = linspace(-2*pi, 2*pi, 100)
plt.plot(x, sin(x))
plt.show() # your need the show command to actually see the plot
```

Change the vertical limits so that there is some space between the plot and the axis box.

In [4]:

```
from math import pi
import matplotlib.pyplot as plt
from numpy import sin, linspace
x = linspace(-2*pi, 2*pi, 100)
plt.plot(x, sin(x))
plt.ylim([-1.5, 1.5])
plt.show()
```

It would be nice to be able to see the $x$ and $y$ axes. Let's turn those on.

In [5]:

```
from math import pi
import matplotlib.pyplot as plt
from numpy import sin, linspace
x = linspace(-2*pi, 2*pi, 100)
plt.plot(x, sin(x))
plt.ylim([-1.5, 1.5])
plt.axhline(); plt.axvline()
plt.show()
```

In [7]:

```
from math import pi
import matplotlib.pyplot as plt
from numpy import sin, linspace
x = linspace(-2*pi, 2*pi, 100)
plt.plot(x, sin(x))
plt.ylim([-1.5, 1.5])
plt.axhline(color='k'); plt.axvline(color='k')
plt.show()
```

`?plt.axhline`

at the `ipython`

prompt. Instead let's do some other simple things. Say you want to get rid of the box surrounding the figure. Use the `box()`

command to toggle state or `box('on')`

, `box('off')`

, or use a boolean argument.

In [2]:

```
from math import pi
import matplotlib.pyplot as plt
from numpy import sin, linspace
x = linspace(-2*pi, 2*pi, 100)
plt.plot(x, sin(x))
plt.ylim([-1.5, 1.5])
plt.axhline(color='k'); plt.axvline(color='k')
plt.box()
plt.show()
```

This looks ugly. Let's get rid of the axes completely.

In [3]:

```
from math import pi
import matplotlib.pyplot as plt
from numpy import sin, linspace
x = linspace(-2*pi, 2*pi, 100)
plt.plot(x, sin(x))
plt.ylim([-1.5, 1.5])
plt.axhline(color='k'); plt.axvline(color='k')
plt.axis('off')
plt.show()
```

Let's go back to the box axes and remove the black axes intersecting at the origin.

In [4]:

```
from math import pi
import matplotlib.pyplot as plt
from numpy import sin, linspace
x = linspace(-2*pi, 2*pi, 100)
plt.plot(x, sin(x))
plt.ylim([-1.5, 1.5])
plt.show()
```

*markers*). It is also possible to combine a continuous plot and a plot of the markers. The following example shows how to do this. It also shows how to create separate figures.

In [8]:

```
from math import pi
import matplotlib.pyplot as plt
from numpy import sin, linspace
x = linspace(-2*pi, 2*pi, 30)
plt.ylim(-1.5, 1.5)
plt.plot(x, sin(x), 'ro')
plt.figure()
plt.ylim(-1.5, 1.5)
plt.plot(x, sin(x), 'ro')
plt.plot(x, sin(x), 'k-')
```

Out[8]:

In [2]:

```
from math import pi
from matplotlib.pylab import plot, show, title, xlabel, \
ylabel
from numpy import sin, linspace
x = linspace(-2*pi, 2*pi, 30)
plot(x, sin(x), 'k-')
plot(x, sin(x), 'ro')
title('$\sin(\\theta)$')
xlabel('$\\theta$')
ylabel('$\sin(\\theta)$')
```

Out[2]:

In [5]:

```
from math import pi
from matplotlib.pylab import plot, show, title, xlabel, \
ylabel
from numpy import sin, linspace
x = linspace(-2*pi, 2*pi, 30)
plot(x, sin(x), 'k-')
plot(x, sin(x), 'ro')
title('$\sin(\\theta)$', fontsize=20)
xlabel('$\\theta$', fontsize=20)
ylabel('$\sin(\\theta)$', fontsize=20)
grid('on')
```

In an earlier module (`files-string`

we looked at reading, processing and structuring data from files. We will not do some very rudimentary analysis and plotting of that data.

For example, we may want a histogram of the clients' ages or a description of the mean and standard deviation of the clients' ages.

Further, we don't just want a list of numbers but some kind of a visual representation of the dataset.

Let's start out with visualizing our data, since pictures are fun to make.

We will process the client data again, but this time only extracting their ages.

In [2]:

```
def parse(line):
first, last, age, phone = line.split()
return int(age)
ages = [parse(line) for line in open('clients.txt')]
```

Now, let's see how to create a basic histogram - nothing fancy.

In [3]:

```
%matplotlib inline
import matplotlib.pyplot as plt
plt.figure()
plt.hist(ages)
plt.show()
```

In [4]:

```
plt.figure()
plt.title('Age Distribution of Clients')
plt.xlabel('Age')
plt.ylabel('Count')
plt.hist(ages)
plt.show()
```

In [5]:

```
plt.figure()
plt.title('Age Distribution of Clients')
plt.xlabel('Age')
plt.ylabel('Count')
plt.hist(ages)
plt.savefig('age-histogram.png')
```

*describe* function.

In [6]:

```
from scipy.stats import describe
info = describe(ages)
info
```

Out[6]:

Let's try to add this newfound information to our plot!

In [7]:

```
mu = round(info.mean, 2)
sigma = round(info.variance**.5, 2)
plt.figure()
plt.title('Age Distribution of Clients ($\\mu={}$, $\\sigma={}$)'.format(mu, sigma))
plt.xlabel('Age')
plt.ylabel('Count')
plt.hist(ages)
plt.savefig('age-histogram.png')
# The \\ is needed in the title string as \ performs special "escape" commands. For example,
# we've seen that \n produces a newline. \\ just leaves us with a \
```