The result (static image) is:
This page shows how to plot data in interactive figure using python, matplotlib.pyplot and mpld3. A interactive legend is added to the figure.
See also:
This page shows how to generate interactive figure using python, matplotlib.pyplot and mpld3. The stock information of the apple.inc is used as the example to plot.
Tips for drawing efficient figures using python matplotlib pyplot. You can brush up them by adding some additional options and settings.
This page is reffering to:
- Interactive legend plugin - mpld3 Example Gellery - URL: https://mpld3.github.io/examples/interactive_legend.html
In [1]:
import platform
print('python: '+platform.python_version())
import numpy as np
print('numpy: '+np.version.full_version)
import matplotlib.pyplot as plt
import matplotlib
print('matplotlib: '+matplotlib.__version__)
%matplotlib inline
In [2]:
import mpld3
print('mpld3: '+mpld3.__version__)
mpld3.enable_notebook()
Set data to show using mpld3 with interactive legend
In [3]:
x = np.linspace(0,6*np.pi,500)
y1 = np.sin(x)
y2 = np.cos(x)
ys = [y1,y2]
labels = ['sin(x)', 'cos(x)']
Generate usual matplotlib.pyplot figure then convert it to the mpld3 conpatible
In [4]:
fig = plt.figure(figsize=(8,5))
# Adjust right padding to show legend label properly
# If not do this, the labels would not appear
# See also: How to show the labels using Interactive legend plugin on mpld3?
# URL: https://stackoverflow.com/questions/44124557/how-to-show-the-labels-using-interactive-legend-plugin-on-mpld3
fig.subplots_adjust(right=0.8)
# Generate matplotlib.pyplot figure as usual
ax = fig.add_subplot(1,1,1)
x = np.linspace(0,6*np.pi,500)
lns = []
for y,l in zip(ys, labels):
ln, = ax.plot(x, y, label=l)
lns.append(ln)
ax.set_xlabel('x')
ax.set_ylabel('sin(x), cos(x)')
# Set interactive legend and connect it to the figure
interactive_legend = mpld3.plugins.InteractiveLegendPlugin(lns, labels)
mpld3.plugins.connect(fig, interactive_legend)
# Show the interactive figure in the notebook
mpld3.display()
Out[4]: