#!/usr/bin/env python # coding: utf-8 # > This is one of the 100 recipes of the [IPython Cookbook](http://ipython-books.github.io/), the definitive guide to high-performance scientific computing and data science in Python. # # # 6.1. Making nicer matplotlib figures with prettyplotlib # 1. Let's first import NumPy and matplotlib. # In[1]: import numpy as np import matplotlib.pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline') # 2. We first draw several curves with matplotlib. # In[2]: plt.figure(figsize=(6,4)); np.random.seed(12) for i in range(8): x = np.arange(1000) y = np.random.randn(1000).cumsum() plt.plot(x, y, label=str(i)) plt.legend(); # 3. Now, we create the exact same plot with prettplotlib. We can basically replace the `matplotlib.pyplot` namespace with `prettyplotlib`. # In[3]: import prettyplotlib as ppl plt.figure(figsize=(6,4)); np.random.seed(12) for i in range(8): x = np.arange(1000) y = np.random.randn(1000).cumsum() ppl.plot(x, y, label=str(i)) ppl.legend(); # The figure appears clearer, and the colors are more visually pleasant. # 4. Let's show another example with an image. We first use matplotlib's `pcolormesh` function to display a 2D array as an image. # In[4]: plt.figure(figsize=(4,3)); np.random.seed(12) plt.pcolormesh(np.random.rand(16, 16)); plt.colorbar(); # The default *rainbow* color map is known to mislead data visualization. # 5. Now, we use prettyplotlib to display the exact same image. # In[5]: plt.figure(figsize=(4,3)); np.random.seed(12); ppl.pcolormesh(np.random.rand(16, 16)); # This visualization is much clearer, in that high or low values are better brought out than with the rainbow color map. # > You'll find all the explanations, figures, references, and much more in the book (to be released later this summer). # # > [IPython Cookbook](http://ipython-books.github.io/), by [Cyrille Rossant](http://cyrille.rossant.net), Packt Publishing, 2014 (500 pages).