Introduction to plotting with matplotlib

In [3]:
# IGNORE THE LINE BELOW, IT IS ONLY FOR CREATING THIS SCRIPT!
%matplotlib inline
In [4]:
import math

x = []
dx = 0.25
for i in range(30):
    x.append(i * dx)
    
y = []
for xi in x:
    y.append(math.sin(xi))
    

With list comprehension:

In [5]:
x = [i * dx for i in range(30)]
y = [math.sin(xi) for xi in x]
In [6]:
import pylab
pylab.plot(x, y)
pylab.show()

Blue is the standard color. To change this use:

In [7]:
pylab.plot(x, y, "green")
pylab.show()

To plot dots, eg red dots:

In [8]:
pylab.plot(x, y, "r.")
pylab.show()

Overlay multiple plots

In [9]:
y2 = [math.cos(xi) for xi in x]

pylab.plot(x, y)
pylab.plot(x, y, "ro")   # "o" means: big dots
pylab.plot(x, y2, "green")
pylab.show()

Adding grids

In [10]:
pylab.plot(x, y)
pylab.grid(True)
pylab.show()

Adding labels to the plot

In [11]:
pylab.plot(x, y, label="wave")
pylab.plot(x, y2, label="shifted wave")
pylab.grid(True)
pylab.xlabel("time")
pylab.ylabel("amplitude")
pylab.title("sine wave over time")
pylab.legend()  # activates the legent in the upper corner
pylab.show()

Arranging multiple plots

In [12]:
# 1 row, 2 columns ,first plot:
pylab.subplot(1, 2, 1) 
pylab.plot(x, y, label="sin")
pylab.legend()
# 1 row, 2 columns, second plot:
pylab.subplot(1, 2, 2)
pylab.plot(x, y2, "green")
pylab.grid(True)
pylab.show()

How to store a plot to the file system

In [13]:
pylab.plot(x, y)
pylab.savefig("sin.png")
!ls -l sin.png
-rw-r--r--  1 uweschmitt  staff  10689  5 Jul 17:13 sin.png

Some exampels for other types of plots

In [14]:
import random
import math
N = 50

x = [random.normalvariate(0, 1) for i in range(N)]
y = [random.normalvariate(0, 1) for i in range(N)]
areas = [math.pi * 125 * random.random() ** 2 for i in range(N)]
colors = [random.random() for i in range(N)]
In [15]:
pylab.scatter(x, y, s=areas, c=colors, alpha=0.5)
pylab.show()
In [22]:
N = 2500

values_1 = [random.normalvariate(0, 1) for i in range(N)]
values_2 = [random.normalvariate(0.5, 0.3) for i in range(N)]
pylab.hist(values_1, bins=50, color="green", alpha=0.3, histtype="stepfilled")
pylab.hist(values_2, bins=50, color="red", alpha=0.6)
pylab.show()

Thats not all folks, many more examples at:

http://matplotlib.org/gallery.html

In [ ]:
# THE LINES BELOW ARE JUST FOR FORMATTING THE INSTRUCTIONS ABOVE !
from IPython import utils
from IPython.core.display import HTML
import os
def css_styling():
    """Load default custom.css file from ipython profile"""
    base = utils.path.get_ipython_dir()
    styles = """<style>
    
    @import url('http://fonts.googleapis.com/css?family=Source+Code+Pro');
    
    @import url('http://fonts.googleapis.com/css?family=Kameron');
    @import url('http://fonts.googleapis.com/css?family=Crimson+Text');
    
    @import url('http://fonts.googleapis.com/css?family=Lato');
    @import url('http://fonts.googleapis.com/css?family=Source+Sans+Pro');
    
    @import url('http://fonts.googleapis.com/css?family=Lora'); 

    
    body {
        font-family: 'Lora', Consolas, sans-serif;
      
    }
    .rendered_html code
    {
        color: black;
        background: #eaf0ff;
        padding: 1pt;
        font-family:  'Source Code Pro', Consolas, monocco, monospace;
    }
    
    .CodeMirror pre {
    font-family: 'Source Code Pro', monocco, Consolas, monocco, monospace;
    }
    
    .cm-s-ipython span.cm-keyword {
        font-weight: normal;
     }
     
     strong {
         background: #ffe7e7;
         padding: 1pt;
     }
     
    
    div #notebook {
        # font-size: 10pt; 
        line-height: 145%;
        }
        
    li {
        line-heigt: 145%;
    }

    div.output_area pre {
        background: #fffdf0;
        padding: 3pt;
    }
    
    h1, h2, h3, h4 {
        font-family: Kameron, arial;
    }
    
    div#maintoolbar {display: none !important;}
    </style>"""
    return HTML(styles)
css_styling()
In [ ]: