Note that if you're viewing this notebook statically (e.g. on nbviewer) the examples below will not work. They require connection to a running Python kernel
%run talktools
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
In practice, it's a long process of trial and error.
For example, let's say we have some data and want to draw a scatter plot:
np.random.seed(0)
x, y = np.random.normal(size=(2, 100))
s, c = np.random.random(size=(2, 100))
def draw_scatter(size=100, cmap='jet', alpha=1.0):
fig, ax = plt.subplots(figsize=(8, 6))
points = ax.scatter(x, y, s=size*s, c=c, alpha=alpha, cmap=cmap)
fig.colorbar(points, ax=ax)
return fig
draw_scatter(size=100, cmap='jet', alpha=1.0);
IPython 2.0 (released at the beginning of April 2014) adds an interactive widget interface:
from IPython.html.widgets import interact
colormaps = sorted(m for m in plt.cm.datad if not m.endswith("_r"))
interact(draw_scatter, size=[0, 2000], alpha=[0.0, 1.0], cmap=colormaps);
import networkx as nx
def random_lobster(n, m, k, p):
return nx.random_lobster(n, p, p / m)
def powerlaw_cluster(n, m, k, p):
return nx.powerlaw_cluster_graph(n, m, p)
def erdos_renyi(n, m, k, p):
return nx.erdos_renyi_graph(n, p)
def newman_watts_strogatz(n, m, k, p):
return nx.newman_watts_strogatz_graph(n, k, p)
def plot_random_graph(n, m, k, p, generator):
g = generator(n, m, k, p)
nx.draw(g)
plt.show()
interact(plot_random_graph, n=(2,30), m=(1,10), k=(1,10), p=(0.0, 1.0, 0.001),
generator={'lobster': random_lobster,
'power law': powerlaw_cluster,
'Newman-Watts-Strogatz': newman_watts_strogatz,
u'Erdős-Rényi': erdos_renyi,
});
We can also call-back to arbitrarily complicated Python scripts.
One well-known set of differential equations is the Lorenz system:
$$ \begin{aligned} \dot{x} & = \sigma(y-x) \\ \dot{y} & = \rho x - y - xz \\ \dot{z} & = -\beta z + xy \end{aligned} $$It exhibits a range of different behaviors as the parameters ($\sigma$, $\beta$, $\rho$) are varied.
import numpy as np
from scipy import integrate
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.colors import cnames
from matplotlib import animation
def solve_lorenz(N=10, angle=0.0, max_time=4.0, sigma=10.0, beta=8./3, rho=28.0):
def lorenz_deriv((x, y, z), t0, sigma=sigma, beta=beta, rho=rho):
"""Compute the time-derivative of a Lorentz system."""
return [sigma * (y - x), x * (rho - z) - y, x * y - beta * z]
# Choose random starting points, uniformly distributed from -15 to 15
np.random.seed(1)
x0 = -15 + 30 * np.random.random((N, 3))
# Solve for the trajectories
t = np.linspace(0, max_time, int(250*max_time))
x_t = np.asarray([integrate.odeint(lorenz_deriv, x0i, t)
for x0i in x0])
# choose a different color for each trajectory
colors = plt.cm.jet(np.linspace(0, 1, N))
# plot the results
fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1], projection='3d')
ax.axis('off')
# prepare the axes limits
ax.set_xlim((-25, 25))
ax.set_ylim((-35, 35))
ax.set_zlim((5, 55))
for i in range(N):
x, y, z = x_t[i,:,:].T
lines = ax.plot(x, y, z, '-', c=colors[i])
plt.setp(lines, linewidth=2)
ax.view_init(30, angle)
plt.show()
return t, x_t
t, x_t = solve_lorenz()
from IPython.html.widgets import interactive
w = interactive(solve_lorenz, angle=(0.,360.), N=(0,50), sigma=(0.0,50.0), rho=(0.0,50.0))
display(w)
w.result
(array([ 0. , 0.004004 , 0.00800801, 0.01201201, 0.01601602, 0.02002002, 0.02402402, 0.02802803, 0.03203203, 0.03603604, 0.04004004, 0.04404404, 0.04804805, 0.05205205, 0.05605606, 0.06006006, 0.06406406, 0.06806807, 0.07207207, 0.07607608, 0.08008008, 0.08408408, 0.08808809, 0.09209209, 0.0960961 , 0.1001001 , 0.1041041 , 0.10810811, 0.11211211, 0.11611612, 0.12012012, 0.12412412, 0.12812813, 0.13213213, 0.13613614, 0.14014014, 0.14414414, 0.14814815, 0.15215215, 0.15615616, 0.16016016, 0.16416416, 0.16816817, 0.17217217, 0.17617618, 0.18018018, 0.18418418, 0.18818819, 0.19219219, 0.1961962 , 0.2002002 , 0.2042042 , 0.20820821, 0.21221221, 0.21621622, 0.22022022, 0.22422422, 0.22822823, 0.23223223, 0.23623624, 0.24024024, 0.24424424, 0.24824825, 0.25225225, 0.25625626, 0.26026026, 0.26426426, 0.26826827, 0.27227227, 0.27627628, 0.28028028, 0.28428428, 0.28828829, 0.29229229, 0.2962963 , 0.3003003 , 0.3043043 , 0.30830831, 0.31231231, 0.31631632, 0.32032032, 0.32432432, 0.32832833, 0.33233233, 0.33633634, 0.34034034, 0.34434434, 0.34834835, 0.35235235, 0.35635636, 0.36036036, 0.36436436, 0.36836837, 0.37237237, 0.37637638, 0.38038038, 0.38438438, 0.38838839, 0.39239239, 0.3963964 , 0.4004004 , 0.4044044 , 0.40840841, 0.41241241, 0.41641642, 0.42042042, 0.42442442, 0.42842843, 0.43243243, 0.43643644, 0.44044044, 0.44444444, 0.44844845, 0.45245245, 0.45645646, 0.46046046, 0.46446446, 0.46846847, 0.47247247, 0.47647648, 0.48048048, 0.48448448, 0.48848849, 0.49249249, 0.4964965 , 0.5005005 , 0.5045045 , 0.50850851, 0.51251251, 0.51651652, 0.52052052, 0.52452452, 0.52852853, 0.53253253, 0.53653654, 0.54054054, 0.54454454, 0.54854855, 0.55255255, 0.55655656, 0.56056056, 0.56456456, 0.56856857, 0.57257257, 0.57657658, 0.58058058, 0.58458458, 0.58858859, 0.59259259, 0.5965966 , 0.6006006 , 0.6046046 , 0.60860861, 0.61261261, 0.61661662, 0.62062062, 0.62462462, 0.62862863, 0.63263263, 0.63663664, 0.64064064, 0.64464464, 0.64864865, 0.65265265, 0.65665666, 0.66066066, 0.66466466, 0.66866867, 0.67267267, 0.67667668, 0.68068068, 0.68468468, 0.68868869, 0.69269269, 0.6966967 , 0.7007007 , 0.7047047 , 0.70870871, 0.71271271, 0.71671672, 0.72072072, 0.72472472, 0.72872873, 0.73273273, 0.73673674, 0.74074074, 0.74474474, 0.74874875, 0.75275275, 0.75675676, 0.76076076, 0.76476476, 0.76876877, 0.77277277, 0.77677678, 0.78078078, 0.78478478, 0.78878879, 0.79279279, 0.7967968 , 0.8008008 , 0.8048048 , 0.80880881, 0.81281281, 0.81681682, 0.82082082, 0.82482482, 0.82882883, 0.83283283, 0.83683684, 0.84084084, 0.84484484, 0.84884885, 0.85285285, 0.85685686, 0.86086086, 0.86486486, 0.86886887, 0.87287287, 0.87687688, 0.88088088, 0.88488488, 0.88888889, 0.89289289, 0.8968969 , 0.9009009 , 0.9049049 , 0.90890891, 0.91291291, 0.91691692, 0.92092092, 0.92492492, 0.92892893, 0.93293293, 0.93693694, 0.94094094, 0.94494494, 0.94894895, 0.95295295, 0.95695696, 0.96096096, 0.96496496, 0.96896897, 0.97297297, 0.97697698, 0.98098098, 0.98498498, 0.98898899, 0.99299299, 0.996997 , 1.001001 , 1.00500501, 1.00900901, 1.01301301, 1.01701702, 1.02102102, 1.02502503, 1.02902903, 1.03303303, 1.03703704, 1.04104104, 1.04504505, 1.04904905, 1.05305305, 1.05705706, 1.06106106, 1.06506507, 1.06906907, 1.07307307, 1.07707708, 1.08108108, 1.08508509, 1.08908909, 1.09309309, 1.0970971 , 1.1011011 , 1.10510511, 1.10910911, 1.11311311, 1.11711712, 1.12112112, 1.12512513, 1.12912913, 1.13313313, 1.13713714, 1.14114114, 1.14514515, 1.14914915, 1.15315315, 1.15715716, 1.16116116, 1.16516517, 1.16916917, 1.17317317, 1.17717718, 1.18118118, 1.18518519, 1.18918919, 1.19319319, 1.1971972 , 1.2012012 , 1.20520521, 1.20920921, 1.21321321, 1.21721722, 1.22122122, 1.22522523, 1.22922923, 1.23323323, 1.23723724, 1.24124124, 1.24524525, 1.24924925, 1.25325325, 1.25725726, 1.26126126, 1.26526527, 1.26926927, 1.27327327, 1.27727728, 1.28128128, 1.28528529, 1.28928929, 1.29329329, 1.2972973 , 1.3013013 , 1.30530531, 1.30930931, 1.31331331, 1.31731732, 1.32132132, 1.32532533, 1.32932933, 1.33333333, 1.33733734, 1.34134134, 1.34534535, 1.34934935, 1.35335335, 1.35735736, 1.36136136, 1.36536537, 1.36936937, 1.37337337, 1.37737738, 1.38138138, 1.38538539, 1.38938939, 1.39339339, 1.3973974 , 1.4014014 , 1.40540541, 1.40940941, 1.41341341, 1.41741742, 1.42142142, 1.42542543, 1.42942943, 1.43343343, 1.43743744, 1.44144144, 1.44544545, 1.44944945, 1.45345345, 1.45745746, 1.46146146, 1.46546547, 1.46946947, 1.47347347, 1.47747748, 1.48148148, 1.48548549, 1.48948949, 1.49349349, 1.4974975 , 1.5015015 , 1.50550551, 1.50950951, 1.51351351, 1.51751752, 1.52152152, 1.52552553, 1.52952953, 1.53353353, 1.53753754, 1.54154154, 1.54554555, 1.54954955, 1.55355355, 1.55755756, 1.56156156, 1.56556557, 1.56956957, 1.57357357, 1.57757758, 1.58158158, 1.58558559, 1.58958959, 1.59359359, 1.5975976 , 1.6016016 , 1.60560561, 1.60960961, 1.61361361, 1.61761762, 1.62162162, 1.62562563, 1.62962963, 1.63363363, 1.63763764, 1.64164164, 1.64564565, 1.64964965, 1.65365365, 1.65765766, 1.66166166, 1.66566567, 1.66966967, 1.67367367, 1.67767768, 1.68168168, 1.68568569, 1.68968969, 1.69369369, 1.6976977 , 1.7017017 , 1.70570571, 1.70970971, 1.71371371, 1.71771772, 1.72172172, 1.72572573, 1.72972973, 1.73373373, 1.73773774, 1.74174174, 1.74574575, 1.74974975, 1.75375375, 1.75775776, 1.76176176, 1.76576577, 1.76976977, 1.77377377, 1.77777778, 1.78178178, 1.78578579, 1.78978979, 1.79379379, 1.7977978 , 1.8018018 , 1.80580581, 1.80980981, 1.81381381, 1.81781782, 1.82182182, 1.82582583, 1.82982983, 1.83383383, 1.83783784, 1.84184184, 1.84584585, 1.84984985, 1.85385385, 1.85785786, 1.86186186, 1.86586587, 1.86986987, 1.87387387, 1.87787788, 1.88188188, 1.88588589, 1.88988989, 1.89389389, 1.8978979 , 1.9019019 , 1.90590591, 1.90990991, 1.91391391, 1.91791792, 1.92192192, 1.92592593, 1.92992993, 1.93393393, 1.93793794, 1.94194194, 1.94594595, 1.94994995, 1.95395395, 1.95795796, 1.96196196, 1.96596597, 1.96996997, 1.97397397, 1.97797798, 1.98198198, 1.98598599, 1.98998999, 1.99399399, 1.997998 , 2.002002 , 2.00600601, 2.01001001, 2.01401401, 2.01801802, 2.02202202, 2.02602603, 2.03003003, 2.03403403, 2.03803804, 2.04204204, 2.04604605, 2.05005005, 2.05405405, 2.05805806, 2.06206206, 2.06606607, 2.07007007, 2.07407407, 2.07807808, 2.08208208, 2.08608609, 2.09009009, 2.09409409, 2.0980981 , 2.1021021 , 2.10610611, 2.11011011, 2.11411411, 2.11811812, 2.12212212, 2.12612613, 2.13013013, 2.13413413, 2.13813814, 2.14214214, 2.14614615, 2.15015015, 2.15415415, 2.15815816, 2.16216216, 2.16616617, 2.17017017, 2.17417417, 2.17817818, 2.18218218, 2.18618619, 2.19019019, 2.19419419, 2.1981982 , 2.2022022 , 2.20620621, 2.21021021, 2.21421421, 2.21821822, 2.22222222, 2.22622623, 2.23023023, 2.23423423, 2.23823824, 2.24224224, 2.24624625, 2.25025025, 2.25425425, 2.25825826, 2.26226226, 2.26626627, 2.27027027, 2.27427427, 2.27827828, 2.28228228, 2.28628629, 2.29029029, 2.29429429, 2.2982983 , 2.3023023 , 2.30630631, 2.31031031, 2.31431431, 2.31831832, 2.32232232, 2.32632633, 2.33033033, 2.33433433, 2.33833834, 2.34234234, 2.34634635, 2.35035035, 2.35435435, 2.35835836, 2.36236236, 2.36636637, 2.37037037, 2.37437437, 2.37837838, 2.38238238, 2.38638639, 2.39039039, 2.39439439, 2.3983984 , 2.4024024 , 2.40640641, 2.41041041, 2.41441441, 2.41841842, 2.42242242, 2.42642643, 2.43043043, 2.43443443, 2.43843844, 2.44244244, 2.44644645, 2.45045045, 2.45445445, 2.45845846, 2.46246246, 2.46646647, 2.47047047, 2.47447447, 2.47847848, 2.48248248, 2.48648649, 2.49049049, 2.49449449, 2.4984985 , 2.5025025 , 2.50650651, 2.51051051, 2.51451451, 2.51851852, 2.52252252, 2.52652653, 2.53053053, 2.53453453, 2.53853854, 2.54254254, 2.54654655, 2.55055055, 2.55455455, 2.55855856, 2.56256256, 2.56656657, 2.57057057, 2.57457457, 2.57857858, 2.58258258, 2.58658659, 2.59059059, 2.59459459, 2.5985986 , 2.6026026 , 2.60660661, 2.61061061, 2.61461461, 2.61861862, 2.62262262, 2.62662663, 2.63063063, 2.63463463, 2.63863864, 2.64264264, 2.64664665, 2.65065065, 2.65465465, 2.65865866, 2.66266266, 2.66666667, 2.67067067, 2.67467467, 2.67867868, 2.68268268, 2.68668669, 2.69069069, 2.69469469, 2.6986987 , 2.7027027 , 2.70670671, 2.71071071, 2.71471471, 2.71871872, 2.72272272, 2.72672673, 2.73073073, 2.73473473, 2.73873874, 2.74274274, 2.74674675, 2.75075075, 2.75475475, 2.75875876, 2.76276276, 2.76676677, 2.77077077, 2.77477477, 2.77877878, 2.78278278, 2.78678679, 2.79079079, 2.79479479, 2.7987988 , 2.8028028 , 2.80680681, 2.81081081, 2.81481481, 2.81881882, 2.82282282, 2.82682683, 2.83083083, 2.83483483, 2.83883884, 2.84284284, 2.84684685, 2.85085085, 2.85485485, 2.85885886, 2.86286286, 2.86686687, 2.87087087, 2.87487487, 2.87887888, 2.88288288, 2.88688689, 2.89089089, 2.89489489, 2.8988989 , 2.9029029 , 2.90690691, 2.91091091, 2.91491491, 2.91891892, 2.92292292, 2.92692693, 2.93093093, 2.93493493, 2.93893894, 2.94294294, 2.94694695, 2.95095095, 2.95495495, 2.95895896, 2.96296296, 2.96696697, 2.97097097, 2.97497497, 2.97897898, 2.98298298, 2.98698699, 2.99099099, 2.99499499, 2.998999 , 3.003003 , 3.00700701, 3.01101101, 3.01501502, 3.01901902, 3.02302302, 3.02702703, 3.03103103, 3.03503504, 3.03903904, 3.04304304, 3.04704705, 3.05105105, 3.05505506, 3.05905906, 3.06306306, 3.06706707, 3.07107107, 3.07507508, 3.07907908, 3.08308308, 3.08708709, 3.09109109, 3.0950951 , 3.0990991 , 3.1031031 , 3.10710711, 3.11111111, 3.11511512, 3.11911912, 3.12312312, 3.12712713, 3.13113113, 3.13513514, 3.13913914, 3.14314314, 3.14714715, 3.15115115, 3.15515516, 3.15915916, 3.16316316, 3.16716717, 3.17117117, 3.17517518, 3.17917918, 3.18318318, 3.18718719, 3.19119119, 3.1951952 , 3.1991992 , 3.2032032 , 3.20720721, 3.21121121, 3.21521522, 3.21921922, 3.22322322, 3.22722723, 3.23123123, 3.23523524, 3.23923924, 3.24324324, 3.24724725, 3.25125125, 3.25525526, 3.25925926, 3.26326326, 3.26726727, 3.27127127, 3.27527528, 3.27927928, 3.28328328, 3.28728729, 3.29129129, 3.2952953 , 3.2992993 , 3.3033033 , 3.30730731, 3.31131131, 3.31531532, 3.31931932, 3.32332332, 3.32732733, 3.33133133, 3.33533534, 3.33933934, 3.34334334, 3.34734735, 3.35135135, 3.35535536, 3.35935936, 3.36336336, 3.36736737, 3.37137137, 3.37537538, 3.37937938, 3.38338338, 3.38738739, 3.39139139, 3.3953954 , 3.3993994 , 3.4034034 , 3.40740741, 3.41141141, 3.41541542, 3.41941942, 3.42342342, 3.42742743, 3.43143143, 3.43543544, 3.43943944, 3.44344344, 3.44744745, 3.45145145, 3.45545546, 3.45945946, 3.46346346, 3.46746747, 3.47147147, 3.47547548, 3.47947948, 3.48348348, 3.48748749, 3.49149149, 3.4954955 , 3.4994995 , 3.5035035 , 3.50750751, 3.51151151, 3.51551552, 3.51951952, 3.52352352, 3.52752753, 3.53153153, 3.53553554, 3.53953954, 3.54354354, 3.54754755, 3.55155155, 3.55555556, 3.55955956, 3.56356356, 3.56756757, 3.57157157, 3.57557558, 3.57957958, 3.58358358, 3.58758759, 3.59159159, 3.5955956 , 3.5995996 , 3.6036036 , 3.60760761, 3.61161161, 3.61561562, 3.61961962, 3.62362362, 3.62762763, 3.63163163, 3.63563564, 3.63963964, 3.64364364, 3.64764765, 3.65165165, 3.65565566, 3.65965966, 3.66366366, 3.66766767, 3.67167167, 3.67567568, 3.67967968, 3.68368368, 3.68768769, 3.69169169, 3.6956957 , 3.6996997 , 3.7037037 , 3.70770771, 3.71171171, 3.71571572, 3.71971972, 3.72372372, 3.72772773, 3.73173173, 3.73573574, 3.73973974, 3.74374374, 3.74774775, 3.75175175, 3.75575576, 3.75975976, 3.76376376, 3.76776777, 3.77177177, 3.77577578, 3.77977978, 3.78378378, 3.78778779, 3.79179179, 3.7957958 , 3.7997998 , 3.8038038 , 3.80780781, 3.81181181, 3.81581582, 3.81981982, 3.82382382, 3.82782783, 3.83183183, 3.83583584, 3.83983984, 3.84384384, 3.84784785, 3.85185185, 3.85585586, 3.85985986, 3.86386386, 3.86786787, 3.87187187, 3.87587588, 3.87987988, 3.88388388, 3.88788789, 3.89189189, 3.8958959 , 3.8998999 , 3.9039039 , 3.90790791, 3.91191191, 3.91591592, 3.91991992, 3.92392392, 3.92792793, 3.93193193, 3.93593594, 3.93993994, 3.94394394, 3.94794795, 3.95195195, 3.95595596, 3.95995996, 3.96396396, 3.96796797, 3.97197197, 3.97597598, 3.97997998, 3.98398398, 3.98798799, 3.99199199, 3.995996 , 4. ]), array([[[ -2.48933986e+00, 6.60973480e+00, -1.49965688e+01], [ -2.14077645e+00, 6.18646806e+00, -1.48962127e+01], [ -1.82130748e+00, 5.82299967e+00, -1.47853208e+01], ..., [ 6.87667416e+00, 1.07734499e+01, 1.78286316e+01], [ 7.03431517e+00, 1.10115546e+01, 1.79410297e+01], [ 7.19515729e+00, 1.12517722e+01, 1.80659207e+01]], [[ -5.93002282e+00, -1.05973233e+01, -1.22298422e+01], [ -6.13136261e+00, -1.15193465e+01, -1.18344052e+01], [ -6.36138540e+00, -1.24619735e+01, -1.14104771e+01], ..., [ -1.11085203e+01, -1.62261151e+01, 2.32341935e+01], [ -1.13121656e+01, -1.63640151e+01, 2.37150767e+01], [ -1.15128567e+01, -1.64827701e+01, 2.42098140e+01]], [[ -9.41219366e+00, -4.63317819e+00, -3.09697577e+00], [ -9.24717733e+00, -5.76936651e+00, -2.87083263e+00], [ -9.13260659e+00, -6.87477643e+00, -2.60897637e+00], ..., [ 8.94933599e+00, 1.00204877e+01, 2.61339939e+01], [ 8.99188828e+00, 1.00458891e+01, 2.62149083e+01], [ 9.03371359e+00, 1.00685518e+01, 2.62975123e+01]], ..., [[ 1.40478473e+01, -5.59727466e+00, 5.76967847e+00], [ 1.33015844e+01, -4.35137818e+00, 5.43762841e+00], [ 1.26324566e+01, -3.15868945e+00, 5.18607122e+00], ..., [ -5.62183444e+00, -9.08227752e+00, 1.57576353e+01], [ -5.76241216e+00, -9.32397964e+00, 1.57989149e+01], [ -5.90705639e+00, -9.57059699e+00, 1.58506576e+01]], [[ 1.12916746e+01, 1.18381999e+01, -1.24486737e+01], [ 1.13481065e+01, 1.36039497e+01, -1.17430178e+01], [ 1.14710812e+01, 1.53431579e+01, -1.09606825e+01], ..., [ -1.47677316e-02, -1.23193351e-02, 1.01977111e+01], [ -1.46914304e-02, -1.33208729e-02, 1.00894066e+01], [ -1.46573919e-02, -1.43208005e-02, 9.98225246e+00]], [[ -1.38283565e+01, -9.90508741e+00, 1.13442751e+01], [ -1.36915606e+01, -1.07690154e+01, 1.17903119e+01], [ -1.35933180e+01, -1.15964765e+01, 1.22727276e+01], ..., [ 1.61306741e+01, 1.93584734e+01, 3.43088188e+01], [ 1.62473985e+01, 1.88446346e+01, 3.51764615e+01], [ 1.63382575e+01, 1.82747191e+01, 3.60076113e+01]]]))