Example of how Python draws a pie chart using the Matplotlib library _python

Source: Internet
Author: User
Tags explode

Introduced

Matplotlib is Python's most famous drawing library, which provides a complete set of command APIs similar to MATLAB and is ideal for interactive charting. It can also be conveniently embedded in GUI applications as a drawing control.

Its documentation is fairly complete, and there are hundreds of thumbnails on the Gallery page, which are available after they are opened. So if you need to draw some kind of diagram, just browse/copy/paste it in this page, and basically you can do it.

Matplotlib installation method can be clicked here

This article gives you a major introduction to Python use Matplotlib to draw a pie chart method, not to say more, the following look at the code.

Sample code

Import Matplotlib.pyplot as Plt

# The slices would be ordered and plotted counter-clockwise.
Labels = ' frogs ', ' hogs ', ' Dogs ', ' Logs '
sizes = [M, m,]
colors = [' yellowgreen ', ' Gold ', ' lightskyblue ', ' Lightcoral ']
explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. ' hogs ')

plt.pie (sizes, Explode=explod  E, Labels=labels, colors=colors,
  autopct= '%1.1f%% ', Shadow=true, startangle=90)

# Set aspect ratio to is equal so That the pie is drawn as a circle.
Plt.axis (' equal ')


plt.savefig (' d:\\pie.png ')
plt.show ()

Results

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.