{}

Index

Visualization of Data

Visualization

In [1]:
import numpy as np
import matplotlib.pyplot as pl

Line plots

In [70]:
x = np.linspace(-5, 5, 100)
y = np.sin(x)

pl.plot(x, y);
In [15]:
# We can control the styling
pl.plot(x, y, color='r', linestyle='--');
In [14]:
#
# We can superimpose multiple lines
#
x2 = np.linspace(-5, 5, 10)
y2 = np.sin(x2)

pl.plot(x, y, color='r')
pl.plot(x2, y2, marker='o', linestyle="");
In [22]:
#
# We can also add text annotations
#
x1 = 2.1; y1 = np.sin(x1)
x2 = np.pi + 0.1; y2 = np.sin(x2)

pl.plot(x, y)
pl.text(x1, y1, 'higher')
pl.text(x2, y2, 'lower')
Out[22]:
Text(3.241592653589793, -0.09983341664682811, 'lower')
In [72]:
#
# Manually set the limits
#
x1 = 2.1; y1 = np.sin(x1)
x2 = np.pi + 0.1; y2 = np.sin(x2)

pl.plot(x, y)
pl.text(x1, y1, 'higher')
pl.text(x2, y2, 'lower')

pl.xlim(-10, 10)
pl.ylim(-2, 2);

Area plots

In [23]:
xs = np.linspace(-5, 5, 100)
def f1(x):
    return x * x
def f2(x):
    return 3*x + 4
In [25]:
pl.plot(xs, f1(xs))
pl.plot(xs, f2(xs))
Out[25]:
[<matplotlib.lines.Line2D at 0x7f0624ff6750>]
In [27]:
pl.plot(xs, f1(xs))
pl.plot(xs, f2(xs))
pl.fill_between(xs, f1(xs), f2(xs), color='gray');
In [29]:
pl.plot(xs, f1(xs))
pl.plot(xs, f2(xs))
pl.fill_between(xs, f1(xs), f2(xs), where=f1(xs) < f2(xs), color='gray');
In [43]:
#
# A limitation
#
x1 = np.linspace(-5, 5, 40)
x2 = np.linspace(-4, 4, 80)
y1 = np.sin(x1)
y2 = np.cos(x2)

pl.plot(x1, y1, marker='o')
pl.plot(x2, y2, linestyle='--', marker='o');
In [44]:
#
# This fails because y2 and y1 are different lengths.
#
pl.plot(x1, y1)
pl.plot(x2, y2, linestyle='--')
pl.fill_between(x1, y1, y2)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-44-b5ce3afb3f03> in <module>
      4 pl.plot(x1, y1)
      5 pl.plot(x2, y2, linestyle='--')
----> 6 pl.fill_between(x1, y1, y2)

~/anaconda3/lib/python3.7/site-packages/matplotlib/pyplot.py in fill_between(x, y1, y2, where, interpolate, step, data, **kwargs)
   2590     return gca().fill_between(
   2591         x, y1, y2=y2, where=where, interpolate=interpolate, step=step,
-> 2592         **({"data": data} if data is not None else {}), **kwargs)
   2593 
   2594 

~/anaconda3/lib/python3.7/site-packages/matplotlib/__init__.py in inner(ax, data, *args, **kwargs)
   1597     def inner(ax, *args, data=None, **kwargs):
   1598         if data is None:
-> 1599             return func(ax, *map(sanitize_sequence, args), **kwargs)
   1600 
   1601         bound = new_sig.bind(ax, *args, **kwargs)

~/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_axes.py in fill_between(self, x, y1, y2, where, interpolate, step, **kwargs)
   5242             where = True
   5243         where = where & ~functools.reduce(np.logical_or,
-> 5244                                           map(np.ma.getmask, [x, y1, y2]))
   5245 
   5246         x, y1, y2 = np.broadcast_arrays(np.atleast_1d(x), y1, y2)

