Python BootCamp 2017

Data Display

During this talk I will show you the basics of displaying data using MatPlotLib and Seaborn. For that, we will have to check some of the NumPy basic commands in order to create and manipulate arrays and also how to input/output data from files.

Before we start doing very complicated things using NumPy, let us create a very simple plot and play a bit with it. As you will learn with time, there are a tons of ways of doing plots using MatPlotLib. If you are on Mac OSX, you may have some issues and I recommend you read this.

If you are running this as a Notebook, you may have to execute the following line before starting:


In [1]:
%matplotlib inline

This will turn off the graphical interface and make all the plots to be shown as static figures on your navigator.

My first plot

Let's start with a very simple example where I can simply


In [2]:
from matplotlib.pyplot import *
x = [0, 1, 2, 3, 4]
y = [4, 1, 0, 1, 4]
plot(x, y)
show()


If you want to save this to a file, let's say to file plot_001.png, you can substitute the show() command by savefig('plot_001.png').

As it was explained before, the from ... import * is a bad practice. So we would prefer to keep the package name so anyone that reads the code knows where the methods, variables and etc. come from.


In [3]:
import matplotlib.pyplot as plt
x = [0, 1, 2, 3, 4]
y = [4, 1, 0, 1, 4]
plt.plot(x, y)
plt.savefig('fig/plot_001A.png', dpi=48, transparent=True)
plt.show()


Everytime you call plot without specifying what is the color and the mark style to be used, it will assume continuous line and follow an internal sequence of colors.


In [4]:
plot(x, y)
plot(x, y)
savefig('fig/plot_001B.png', dpi=96, transparent=True)
show()



In [5]:
plot(x, y)
plot(x, y)
plot(x, y)
savefig('plot_001C.png', dpi=96, transparent=True)
show()


As we could see, the line style has been changing for each plot and but we did not have any control over them. So now let's plot with style.

If I want plots with more points, we can use either numpy.arange or numpy.linspace.


In [6]:
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(5)
y = (x - 2) ** 2
plt.plot(x, y)
plt.savefig('fig/plot_001D.png', dpi=96, transparent=True)



In [7]:
x = np.arange(-5, 5, 0.1)
y = x ** 2
plt.plot(x, y)
plt.savefig('fig/plot_001E.png', dpi=96, transparent=True)



In [8]:
x = np.linspace(-5, 5, 100)
y = x ** 2
plt.plot(x, y)
plt.savefig('fig/plot_001F.png', dpi=96, transparent=True)


Plot with style

MatPlotLib allows us to customize a lot of things when we are doing plots. One of them is the style of the data that is being displayed. One can change that by simply adding an argument to the plot() method. This argument usually has two characters: one for the color and another one for the line/marker style. The reader can find some documentation here.


In [9]:
def f(t):
    return (t - 2) ** 2

t1 = np.arange(0.0, 5.0, 0.5)
t2 = np.arange(0.0, 5.0, 0.02)

plt.plot(t1, f(t1), 'ro', t2, f(t2), 'k')
plt.savefig("fig/plot_002A.png", transparent=True, dpi=96)



In [10]:
plt.plot(t1, f(t1), 'ro', t2, f(t2), 'k')
plt.xlabel("t [s]")
plt.ylabel(u"f(t) = (t – 2)$^2$")
plt.savefig("fig/plot_002B.png", transparent=True, dpi=96)



In [11]:
plt.plot(t1, f(t1), 'ro', t2, f(t2), 'k')
plt.xlabel("t [s]", fontsize=24)
plt.ylabel(u"f(t) = (t – 2)$^2$", fontsize=24)
plt.savefig("fig/plot_002C.png", transparent=True, dpi=96)



In [12]:
plt.plot(t1, f(t1), 'ro', t2, f(t2), 'k')

plt.xticks(fontsize=18)
plt.xlabel("t [s]", fontsize=24)
plt.ylabel(u"f(t) = (t – 2)$^2$", fontsize=24)

plt.savefig("fig/plot_002D.png", transparent=True, dpi=96)



In [13]:
import matplotlib as mpl
mpl.rcParams['font.size'] = 18

plt.plot(t1, f(t1), 'ro', t2, f(t2), 'k')

plt.xlabel("t [s]")
plt.ylabel(u"f(t) = (t – 2)$^2$")

plt.savefig("fig/plot_002E.png", transparent=True, dpi=96)



In [14]:
plt.plot(t1, f(t1), 'ro', t2, f(t2), 'k')

plt.title("A simple plot")
plt.xlabel("t [s]")
plt.ylabel(u"f(t) = (t – 2)$^2$")

