Checking and describing alternatives to matplotlib

Matplotlib is a great module, but it has limitations. The main limitations are:

  • Plots embedded in a notebook with %matplotlib inline are static
  • The colour schemes, and the aesthetics of the plots is a bit old-fashioned, specially for "modern" contexts such as presentations or web-pages.

The second point has been solved to a high extent with the latest release of matplotlib (1.4), where you have styles. Styles are not covered in this notebook, but I highly recommend you to try them to get nicer aesthetics to your figures.

Here we check different alternatives to matplotlib. They may represent the future or simply "something different".

Plotly

Plotly is not in the conda distribution. It has to be installed with pip3 install plotly This is a web based service that needs a (free) registration. Therefore it need a web connection to work. Not that uncommon nowadays... but you can use the demo account, as in these examples.


In [2]:
import plotly

For those users familiar with matplotlib, there is an interface which is similar to matplotlib. You can create the figure with matiplotlib and just export it with plotly. Here is how to do it. Check also the cheatsheet


In [32]:
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import numpy as np
import plotly.plotly as py
import plotly.graph_objs as go

py.sign_in('rcrehuet', '2m7pOV8whgD5rmejAwTY')

In [34]:
# evenly sampled time at 200ms intervals
t = np.arange(0., 5., 0.2)

# Create traces
trace0 = go.Scatter(
    x = t,
    y = t,
#    mode = 'lines',
    line = dict(
    color = ('red'),
    width = 4,
    dash = 'dash'),
    name = '$t$'
)
trace1 = go.Scatter(
    x = t,
    y = t**2,
    mode = 'markers',
    name = '$t^2$'
)
trace2 = go.Scatter(
    x = t,
    y = t**3,
    mode = 'markers',
    name = '$t^3$'
)
data = [trace0, trace1, trace2]

py.iplot(data, filename='line-mode')


Out[34]:

This is how the figure looks like in matplotlib


In [35]:
%matplotlib inline
plt.plot(t, t, 'r--', t, t**2, 'o', t, t**3, 'o')


Out[35]:
[<matplotlib.lines.Line2D at 0x7fd3c47d4da0>,
 <matplotlib.lines.Line2D at 0x7fd3c47d4f60>,
 <matplotlib.lines.Line2D at 0x7fd3c47da8d0>]

You can see that, in fact, both plots look the same. What is the advantage of plotly then? I see three advantages:

  1. Plotly works as an online plotting software, somehow like excel, where you don't have to remember the command-line order to setup a graph.
  2. Plotly has nicer default options than matplotlib. This is alleviated in the 1.4 matplotlib release, where you can easily configure styles, with the style method, and some predefined styles are even available in the distribution.
  3. In matplotlib, either you have an interactive plot in a separate window, or a static inline plot in a notebook. This is a severe limitation that plotly removes, and it is probably the main reason to run away from matiplotlib and try all these alternatives.

The graphics gallery shows some example of graphics. They look nice but the repertoire is still much smaller than the matplotlib gallery.

Saving an image is not trivial... Go to the web interface (play with these data), then "fork and edit", and then you can edit or save it, if you are registered. Saving formats are PNG, PDF, SVG or EPS.


In [37]:
x1 = [1, 2, 3, 4]
y1 = [10, 15, 13, 17]
x2 = [1, 2, 3, 4]
y2 = [16, 5, 11, 9]

In [41]:
trace0 = go.Scatter(x=x1, y=y1)
trace1 = go.Scatter(x=x2, y=y2)
data = [trace0, trace1]
py.iplot(data)


Out[41]:

Bokeh

Bokeh is another visualization package based on JavaScript. Its installation is simple: just type sudo pip3 install bokeh or, if you are using an Anaconda distribution conda install bokeh.

Now we are going to test it.


In [3]:
#EVALUATE THIS ONLY ONCE. OTHERWISE THE PLOTS ARE UGLY. THEY HAVE A PROBLEM HERE...
import bokeh.plotting as bk
bk.output_notebook()
# Plot a `line` renderer setting the color, line thickness, title, and legend value.
TOOLS="resize,crosshair,pan,wheel_zoom,box_zoom,reset,tap,previewsave,box_select,poly_select,lasso_select"


BokehJS successfully loaded.

In [6]:
p = bk.figure(tools=TOOLS)
p.line(x1,y1)
p.line(x2,y2)


Out[6]:
<bokeh.plotting.Figure at 0x7f3e973704a8>

In [8]:
bk.show(p)


Take a look at the gallery to get an impression of the kind of plots Bokeh can do. Its good points are:

  • Interactive plots with lots of tools.
  • No need to be connected to the internet

But it also has limitations:

  • The default plots look ugly. Worse than matplotlib.
  • Importing twice produces ugly tools.
  • Somehow I have experience a strange behaviour in the notebook after using bokeh (for example, shift+enter does not evaluate the cell)

Prettyplotlib

Prettyplotlib was intended to make default nicer plots with matplotlib. It is now discontinued. Its author recommends the use of seaborn, but the new matplotlib styles also superseed prettyplotlib.

Seaborn

Seaborn is a statistical data visualization package. It allows lots of typical statystical graphs that are common in the statistics community but are difficult to obtain directly with matplotlib. See its gallery of examples. Because it is focussed on statistics in can directly plot pandas data structures (covered next week).

But seaborn also defines default styles that look more modern than the default matplotlib styles and thus, it can just be used to use this styles. It also has carefully designed colour palettes.


In [8]:
import seaborn

In [9]:
plt.plot(x1, y1)


Out[9]:
[<matplotlib.lines.Line2D at 0x7f3e8222a7f0>]

MPL tools

MPL Tools adds some tools to matplolib. Probably the best tool is the styles option, which has already been implemented in matplotlib 1.4, so it's better to upgrade matplotlib than to install MPL tools (personal opinion!). Besides, it does not work properly with Python 3.x, or that was my experience.

It can be used to get some different formats of graphs without having to tweak a lot the matplotlib settings. It does not include interactive graphs.


In [10]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

from mpltools import color

# Change default color cycle for all new axes
color.cycle_cmap(2)

plt.plot(x1, y1, 'o-', linewidth=2)
plt.plot(x2, y2, 'o-', linewidth=2)


Out[10]:
[<matplotlib.lines.Line2D at 0x7f3e82284438>]

mpld3

mpld3 "simply" recovers the interactivity of inline matplotlib plots. This is done with the D3js javascript library. If you know some javascript, you can extend mpld3 in a very nice way, including much more interactive options than those originally in matplotlib. As an example, see the gallery. Because the interactivity is coded in javascript, the notebook plots remain interactive even when rendered by the browser directly (i.e. even if you are not connected to an ipython server, so that there isn't any connection to a python kernel).


In [2]:
import mpld3

In [3]:
%matplotlib inline
fig=plt.figure()
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')


Out[3]:
[<matplotlib.lines.Line2D at 0x7f62deebee48>,
 <matplotlib.lines.Line2D at 0x7f62deec31d0>,
 <matplotlib.lines.Line2D at 0x7f62deec3908>]

We can render this plot interactive with:


In [4]:
mpld3.display(fig)


Out[4]:

If you'd like to use mpld3 by default for every figure you generate, you can call the following command:

mpld3.enable_notebook()