ValueError: operands could not be broadcast together with shapes (40,) (80,) 
In [45]:
y1_interp = np.interp(x2, x1, y1)
y1_interp.shape
Out[45]:
(80,)
In [46]:
pl.plot(x1, y1)
pl.plot(x2, y2, linestyle='--')
pl.fill_between(x2, y1_interp, y2, where=y1_interp < y2)
Out[46]:
<matplotlib.collections.PolyCollection at 0x7f0625a5aa10>

Error plots

In [51]:
xs = np.linspace(-5, 5, 10)
ys = np.sin(xs)
errs = np.random.random(len(xs))
errs
Out[51]:
array([0.67437957, 0.65731275, 0.10624474, 0.27883236, 0.55785646,
       0.05681281, 0.04248705, 0.6100362 , 0.68089623, 0.5112584 ])
In [52]:
#
# Each point has error 2*errs.
#
pl.errorbar(xs, ys, yerr=errs, capsize=3);
In [54]:
#
# Asymmetric errors
#
err_low = np.random.random(len(xs)) * 0.2
err_hi = np.random.random(len(xs)) * 0.1
errs = np.array([err_low, err_hi])
errs
Out[54]:
array([[0.09028524, 0.15667844, 0.13270258, 0.08077813, 0.1741719 ,
        0.04341984, 0.06995846, 0.03139628, 0.05228361, 0.18537323],
       [0.02243741, 0.04079118, 0.09860552, 0.02533219, 0.0465428 ,
        0.09717926, 0.06045159, 0.00660893, 0.02009699, 0.03619972]])
In [56]:
pl.errorbar(xs, ys, yerr=errs, capsize=0, elinewidth=5, color='gray')
Out[56]:
<ErrorbarContainer object of 3 artists>

Bar Plots

In [77]:
x = np.linspace(-1, 1, 5)
y1 = np.random.random(len(x))
y2 = np.random.random(len(x))
In [78]:
pl.bar(x, y1, width=0.2);
In [79]:
#
# Side-by-side plots
#
pl.bar(x, y1, width=0.2)
pl.bar(x+0.2, y2, width=0.2);
In [82]:
#
# Adjust bottom
#
pl.bar(x, y2, width=0.2, bottom=0.5);
pl.ylim(0, 2)
Out[82]:
(0, 2)
In [84]:
#
# Adjust bottom
#
pl.bar(x, y1, width=0.2)
pl.bar(x, y2, width=0.2, bottom=y1);
pl.ylim(0, 2)
Out[84]:
(0, 2)

Contour Plots

Consider a 2D grid

(x1,y1) (x2,y1) (x3,y1) ... (xn, y1)
(x1,y2) (x2,y2) (x3,y3) ... (xn, y2)
.
.
.
(x1,ym) (x2,ym) (x3,ym) ... (xn,ym)

At each coordinate (xi,yj), we can have height z[i,j].

Contour plot visualizes the heights as a 2D plot.

In [91]:
x = np.linspace(-10, 10, 100)
y = np.linspace(-10, 10, 100)
xgrid, ygrid = np.meshgrid(x, y)
In [94]:
xgrid.shape
Out[94]:
(100, 100)
In [92]:
z = xgrid * xgrid + ygrid * ygrid
In [93]:
z.shape
Out[93]:
(100, 100)
In [95]:
colormap = pl.get_cmap('jet')
In [98]:
pl.contourf(xgrid, ygrid, z, levels=10, cmap=colormap);
In [103]:
pl.contour(xgrid, ygrid, z, colors='gray');

3D surface plot

In [107]:
from mpl_toolkits.mplot3d import Axes3D
In [108]:
ax = pl.gca(projection ='3d')
ax.plot_wireframe(xgrid, ygrid, z);
In [121]:
#
# Default view is:
#   elevation = 30
#   azimuth = 45
#

elevation = 0
azimuth = 45 - 30
ax = pl.gca(projection ='3d')
ax.view_init(elevation, azimuth)
ax.plot_wireframe(xgrid, ygrid, z);
In [ ]: