Skip to content

Instantly share code, notes, and snippets.



Created Nov 9, 2009
What would you like to do?
# 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.
# 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")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment