Matplotlib interactive demo#

Plotting in JupyterLite#

The jupyterlite-pyodide-kernel can install packages in a cell based on import <...> statements, if the importable name of the package is the same as the package name in the Pyodide package index or on PyPI (and the package exists in either of them).

Thus, the below example will work in this deployment, but you may also install any other package(s) on your own using a import piplite; await piplite.install(["mypackage"]) statement, or, better, a %pip install mypackage cell magic.

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(0)

n = 100

x = list(range(n))
y = np.cumsum(np.random.randn(n)) + 100

plt.figure(figsize=(10, 6))
plt.plot(x, y)
plt.xlabel('Index')
plt.ylabel('Value')
plt.title('Line Plot')
plt.grid(True)
plt.show()
../_images/88ce9e48a36ddf9d6368ebf2ffa729ec44a0fa943a17e3f88f282b48a1319f79.png
plt.figure(figsize=(10, 6))
plt.plot(x, y, 'g-', marker='o', fillstyle='full')
plt.fill_between(x, y, min(y), color='green', alpha=0.3)
plt.xlabel('Index')
plt.ylabel('Value')
plt.title('Line Plot with Fill and Markers')
plt.grid(True)
plt.show()
../_images/8b370622be43bdaa277151fa510f475d4e7028f6cb6168c71df85765902e938f.png
n = 100

x = list(range(n))
y = np.cumsum(np.random.randn(n))

plt.figure(figsize=(10, 6))
plt.bar(x, y, width=0.8)
plt.xlabel('Index')
plt.ylabel('Value')
plt.title('Bar Chart')
plt.grid(True, axis='y')
plt.show()
../_images/74ff773eef2311fc38953430ac33a90e1dbbe1bcd9b2fe3b2559829472484e6a.png
# Update the bar chart with new data
y_new = np.cumsum(np.random.randn(n))

plt.figure(figsize=(10, 6))
plt.bar(x, y_new, width=0.8, color='orange')
plt.xlabel('Index')
plt.ylabel('Value')
plt.title('Updated Bar Chart')
plt.grid(True, axis='y')
plt.show()
../_images/31f0d104b2666b7b5d9e783db73173b7452f796709338fa66d343731b7699b25.png

Multiple line plots#

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(0)
y1 = np.cumsum(np.random.randn(150)) + 100.
y2 = np.cumsum(np.random.randn(150)) + 100.
y3 = np.cumsum(np.random.randn(150)) + 100.
y4 = np.cumsum(np.random.randn(150)) + 100.

x = np.arange(len(y1))

plt.figure(figsize=(12, 7))
plt.plot(x, y1, label='Series 1')
plt.plot(x, y2, label='Series 2')
plt.plot(x, y3, label='Series 3')
plt.plot(x, y4, label='Series 4')
plt.xlabel('Index')
plt.ylabel('Value')
plt.title('Multiple Line Series')
plt.legend()
plt.grid(True)
plt.show()
../_images/2ca3e00f0a4ba506624aa26c8a70883afd484a4a2dde42daec34d96f1e72a48f.png