# IGNORE THE LINE BELOW, IT IS ONLY FOR CREATING THIS SCRIPT!
%matplotlib inline
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:
x = [i * dx for i in range(30)]
y = [math.sin(xi) for xi in x]
import pylab
pylab.plot(x, y)
pylab.show()
Blue is the standard color. To change this use:
pylab.plot(x, y, "green")
pylab.show()
To plot dots, eg red dots:
pylab.plot(x, y, "r.")
pylab.show()
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()
pylab.plot(x, y)
pylab.grid(True)
pylab.show()
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()
# 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()
pylab.plot(x, y)
pylab.savefig("sin.png")
!ls -l sin.png
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)]
pylab.scatter(x, y, s=areas, c=colors, alpha=0.5)
pylab.show()
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:
# 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()