Matplotlib:Django¶
日期 | 2011-07-12(最后修改),2007-01-02(创建) |
---|
在 [ ]
# file charts.py
def simple(request):
import random
import django
import datetime
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.dates import DateFormatter
fig=Figure()
ax=fig.add_subplot(111)
x=[]
y=[]
now=datetime.datetime.now()
delta=datetime.timedelta(days=1)
for i in range(10):
x.append(now)
now+=delta
y.append(random.randint(0, 1000))
ax.plot_date(x, y, '-')
ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))
fig.autofmt_xdate()
canvas=FigureCanvas(fig)
response=django.http.HttpResponse(content_type='image/png')
canvas.print_png(response)
return response
由于某些版本的 Internet Explorer 忽略了 content_type。URL 应该以“.png”结尾。您可以在 urls.py 中创建这样的条目
在 [ ]
...
(r'^charts/simple.png$', 'myapp.views.charts.simple'),
...
部分作者:Unknown[90],Unknown[91],Unknown[92],Unknown[93],RalphMoore