Results display using matplotlib
The
matplotlib
library provides another alternative for generating charts from Python. It can be found on the Python Package Index site, http://pypi.python.org/pypi/matplotlib. The version of matplotlib
used in this chapter is 0.98.3.
With matplotlib
, our application cannot simply construct a URL and push the task of generating and serving the image data off to another host. Instead, we need to write a view that will generate and serve the image data. After some investigation of the matplotlib
APIs, an initial implementation (in survey/views.py
) might be:
from django.http import HttpResponse from survey.models import Question from matplotlib.figure import Figure from matplotlib.backends.backend_agg import FigureCanvasAgg as \FigureCanvas @log_view def answer_piechart(request, pk): q = get_object_or_404(Question, pk=pk) answer_set = q.answer_set.all() x = [a.votes for a in answer_set] labels = [a.answer for a in answer_set] fig...