Joshua Sierles / Apr 30 2018
Using Matplotlib with Plotly and SVG
Using Matplotlib with Plotly and SVG
You can plot here in two ways with Matplotlib: via interactive Plotly.js plots, or SVG images.
These examples are drawn from the Matplotlib documentation.
First, let's setup our environment.
import matplotlib.pyplot as plt import numpy as np import plotly.tools as tls # redefine some plotting methods for Nextjournal support plt.show = plt.gcf
1. Simple Line Plot
1.
Simple Line Plot
# Data for plotting t = np.arange(0.0, 2.0, 0.01) s = 1 + np.sin(2 * np.pi * t) # Note that using plt.subplots below is equivalent to using # fig = plt.figure and then ax = fig.add_subplot(111) fig, ax = plt.subplots() ax.plot(t, s) ax.set(xlabel='time (s)', ylabel='voltage (mV)', title='About as simple as it gets, folks') ax.grid() plt.show()
Let's plot with Plot.ly for interactivity.
tls.mpl_to_plotly(fig)
2. Image tutorial
2.
Image tutorial
mkdir -p /images cpstinkbug.png/images/stinkbug.png ls -lsa /images
import matplotlib.image as mpimg img = mpimg.imread("/images/stinkbug.png") print(img)
plt.imshow(img) plt.show()
lum_img = img[:, :, 0] # This is array slicing. You can read more in the `Numpy tutorial # <https://docs.scipy.org/doc/numpy-dev/user/quickstart.html>`_. plt.imshow(lum_img) plt.show()
plt.imshow(lum_img, cmap="hot") plt.show()
imgplot = plt.imshow(lum_img) imgplot.set_cmap('nipy_spectral') plt.gcf
imgplot = plt.imshow(lum_img) plt.colorbar() plt.show()
plt.clf() plt.hist(lum_img.ravel(), bins=256, range=(0.0, 1.0), fc='k', ec='k') plt.show()
fig = plt.figure() a = fig.add_subplot(1, 2, 1) imgplot = plt.imshow(lum_img) a.set_title('Before') plt.colorbar(ticks=[0.1, 0.3, 0.5, 0.7], orientation='horizontal') a = fig.add_subplot(1, 2, 2) imgplot = plt.imshow(lum_img) imgplot.set_clim(0.0, 0.7) a.set_title('After') plt.colorbar(ticks=[0.1, 0.3, 0.5, 0.7], orientation='horizontal') plt.gcf()