plt.savefig("fig/plot_002F.png", transparent=True, dpi=96)



In [15]:
plt.plot(t1, f(t1), 'ro', t2, f(t2), 'k')

plt.title("A simple plot")
plt.xlabel("t [s]")
plt.ylabel(u"f(t) = (t – 2)$^2$")
plt.grid()

plt.savefig("fig/plot_002F.png", transparent=True, dpi=96)



In [16]:
plt.plot(t1, f(t1), 'ro', label="Points")
plt.plot(t2, f(t2), 'k', label="Line")

plt.title("A simple plot")
plt.xlabel("t [s]")
plt.ylabel(u"f(t) = (t – 2)$^2$")
plt.grid()
plt.legend()

plt.savefig("fig/plot_002G.png", transparent=True, dpi=96)



In [17]:
plt.plot(t1, f(t1), 'ro', label="Points")
plt.plot(t2, f(t2), 'k', label="Line")

plt.title("A simple plot")
plt.xlabel("t [s]")
plt.ylabel(u"f(t) = (t – 2)$^2$")
plt.grid()
plt.legend()

plt.tight_layout()
plt.savefig("fig/plot_002H.png", transparent=True, dpi=96)


Types of plots

MatPlotLib offers a large variety of different types of plots. Which plot you may be using really deppends on your data. Here are some examples (most of them were taken from the MatPlotLib Gallery).

Before that, let me customize these plots a bit (this will be explained later).


In [18]:
#plt.style.use("seaborn-talk")
#mpl.rcParams['xtick.labelsize'] = 18
#mpl.rcParams['ytick.labelsize'] = 18
#mpl.rcParams['axes.labelsize'] = 18
#mpl.rcParams['savefig.dpi'] = 80
mpl.rcParams['savefig.transparent'] = True

2D Plot


In [19]:
x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)

y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)

plt.subplot(2, 1, 1)
plt.plot(x1, y1, 'o-')

plt.subplot(2, 1, 2)
plt.plot(x2, y2, '.-')

plt.savefig("fig/ex_subplot.png")


Scatter Plot


In [20]:
mean, std, size = 3., 5., 100
x1 = np.random.normal(mean, std, size)
y1 = np.random.normal(mean, std, size)
z1 = np.random.randint(1, 200 + 1, size)
plt.scatter(x1, y1, s=z1, color="blue", alpha=0.5)

mean, std, size = -3., 5., 100
x2 = np.random.normal(mean, std, size)
y2 = np.random.normal(mean, std, size)
z2 = np.random.randint(1, 200 + 1, size)
plt.scatter(x2, y2, s=z2, color="red", alpha=0.5)

plt.savefig("fig/ex_scatter.png", transparent=True, dpi=96)


Histograms


In [21]:
mean, std, size = 0., 5., 100
x = np.random.normal(mean, std, size)
plt.hist(x, alpha=0.5, color="blue")

mean, std, size = 10., 5., 100
x = np.random.normal(mean, std, size)
plt.hist(x, alpha=0.5, color="red")

plt.savefig("fig/ex_hist.png")


Errorbar


In [22]:
x = np.arange(0.1, 4, 0.5)
y = np.exp(-x)
yerr = np.random.rand(x.size)
plt.errorbar(x, y, xerr=0.2, yerr=yerr, fmt='+', linewidth=2)
plt.savefig("fig/ex_errorbar.png")


Pie Plot


In [23]:
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
colors = ['yellowgreen', 'gold', 'lightskyblue', 'darkorange']

plt.pie(sizes, autopct='%1.0f%%', colors=colors, labels=labels, startangle=90)
plt.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.

plt.savefig('fig/ex_pieplot.png')


Display Images


In [24]:
np.random.seed(0)
grid = np.random.rand(10, 10)
plt.imshow(grid, interpolation='none', cmap='viridis', origin="lower")

plt.savefig("fig/ex_figure.png")


Polar Plots


In [25]:
N = 20
np.random.seed(10)
theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False)
radii = 10 * np.random.rand(N)
width = np.pi / 4 * np.random.rand(N)

ax = plt.subplot(111, projection='polar')
bars = ax.bar(theta, radii, width=width, bottom=0.0)

# Use custom colors and opacity
for r, bar in zip(radii, bars):
    bar.set_facecolor(plt.cm.viridis(r / 10.))
    bar.set_alpha(0.75)

plt.savefig("fig/ex_polarplot.png")


Stream Plot


