Skip to content

Commit

Permalink
Explain parallel mode in README
Browse files Browse the repository at this point in the history
  • Loading branch information
ali-ramadhan committed May 11, 2024
1 parent 0e1615a commit 9425cc9
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,33 @@ with Loom("rotating_circular_sine_wave.mp4", fps=10) as loom:
```

https://github.com/ali-ramadhan/matplotloom/assets/20099589/77f2f0a2-6be1-46f6-b4ba-32a44b11441b

## Parallel mode

By passing `parallel=True` when creating a `Loom`, you can save frames using `loom.save_frame(fig, frame_number)` which allows you to plot and save all your frames in parallel. One easy way to leverage this is by using joblib to parallelize the for loop. For example, here's how you can parallelize the simple sine wave example:

```python
import numpy as np
import matplotlib.pyplot as plt
from matplotloom import Loom
from joblib import Parallel, delayed

def plot_frame(phase, frame_number, loom):
fig, ax = plt.subplots()

x = np.linspace(0, 2*np.pi, 200)
y = np.sin(x + phase)

ax.plot(x, y)
ax.set_xlim(0, 2*np.pi)

loom.save_frame(fig, frame_number)

with Loom("parallel_sine_wave.gif", fps=30, parallel=True) as loom:
phases = np.linspace(0, 2*np.pi, 100)

Parallel(n_jobs=-1)(
delayed(plot_frame)(phase, i, loom)
for i, phase in enumerate(phases)
)
```

0 comments on commit 9425cc9

Please sign in to comment.