ipympl#

Da sich das Jupyter-Widget-Ökosystem zu schnell entwickelt, haben die Matplotlib-Entwickler beschlossen, die Unterstützung in ein eigenes Modul auszulagern: ipympl.

Installation#

ipympl wird sowohl im Kernel- wie auch im Jupyter-Environment installiert mit

$ pipenv install ipympl
Installing ipympl…
Adding ipympl to Pipfile's [packages]…
✔ Installation Succeeded
…

Anschließend könnt ihr das Jupyter-Backend in Notebooks aktivieren, indem ihr die folgende Matplotlib-Magic verwendet:

[1]:
%matplotlib widget

Beispiele#

Einfache Matplotlib-Interaktion#

[2]:
import matplotlib.pyplot as plt
import numpy as np


plt.figure(1)
plt.plot(np.sin(np.linspace(0, 20, 100)))
plt.show()

3D-Plot: subplot3d_demo.py#

[3]:
from mpl_toolkits.mplot3d import axes3d


fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")

# Grab some test data.
X, Y, Z = axes3d.get_test_data(0.05)

# Plot a basic wireframe.
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)

fig.canvas.layout.max_width = "800px"

plt.show()

Komplexeres Beispiel aus der Matplotlib-Galerie#

[4]:
import matplotlib.pyplot as plt
import numpy as np


np.random.seed(0)

n_bins = 10
x = np.random.randn(1000, 3)

fig, axes = plt.subplots(nrows=2, ncols=2)
ax0, ax1, ax2, ax3 = axes.flatten()

colors = ["red", "tan", "lime"]
ax0.hist(x, n_bins, density=1, histtype="bar", color=colors, label=colors)
ax0.legend(prop={"size": 10})
ax0.set_title("bars with legend")

ax1.hist(x, n_bins, density=1, histtype="bar", stacked=True)
ax1.set_title("stacked bar")

ax2.hist(x, n_bins, histtype="step", stacked=True, fill=False)
ax2.set_title("stack step (unfilled)")

# Make a multiple-histogram of data-sets with different length.
x_multi = [np.random.randn(n) for n in [10000, 5000, 2000]]
ax3.hist(x_multi, n_bins, histtype="bar")
ax3.set_title("different sample sizes")

fig.tight_layout()
plt.show()