A Jupyter notebook, on the surface, is a sequence of cells.
By default, each cell in the notebook is akin to a terminal where python commands can be executed.
In [2]:
print "Hello World!"
In [3]:
# Simple python comments indicated by "#"
# Think Twitter #smh
In [4]:
#Block of commands
x=3
y=4
x**y # "**" is python's version of "^"
Out[4]:
In [5]:
#History stored via underscores, "_"=last executed command line, "__"=penultimate executed command line and so on.
_+19
Out[5]:
In [6]:
#To refer even further into the history, simply use "_" followed by the cell number
_4/9
Out[6]:
At any juncture, typing any object followed by the "?" will bring up an information pager on the object.
In [7]:
import pandas as pd
pd.rolling_mean?
In [43]:
#information pager on iPython itself.
?
The "magic" functions are commands specific to iPython and provide a command-line interface. They're prefixed by % or %%.
In [44]:
%quickref
In [1]:
#%history
In [46]:
%time # Outputs time for execution
range(10000); # the ";" supresses output to the screen
In [ ]:
$f(n) = n^5 + 4n^2 + 2 |_{n=17}$
In [9]:
pr #hit tab for command completion
In [ ]:
In [40]:
%matplotlib inline
import matplotlib
import numpy as np
import matplotlib.pyplot as plt # inline magic: Figures are embedded in the document.
In [42]:
#Data
x=np.linspace(0,2*np.pi,50)
y=np.sin(x)
#Plot
plt.plot(x,y,marker='o',color='blue')
#Plot Limits
plt.xlim(0,2*np.pi)
#Plot Title
plt.title("Sine Wave")
#Plot Labels
plt.xlabel('$ x $')
plt.ylabel('$ sin (x) $')
Out[42]:
| marker | description | marker | description | marker | description | marker | description | |||
|---|---|---|---|---|---|---|---|---|---|---|
| "." | point | "+" | plus | "," | pixel | "x" | cross | |||
| "o" | circle | "D" | diamond | "d" | thin_diamond | |||||
| "8" | octagon | "s" | square | "p" | pentagon | "*" | star | |||
| "|" | vertical line | "_" | horizontal line | "h" | hexagon1 | "H" | hexagon2 | |||
| 0 | tickleft | 4 | caretleft | "<" | triangle_left | "3" | tri_left | |||
| 1 | tickright | 5 | caretright | ">" | triangle_right | "4" | tri_right | |||
| 2 | tickup | 6 | caretup | "^" | triangle_up | "2" | tri_up | |||
| 3 | tickdown | 7 | caretdown | "v" | triangle_down | "1" | tri_down | |||
| "None" | nothing | None |
nothing | " " | nothing | "" | nothing |
In [37]:
x=np.linspace(0,2*np.pi,10)
y=np.sin(x)
plt.scatter(x,y)
Out[37]:
In [39]:
x=np.linspace(0,2*np.pi,50)
y=np.sin(x)
plt.plot(x,y,'-.')
plt.xlim(0,2*np.pi)
plt.title("Sine Wave")
plt.xlabel('$ x $')
plt.ylabel('$ sin (x) $')
Out[39]:
In [13]:
from IPython.core.display import Image, display
display(Image(filename='testimage.png'))
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [4]:
x=np.linspace(0,2*np.pi,100)
plt.plot(x,np.sin(x),'-k',label="Sine")
plt.plot(x,np.cos(x),'--b',label="Cosine")
plt.xlim(0,2*np.pi)
plt.ylim(-1.1,1.1)
plt.legend()
plt.grid()
For specific plotting questions, its in you best interests to utilize the Matplotlib gallery You can look through the gallery, find a figure that has pieces of what you want, just click on it to see the code that generated it.
In [ ]:
In [16]:
from IPython.html.widgets import interact
from IPython.html import widgets
First of all, we start off by defining a simple function, $f(x)=x^3$.
In [17]:
def f(x):
print(x**3)
The interact function creates user interface controls for our function argument ($x$), and then calls the function, $f(x)=x^3$, with those arguments when the value of $x$ is changed by the user.
As an illustration, we'll pass our function object with the keyword argument*, $x=2$.
*Not positional! (R users beware)
In [ ]:
interact(f, x=2);
Stuff to note in the widget:
Controlling the widget slider:
IntSliderWidget(min=a, max=b, step=c, value=d)
In [ ]:
#define the function here
In [ ]:
#call the interact function here
In [ ]:
interact(f, x={'one':1,'two':2,'three':3,'four':4,'five':5});
In [ ]:
#call the interact function here to your function, g(x), with the values (1,4,9,16)
In [19]:
import pandas as pd
data = np.array([np.cumsum(np.random.random(1000) - 0.5)]).T
df = pd.DataFrame(data,columns=list('A'))
df.head(5)
Out[19]:
In [20]:
smooth_df=pd.rolling_mean(df, 5)
smooth_df.head(10)
Out[20]:
In [21]:
plt.subplot(1,2,1)
plt.plot(df)
plt.title("Original Data")
plt.subplot(1,2,2)
plt.plot(smooth_df)
plt.title("Filtered Data")
Out[21]:
In [ ]:
#define the function here
In [ ]:
# Call the interact function here
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: