Skip to content

Instantly share code, notes, and snippets.

@mtigas
Created November 9, 2009 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtigas/229801 to your computer and use it in GitHub Desktop.
Save mtigas/229801 to your computer and use it in GitHub Desktop.
# coding=utf-8
from matplotlib.figure import Figure
def make_example_pie():
"""
Follows the simple pie chart demo[1] from matplotlib's gallery, but
rewritten in a cleaner fashion that can be used for PNG rendering.
[1] http://matplotlib.sourceforge.net/examples/pylab_examples/pie_demo.html
"""
# with DPI@100px/in, this renders a 600x600 image
fig = Figure(figsize=(6,6), dpi=100, frameon=False)
ax = fig.add_subplot(111)
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
fracs = [15,30,45, 10]
explode=(0, 0.05, 0, 0)
ax.pie(fracs, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True)
ax.set_title('Raining Hogs and Dogs', bbox={'facecolor':'0.8', 'pad':5})
return fig
if __name__ == "__main__":
# Call the function above, which renders the figure.
fig = make_example_pie()
# Write it to a file.
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
canvas = FigureCanvas(fig)
output_file = open("test.png","wb")
canvas.print_png(output_file)
output_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment