Matplotlib interactive demo#

Plotting in JupyterLite#

The packages used in this notebook are NumPy and matplotlib, which are available in the emscripten-forge channel. They are pre-installed into the deployment using the ennvironment.yml file in the docs source directory for the Xeus kernel to load. Once the kernel is started, the packages are available for use right away.

Any extra packages must be pre-installed into the environment before the JupyterLite deployment is built as a part of the Sphinx build process.

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('Lines')
plt.grid(True)
plt.show()
../_images/d07c00cc4acf5db920fa1014dfd3ff994a95e806fc138ded7403f218854b8126.png
# Update the line plot with green color, markers and fill
plt.figure(figsize=(10, 6))
plt.plot(x, y, color='green', marker='o')
plt.fill_between(x, y, min(y), color='green', alpha=0.3)
plt.xlabel('Index')
plt.ylabel('Value')
plt.title('Lines')
plt.grid(True)
plt.show()
../_images/32698157501681785bfcfa3aa83b2e0ad3789ac38274514a41e4c359f735844e.png
n = 100

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

plt.figure(figsize=(10, 6))
plt.bar(x, y)
plt.xlabel('Index')
plt.ylabel('Value')
plt.title('Bars')
plt.grid(True, axis='y')
plt.show()
../_images/d748db0c3fa68652baf85c99a5ba566807a60692271934a728745c1740d7af68.png
# Update the bars with new random data
new_y = np.cumsum(np.random.randn(n))
plt.figure(figsize=(10, 6))
plt.bar(x, new_y)
plt.xlabel('Index')
plt.ylabel('Value')
plt.title('Bars')
plt.grid(True, axis='y')
plt.show()
../_images/d2dc21341fd445f7deeeb7c33ce95dcac92ec4ee4510d656cb851a335a7e7523.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('Lines')
plt.legend()
plt.grid(True)
plt.show()
../_images/b34da6c9fc90c41f8d66958ecc48350b3b521380e7a4f2fb4309f59a5c8a3de2.png