parallel_plot.ipy
44 lines
| 1.2 KiB
| text/plain
|
TextLexer
MinRK
|
r15185 | """Example of how to use matplotlib to plot parallel data. | ||
Brian E Granger
|
r1337 | |||
The idea here is to run matplotlib is the same IPython session | ||||
MinRK
|
r3690 | as an ipython parallel Client. That way matplotlib | ||
Brian E Granger
|
r1337 | can be used to plot parallel data that is gathered using | ||
MinRK
|
r15185 | a DirectView. | ||
Brian E Granger
|
r1337 | |||
To run this example, first start the IPython controller and 4 | ||||
engines:: | ||||
ipcluster -n 4 | ||||
MinRK
|
r15185 | Then start ipython with matplotlib integration mode:: | ||
Brian E Granger
|
r1337 | |||
MinRK
|
r15185 | ipython --matplotlib | ||
Brian E Granger
|
r1337 | |||
MinRK
|
r15185 | Then a simple "%run parallel_plot.ipy" in IPython will run the | ||
Brian E Granger
|
r1337 | example. | ||
""" | ||||
MinRK
|
r15185 | import matplotlib.pyplot as plt | ||
MinRK
|
r3690 | from IPython.parallel import Client | ||
Brian E Granger
|
r1337 | |||
MinRK
|
r3690 | # Get an IPython Client | ||
rc = Client() | ||||
v = rc[:] | ||||
Brian E Granger
|
r1337 | |||
# Create random arrays on the engines | ||||
# This is to simulate arrays that you have calculated in parallel | ||||
# on the engines. | ||||
# Anymore that length 10000 arrays, matplotlib starts to be slow | ||||
MinRK
|
r15185 | %px import numpy as np | ||
%px x = np.random.standard_normal(10000) | ||||
%px y = np.random.standard_normal(10000) | ||||
Brian E Granger
|
r1337 | |||
MinRK
|
r3690 | print v.apply_async(lambda : x[0:10]).get_dict() | ||
print v.apply_async(lambda : y[0:10]).get_dict() | ||||
Brian E Granger
|
r1337 | |||
# Bring back the data | ||||
MinRK
|
r3690 | x_local = v.gather('x', block=True) | ||
y_local = v.gather('y', block=True) | ||||
Brian E Granger
|
r1337 | |||
# Make a scatter plot of the gathered data | ||||
MinRK
|
r15185 | plt.plot(x_local, y_local,'ro') | ||