**Path:**support / 2014-11-05-Nature.html

**Description:**

Examples for support purposes.

**Views:**1079171

**License:**GPL3

**Image:**ubuntu2004-dev

**Project:**Support and Testing

Raw | Embed | Download |

# imports a 2D/3D graphics plotting library called matplotlib # which can generate scientific figures, and instructs the notebook # to display those figures 'inline' (embedded within this page). import matplotlib.pyplot as plt %matplotlib inline # creates an array of 30 values for x equally spaced from 0 to 5. import numpy as np from numpy import * x = np.linspace(0, 5, 30) y = x **2 plt.plot(x, y, 'r') plt.title('A simple graph');

You can tweak this code, and re-execute it within the notebook. For example, replace "`y = x ** 2`

" with "`y = sin(x)`

". (For a list of valid commands, see the numpy reference manual

IPython notebooks can run interactive plots. Aron Ahmadia (ERDC-CHL) and David Ketcheson (KAUST) created the following example to illustrate the perils of aliasing: when a rapidly-changing periodic signal is sampled too infrequently, creating a faulty impression.

Ketcheson explains:

As an undergraduate, I did some observational astronomy looking at variable stars. These are stars whose brightness oscillates, usually on a fairly regular basis. Many published results claim to measure how quickly the star's brightness oscillates - but actually report the oscillations at some multiple of the real answer, due to insufficient observation and (as a result) aliasing

%matplotlib inline import numpy as np import matplotlib.pyplot as plt from IPython.display import display, HTML from IPython.html import widgets from IPython.html.widgets import interact, interactive def plot_sine(wavenumber=4.0,grid_points=12,plot_sine='On'): "Plot sin(2*pi*p), sampled at m equispaced points." x = np.linspace(0,1,grid_points+2); # grid xf = np.linspace(0,1,1000) # fine grid y = np.sin(wavenumber*np.pi*x) yf = np.sin(wavenumber*np.pi*xf) fig = plt.figure(figsize = (8, 6)); ax = fig.add_subplot(1,1,1); if plot_sine == 'On': ax.plot(xf, yf, 'r-', linewidth=2); ax.plot(x, y, 'o-', lw=2) interact(plot_sine, wavenumber=(1.0,44.0), grid_points=(10, 16, 1), plot_sine=(('On','Off')));

Run previous cell, then set the `gridpoint`

with above slider to `13`

and the `wavenumber`

Since it runs any code, an IPython notebook can be used to import libraries that permit commands for more complicated algorithms (including anything that runs in the computer languages R, Python, Julia, and others). *Scikit-image*, for example, is a collection of algorithms that can be used for image processing.

%matplotlib inline from IPython.html.widgets import interact, interactive, fixed from IPython.display import display import matplotlib.pyplot as plt import skimage from skimage import data, filter, io i = data.coffee() io.Image(i) lims = (0.0,1.0,0.01) @interact def edit_image(image: fixed(i), σ:(0.0,30.0,0.1)=0.1, r:lims=1.0, g:lims=1.0, b:lims=1.0): new_image = filter.gaussian_filter(image, sigma=σ, multichannel=True) new_image[:,:,0] = r*new_image[:,:,0] new_image[:,:,1] = g*new_image[:,:,1] new_image[:,:,2] = b*new_image[:,:,2] new_image = io.Image(new_image) plt.imshow(new_image) return new_image