#!/usr/bin/env python # coding: utf-8 # #
Matplotlib # Matplotlib is a python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. matplotlib can be used in python scripts, the python and ipython shell, web application servers, and six graphical user interface toolkits. # # # Matplotlib tries to make easy things easy and hard things possible. You can generate plots, histograms, power spectra, bar charts, errorcharts, scatterplots, etc, with just a few lines of code. For a sampling, see the screenshots, thumbnail gallery, and examples directory # # # For simple plotting the pyplot interface provides a MATLAB-like interface, particularly when combined with IPython. For the power user, you have full control of line styles, font properties, axes properties, etc, via an object oriented interface or via a set of functions familiar to MATLAB users. # # # [**Official page**](http://matplotlib.org/) # - - - # # - [Basic Use](#Basic-Use) # - [Formating Figures](#Formating-Figures) # - [Gallery](#Gallery) # # - - - # # Basic Use # One of the most appealing advantages of Matplotlib is its versatility. You can use it in a very basic way as well as in a very customizable way, allowing a total control of what we want to sketch. We illustrate here some of the basic functions. # In[1]: #Importing numpy import numpy as np #Ignore me!! (for scripts) get_ipython().run_line_magic('pylab', 'inline') #Importing matplotlib import matplotlib.pyplot as plt # The line **%pylab inline** is always necessary when using a IPython notebook. If you are working on the interpreter or with scripts, always use the function **plt.show()** in order to display the resulting image. # In[2]: #Ploting a function X = np.linspace( 0, 2*np.pi, 100 ) Y = np.sin( X ) plt.plot( X, Y ) # In[6]: #Scatter of points X = np.random.random(200) Y = np.random.random(200) plt.plot( X, Y, 'o' ) # In[7]: #Multiple plots #Ploting a function X = np.linspace( 0, 2*np.pi, 100 ) Y1 = np.sin( X ) Y2 = np.cos( X ) plt.plot( X, Y1 ) plt.plot( X, Y2 ) # In[8]: #Logaritmic axis Y X = np.linspace( 1, 10, 100 ) Y = np.exp( X ) plt.semilogy( X, Y ) # In[9]: #Logaritmic axis X X = np.linspace( 1, 10, 100 ) Y = np.log( X ) plt.semilogx( X, Y ) # In[14]: #Double Logaritmic axis X = np.linspace( 0.1, 16.5*np.pi, 1000 ) Y = np.sin( X )**2 plt.loglog( X, Y ) # In[18]: #Polar plots r = np.arange( 0, 3.0, 0.01 ) theta = 2*np.pi*r plt.subplot(111, polar=True) plt.plot( theta, r ) # In[23]: #Fill between X = np.arange( 0, 2, 0.1 ) Y1 = X/2. Y2 = X**2 + 1 plt.fill_between( X, Y1, Y2 ) # In[27]: #Multiple figures X = np.arange( 0, 10, 0.1 ) #First plot plt.subplot( 1, 2, 1 ) plt.plot( X, X**2+1 ) #Second plot plt.subplot( 1, 2, 2 ) plt.semilogy( X, np.exp(X-10) ) # In[33]: #Histograms X = np.random.random( 500 ) plt.hist( X ) # - - - # # Formating Figures # Matplotlib supports complex formatting, even allowing LaTeX expressions. These features makes Matplotlib a very appealing package for making professional figures, for example for a scientific paper. Next it is shown an example (**script**) where you can see some of the capabilities of Matplotlib # In[79]: #! /usr/bin/python #==================================================================== #Importing libraries #==================================================================== import numpy as np import matplotlib.pyplot as plt #Arrays X = np.linspace( 0, 10, 100 ) Y = X**0.5 Y1 = Y+0.5*(1+np.random.random(100)) Y2 = Y-0.5*(1+np.random.random(100)) #Setting the figure environment plt.figure( figsize = (16,6) ) #==================================================================== #First plot #==================================================================== plt.subplot( 1, 2, 1 ) plt.plot( X, Y ) plt.title( "This is an unformatted plot" ) #==================================================================== #Second plot #==================================================================== plt.subplot( 1, 2, 2 ) #2D Plots plt.plot( X, Y, linewidth=4, color="green", label="Function" ) plt.plot( X, Y1, linewidth=2, color="green" ) plt.plot( X, Y2, linewidth=2, color="green" ) #Error region plt.fill_between( X, Y1, Y2, color="green", alpha=0.2 ) #Grid plt.grid( True ) #X-axis limits plt.xlim( (0, 10) ) #Y-axis limits plt.ylim( (0, 5) ) #Label plt.legend( loc="upper left", fontsize=16 ) #X label plt.xlabel( "$x$ coordinate", fontsize=20 ) #Y label plt.ylabel( "$\sqrt{x}$", fontsize=20 ) #Title plt.title( "This is a formatted plot", fontsize=16 ) #==================================================================== #Showing #==================================================================== #You can store this figure in any format, even in vectorial formats #like pdf plt.savefig( "Figure.pdf" ) #Showing the figure on screen plt.show() # - - - # # Gallery # This gallery comprehends some interesting advanced uses of matplotlib. # In[70]: from IPython.core.display import Image Image(filename='./figures/voids.png') # In[77]: Image(filename='./figures/VPHphase.png') # In[78]: Image(filename='./figures/simulation.png') # In[75]: Image(filename='./figures/voiddensity.png') # In[76]: Image(filename='./figures/halosfraction.png') # - - -