There are a few tools for interactive plots that one may choose from, such as Bokeh, Plotly, and VisPy.
Bokeh allows you to plot matplotlib objects via JavaScript, which enables the interactive part easily. For instance, if one needs a map plot that is interactive, Bokeh can be used. Bokeh uses JavaScript and enables D3.js style plots and targets the visualization via modern web browsers. Bokeh delivers good performance over a large dataset. You can easily install bokeh
either via conda
or pip
, as shown in the following code:
conda install bokeh OR pip install bokeh
import collections from bokeh.sampledata import us_counties, unemployment from bokeh.plotting import figure, show, output_file from bokeh.models import HoverTool county_coordinate_xs=[ us_counties.data[code]['lons'] for code in us_counties.data if us_counties.data[code]['state'] == 'ca' ] county_coordinate_ys=[ us_counties.data[code]['lats'] for code in us_counties.data if us_counties.data[code]['state'] == 'ca' ] colors = ["#e6f2ff", "#cce5ff", "#99cbff", "#b2d8ff", "#73abe5", "#5985b2"] county_colors = [] for county_id in us_counties.data: if us_counties.data[county_id]['state'] != 'ca': continue try: rate = unemployment.data[county_id] idx = min(int(rate/2), 5) county_colors.append(colors[idx]) except KeyError: county_colors.append("black") output_file("california.html", title="california.py example") TOOLS="pan,wheel_zoom,box_zoom,reset,hover,save" p = figure(title="California Unemployment 2009", width=1000, height=1000, tools=TOOLS) p.patches(county_coordinate_xs, county_coordinate_ys, fill_color=county_colors, fill_alpha=0.7, line_color="white", line_width=0.5) mouse_hover = p.select(dict(type=HoverTool)) mouse_hover.point_policy = "follow_mouse" mouse_hover.tooltips = collections.OrderedDict([ ("index", "$index"), ("(x,y)", "($x, $y)"), ("fill color", "$color[hex, swatch]:fill_color"), ]) show(p)
In order to view the results, you may have to use a browser to open California.html
:
Plotly is another option that allows interactive plots, but requires one to be online and have a Plotly account. The plots using Plotly look very nice and is interactive. The following code shows how one can create interactive plots using plotly
:
from pylab import * import plotly #py = plotly.plotly('me', 'mykey') def to_plotly(ax=None): if ax is None: ax = gca() lines = [] for line in ax.get_lines(): lines.append({'x': line.get_xdata(), 'y': line.get_ydata(), 'name': line.get_label(), }) layout = {'title':ax.get_title(), 'xaxis':{'title':ax.get_xlabel()}, 'yaxis':{'title':ax.get_ylabel()} } filename = ax.get_title() if ax.get_title() != '' else 'Untitled' print filename close('all') #return lines, layout return py.iplot(lines,layout=layout, filename = filename) plot(rand(100), label = 'trace1') plot(rand(100)+1, label = 'trace2') title('Title') xlabel('X label') ylabel('Y label ') response = to_plotly() response
VisPy is another high performance interactive tool built using Python and OpenGL; therefore, it delivers the power of modern GPU's. It is fairly new, and as it matures, it leaves users with another good visualization library to choose from. The following example shows that using vispy
one can create an image that can be zoomed interactively:
import sys from vispy import scene from vispy import app import numpy as np canvas = scene.SceneCanvas(keys='interactive') canvas.size = 800, 800 canvas.show() # Set up a viewbox to display the image with interactive pan/zoom view = canvas.central_widget.add_view() # Create the image img_data = np.random.normal(size=(100, 100, 3), loc=128, scale=40).astype(np.ubyte) image = scene.visuals.Image(img_data, parent=view.scene) # Set 2D camera (the camera will scale to the contents in the scene) view.camera = scene.PanZoomCamera(aspect=1) if __name__ == '__main__' and sys.flags.interactive == 0: app.run()
The preceding image shows the plot that appears the first time, but as we move the mouse and zoom in on it, it appears as follows: