Matplotlib: Difference between revisions
From charlesreid1
(Replaced content with "#REDIRECT Matplotlib:Main_Page Category:Python") |
No edit summary |
||
| Line 1: | Line 1: | ||
=Using Matplotlib in Web Apps= | |||
I wanted to write a Python web app that would call Matplotlib to visualize some data on the back end, and serve it up to a browser window on the front end. | |||
Initially I saw [webplotlib https://pypi.python.org/pypi/webplotlib/0.1], which looked promising, but wrapped all of matplotlib into two dinky kinds of plots: time series, and bar charts. I needed something that, like webplotlib, could communicate a figure to a browser, but something that, unlike webplotlib, still kept the full functionality of matplotlib. | |||
The fix was easy. The core functionality of webplotlib is passing a figure as a string to the browser; this is about 4 lines. The rest is entirely case-dependent. | |||
Let's walk through how to do this. | |||
==Part 1: Create Your Figure== | |||
Some quick code to make a dummy figure: | |||
<source lang="python"> | |||
import matplotlib.pylab as plt | |||
from numpy.random import * | |||
fig = plt.figure() | |||
ax1 = fig.add_subplot(1,2,1) | |||
ax2 = fig.add_subplot(1,2,2) | |||
x = range(10) | |||
y1 = rand(10,) | |||
y2 = 100*rand(10,) | |||
ax1.plot(x,y1,'b-') | |||
ax2.plot(x,y2,'r-') | |||
</source> | |||
[[Category:Python]] | [[Category:Python]] | ||
Revision as of 23:14, 22 May 2014
Using Matplotlib in Web Apps
I wanted to write a Python web app that would call Matplotlib to visualize some data on the back end, and serve it up to a browser window on the front end.
Initially I saw [webplotlib https://pypi.python.org/pypi/webplotlib/0.1], which looked promising, but wrapped all of matplotlib into two dinky kinds of plots: time series, and bar charts. I needed something that, like webplotlib, could communicate a figure to a browser, but something that, unlike webplotlib, still kept the full functionality of matplotlib.
The fix was easy. The core functionality of webplotlib is passing a figure as a string to the browser; this is about 4 lines. The rest is entirely case-dependent.
Let's walk through how to do this.
Part 1: Create Your Figure
Some quick code to make a dummy figure:
import matplotlib.pylab as plt
from numpy.random import *
fig = plt.figure()
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)
x = range(10)
y1 = rand(10,)
y2 = 100*rand(10,)
ax1.plot(x,y1,'b-')
ax2.plot(x,y2,'r-')