i'm using matplotlib with django. I'm trying to create bar charts.
i followed the cookbook, but i just got a grey rectangular box.
Now I'm using the following code, and have a title and axes.
How can I add a bar graph to the figure? Currently there is no actual data inside the axes.
Here's my charting code:
from matplotlib.backends.backend_agg import FigureCanvasAgg
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
class Chart(object):
## Creates a bar chart of the given data
@staticmethod
def bar(data):
figure = Figure(figsize=(6,6))
ax = figure.add_axes([0.1, 0.1, 0.8, 0.8])
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
fracs = [15, 30, 45, 10]
explode=(0, 0.05, 0, 0)
plt.pie(fracs, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True)
figure.suptitle('Raining Hogs and Dogs', fontsize=14)
canvas = FigureCanvasAgg(figure)
return canvas
In my view I have:
canvas = Chart.bar(results)
# turn the returned canvas into an HTTP response
response=HttpResponse(content_type='image/png')
canvas.print_png(response)
return response