Skip to main content

Command Palette

Search for a command to run...

MatplotLib - pie graphs

Updated
•2 min read•View as Markdown
V

Greetings! I'm Vishnu Vinay, a Computer Science and Engineering graduate holding a B. Tech degree. Currently immersed in the captivating world of Artificial Intelligence, I am on a quest for knowledge while pursuing a graduate certificate in Artificial Intelligence with Machine Learning. My passion lies in sharing insights and discoveries in the fields of AI, Machine Learning, Artificial General Intelligence, and Robotics through engaging blog posts. Proficient in Python, ML libraries, and algorithms, I find joy in developing and deploying ML models, with a focus on leveraging AWS Sagemaker. Join me on this exciting journey of unraveling the mysteries of AI through the lens of coding, exploration, and the ever-evolving landscape of machine learning. Let's embark on this knowledge-sharing adventure together!

1) plotting pie graph

sizes = [3, 4, 6, 2] # an array of a number of splits

plt.pie(sizes)

plt.axis("equal") # makes it a perfect circle (optional)

plt.show()

2) changing the colors

sizes = [3, 4, 6, 2] # an array of a number of splits

colors = ["blue", "red", "black", "yellow"] # this array must be equal to the sizes array

plt.pie(sizes, colors = colors)

plt.show()

3) adding labels

sizes = [3, 4, 6, 2] # an array of a number of splits

labels = ["A", "B", "C", "D"] # this array must be equal to the sizes array

plt.pie(sizes, labels = labels)

plt.show()

4) adding descriptions to the graphs

sizes = [3, 4, 6, 2] # an array of a number of splits

plt.pie(autopct = "%.2f") # shows the percentage of each part inside the split part

plt.show()

In the case of autopct, we can either pass a function that calculates the required value for us and then displays it or choose a percentage value by giving "%.nf" (n - denotes how many decimal values to show after the point eg:- 2f = 2 decimal values (1.86, 2,89 etc)).

5) highlight certain part

sizes = [3, 4, 6, 2] # an array of a number of splits

explode = [0.1, 0, 0, 0] # this array must be equal to the sizes array. Secondly, the values in the array represent which part needs to be more highlighted. In most cases, values less than 0.5 will be enough.

plt.pie(sizes, explode = explode)

plt.axis("equal") # makes it a perfect circle (optional)

plt.show()

More from this blog

Untitled Publication

31 posts