In [26]:
Y, X = np.mgrid[-3:3:100j, -3:3:100j]
U = -1 - X**2 + Y
V = 1 + X - Y**2
speed = np.sqrt(U*U + V*V)

lw = 5 * speed / speed.max()
strm = plt.streamplot(X, Y, U, V, density=0.7, color=U, linewidth=lw, cmap=plt.cm.viridis)
plt.colorbar(strm.lines)

plt.savefig("fig/ex_streamplot.png")


Plot 3D


In [27]:
from mpl_toolkits.mplot3d import axes3d

matplotlib.rcParams['xtick.direction'] = 'out'
matplotlib.rcParams['ytick.direction'] = 'out'

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Grab some test data.
X, Y, Z = axes3d.get_test_data(0.05)

# Plot a basic wireframe.
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)

plt.savefig("fig/ex_3d.png")


Contour Plot


In [28]:
im = plt.imshow(Z, cmap="gray", origin="lower", interpolation="nearest")

CS = plt.contour(Z, origin="lower", cmap="RdYlBu")
plt.clabel(CS, inline=1, fontsize=10)

plt.savefig("fig/ex_contour.png")


Styles

MatPlotLib also allow us to change the color and the appareance by using different styles. It comes with a set of pre-defined styles but the user can create his/her own. Here are some examples:


In [29]:
print(plt.style.available)


[u'seaborn-darkgrid', u'seaborn-notebook', u'classic', u'seaborn-ticks', u'grayscale', u'bmh', u'seaborn-talk', u'dark_background', u'ggplot', u'fivethirtyeight', u'seaborn-colorblind', u'seaborn-deep', u'seaborn-whitegrid', u'seaborn-bright', u'seaborn-poster', u'seaborn-muted', u'seaborn-paper', u'seaborn-white', u'seaborn-pastel', u'seaborn-dark', u'seaborn-dark-palette']

In [30]:
styles = ['default', 'classic', 'grayscale', 'ggplot', 'bmh',  'seaborn-ticks', 'seaborn-colorblind']



for temp_style in styles:
    print(temp_style)
    with plt.style.context((temp_style)):
        
        prng = np.random.RandomState(96917002)
        
        fig, axes = plt.subplots(ncols=2, nrows=2)
        ax1, ax2, ax3, ax4 = axes.ravel()

        # 2D Plot
        for mu, sigma, marker in [(-.5, 0.75, 'o'), (0.75, 1., 's')]:
            x, y = prng.normal(loc=mu, scale=sigma, size=(2, 100))
            ax1.plot(x, y, ls='none', marker=marker)

        # sinusoidal lines with colors from default color cycle
        L = 2*np.pi
        x = np.linspace(0, L)
        ncolors = len(plt.rcParams['axes.prop_cycle'])
        shift = np.linspace(0, L, ncolors, endpoint=False)
        for s in shift:
            ax2.plot(x, np.sin(x + s), '-')
        ax2.margins(0)

        # bar graphs
        x = np.arange(5)
        y1, y2 = prng.randint(1, 25, size=(2, 5))
        width = 0.25
        ax3.bar(x, y1, width)
        ax3.bar(x + width, y2, width,
                color=list(plt.rcParams['axes.prop_cycle'])[2]['color'])
        ax3.set_xticks(x + width)
        ax3.set_xticklabels(['a', 'b', 'c', 'd', 'e'])

        # circles with colors from default color cycle
        for i, color in enumerate(plt.rcParams['axes.prop_cycle']):
            xy = prng.normal(size=2)
            ax4.add_patch(plt.Circle(xy, radius=0.3, color=color['color']))
        ax4.axis('equal')
        ax4.margins(0)

        plt.tight_layout()
        plt.savefig("fig/style_{:s}.png".format(temp_style), transparent=True, dpi=72)


default
classic
grayscale
ggplot
bmh
seaborn-ticks
seaborn-colorblind

Colormaps


In [31]:
cmaps = ['jet', 'gray', 'viridis', 'cubehelix', 'RdYlBu']

for cm in cmaps:
    plt.figure()
    im = plt.imshow(Z, origin="lower", interpolation="nearest", cmap=cm)
    cb = plt.colorbar(im)
    plt.savefig("fig/imshow_{:s}.png".format(cm), transparent=True, dpi=72)


A little tip


In [32]:
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(5) - 2
y = x ** 2

fig = plt.figure(figsize=(4 / 0.8, 3 / 0.8))
ax = fig.add_subplot(111)

ax.plot(x, y, 'o')
ax.set_xlim(-3, 3)
ax.set_ylim(-1, 5)

plt.savefig('fig/advanced.png', transparent=True)



In [ ]: