{"id":26343,"date":"2021-12-17T08:43:09","date_gmt":"2021-12-17T03:13:09","guid":{"rendered":"https:\/\/python-programs.com\/?p=26343"},"modified":"2021-12-17T08:43:09","modified_gmt":"2021-12-17T03:13:09","slug":"in-python-how-can-you-plot-and-customize-a-pie-chart","status":"publish","type":"post","link":"https:\/\/python-programs.com\/in-python-how-can-you-plot-and-customize-a-pie-chart\/","title":{"rendered":"In Python, How can you Plot and Customize a Pie Chart?"},"content":{"rendered":"
A pie chart is a circular statistical picture graph\u00a0divided into slices to show numerical proportions. The arc length of each slice in a pie chart is proportionate to the quantity it represents.<\/p>\n
The area of the wedge is defined by the length of the wedge’s arc. The area of a wedge represents the percentage of that part of the data in relation to the entire data set. Pie charts are widely used in corporate presentations to provide a brief review of topics such as sales, operations, survey data, and resources.<\/p>\n
Pie charts are a common technique to display poll results.<\/p>\n
# Give some random list(fruits) as static input and store it in a variable.\r\nfruit_lst = [\"Apple\", \"mango\", \"banana\", \"orange\", \"grapes\"]\r\n# Give the percentage list as static input and store it in another variable.\r\ngvn_percentges = [20, 15, 40, 10, 15]\r\n<\/pre>\nHow to Create a Pie Chart?<\/h4>\n
Matplotlib will be used to create a Pie-Chart.<\/p>\n
In its pyplot module, the Matplotlib API offers a pie() method that generates a pie chart based on the data in an array.<\/p>\n
Syntax:<\/strong><\/p>\n
matplotlib.pyplot.pie(data, explode=None, labels=None, colors=None, \r\nautopct=None, shadow=False)<\/pre>\nParameters<\/strong><\/p>\n
data:<\/strong> The array of data values to be plotted is represented by data, and the fractional area of each slice is represented by data\/sum (data). If sum(data)<1 returns the fractional area directly, the resulting pie will have an empty wedge of size 1-sum (data).<\/p>\n
labels:<\/strong> It is a list of string sequences that sets the label of each wedge.<\/p>\n
colour:<\/strong> The colour attribute is used to give the colour of the wedge.<\/p>\n
autopct: <\/strong>This is a string that is used to name each wedge with its numerical value.<\/p>\n
shadow:<\/strong> The shadow is utilised to generate the shadow of the wedge.<\/p>\n
To create a basic Pie-chart, we need labels and the values that go with those labels.<\/p>\n
Example<\/strong><\/p>\n
Approach:<\/strong><\/p>\n
\n
- Import matplotlib.pyplot module as plt using the import keyword.<\/li>\n
- Give some random list(fruits) as static input and store it in a variable.<\/li>\n
- Give the percentage(size) list as static input and store it in another variable.<\/li>\n
- Get the subplots using the subplots function and assign it to two different variables.<\/li>\n
- Pass the given percentages(size)list, labels to the pie() function and apply it to the above axis (where labels is the fruit list).<\/li>\n
- Show the piechart using the show() function.<\/li>\n
- The Exit of the Program.<\/li>\n<\/ul>\n
Below is the implementation:<\/strong><\/p>\n
# Import matplotlib.pyplot module as plt using the import keyword.\r\nimport matplotlib.pyplot as plt\r\n# Give some random list(fruits) as static input and store it in a variable.\r\nfruit_lst = [\"Apple\", \"mango\", \"banana\", \"orange\", \"grapes\"]\r\n# Give the percentage list as static input and store it in another variable.\r\ngvn_percentges = [20, 15, 40, 10, 15]\r\n# Get the subplots using the subplots function and assign it to two different\r\n# variables\r\nfigre, axis = plt.subplots()\r\n# Pass the given percentages(size)list, labels to the pie() function\r\n# and apply it to the above axis where labels is the fruit list.\r\naxis.pie(gvn_percentges, labels=fruit_lst)\r\naxis.axis('equal')\r\n# Show the piechart using the show() function.\r\nplt.show()<\/pre>\nOutput:<\/strong><\/p>\n
<\/p>\n
<\/p>\n
Customize a Pie Chart<\/strong><\/p>\n
A pie chart can be altered in a variety of ways. The startangle attribute spins the plot by the provided number of degrees in a counterclockwise direction on the pie chart’s x-axis. The shadow attribute accepts a boolean value; if true, the shadow appears below the rim of the pie. Wedges of the pie can be customized using wedgeprop, which accepts a Python dictionary as an argument and returns a list of name-value pairs specifying wedge attributes such as linewidth, edgecolor, and so on. A frame is drawn around the pie chart by setting frame<\/strong>=True axis. The percentages displayed on the wedges are controlled by autopct<\/strong>.<\/p>\n
How to Make a slice pop-out?<\/h5>\n
Using the explode option, you can make one or more pie-chart slices pop out.<\/p>\n
To do this give an array with the explosion values. The explosion array provides the fraction of the radius by which to offset each slice.<\/p>\n
Example<\/strong><\/p>\n
# Import matplotlib.pyplot module as plt using the import keyword.\r\nimport matplotlib.pyplot as plt\r\n# Give some random list(fruits) as static input and store it in a variable.\r\nfruit_lst = [\"Apple\", \"mango\", \"banana\", \"orange\", \"grapes\"]\r\n# Give the percentage list as static input and store it in another variable.\r\ngvn_percentges = [20, 15, 40, 10, 15]\r\n# Give the explode values as static input and store it in another variable.\r\nexplode_vals = (0.1, 0, 0.2, 0, 0)\r\n# Get the subplots using the subplots function and assign it to two different\r\n# variables\r\nfigre, axis = plt.subplots()\r\n# Pass the given percentages(size)list, explode values, and labels to the pie() function\r\n# and apply it to the above axis (where labels is the fruit list).\r\naxis.pie(gvn_percentges, explode=explode_vals, labels=fruit_lst)\r\n# Show the piechart using the show() function.\r\nplt.show()<\/pre>\nOutput:<\/strong><\/p>\n
<\/p>\n
<\/h5>\n
Rotating the Pie-chart<\/strong><\/p>\n
By defining a strartangle<\/strong>, you can rotate the pie-chart.<\/p>\n
It rotates the start of the pie chart by the specified number of degrees counterclockwise from the x-axis.<\/p>\n
Example<\/strong><\/p>\n
# Import matplotlib.pyplot module as plt using the import keyword.\r\nimport matplotlib.pyplot as plt\r\n# Give some random list(fruits) as static input and store it in a variable.\r\nfruit_lst = [\"Apple\", \"mango\", \"banana\", \"orange\", \"grapes\"]\r\n# Give the percentage list as static input and store it in another variable.\r\ngvn_percentges = [20, 15, 40, 10, 15]\r\n# Give the explode values as static input and store it in another variable.\r\nexplode_vals = (0.1, 0, 0.2, 0, 0)\r\n# Get the subplots using the subplots function and assign it to two different\r\n# variables\r\nfigre, axis = plt.subplots()\r\n# Rotate the pie chart by giving the startangle.\r\naxis.pie(gvn_percentges, explode=explode_vals, labels=fruit_lst,\r\n shadow=True, startangle=90)\r\n# Show the piechart using the show() function.\r\nplt.show()<\/pre>\nOutput:<\/strong><\/p>\n
<\/p>\n
<\/p>\n
To Display the percentages<\/strong><\/p>\n
\nDisplay the percentages using the autopct.<\/div>\n\n# Import matplotlib.pyplot module as plt using the import keyword.\r\nimport matplotlib.pyplot as plt\r\n# Give some random list(fruits) as static input and store it in a variable.\r\nfruit_lst = [\"Apple\", \"mango\", \"banana\", \"orange\", \"grapes\"]\r\n# Give the percentage list as static input and store it in another variable.\r\ngvn_percentges = [20, 15, 40, 10, 15]\r\n# Give the explode values as static input and store it in another variable.\r\nexplode_vals = (0.1, 0, 0.2, 0, 0)\r\n# Get the subplots using the subplots function and assign it to two different\r\n# variables\r\nfigre, axis = plt.subplots()\r\n# Display the percentages using the autopct\r\naxis.pie(gvn_percentges, explode=explode_vals, labels=fruit_lst,\r\n autopct='%1.1f%%',shadow=True, startangle=90)\r\n# Show the piechart using the show() function.\r\nplt.show()<\/pre>\n<\/div>\n<\/div>\nOutput:<\/strong><\/p>\n
<\/p>\n
<\/p>\n
Color customization or Changing<\/strong><\/p>\n
Matplotlib allows you to be creative and make your pie-chart as colorful as possible.<\/p>\n
To modify the colors of your pie chart, give the colors list and apply those colors to the pie chart by giving it as an argument to the pie function().<\/p>\n
# Import matplotlib.pyplot module as plt using the import keyword.\r\nimport matplotlib.pyplot as plt\r\n# Give some random list(fruits) as static input and store it in a variable.\r\nfruit_lst = [\"Apple\", \"mango\", \"banana\", \"orange\", \"grapes\"]\r\n# Give the percentage list as static input and store it in another variable.\r\ngvn_percentges = [20, 15, 40, 10, 15]\r\n# Give the colors as static input and store it in another variable.\r\ngvn_colors = (\"pink\", \"yellow\", \"skyblue\", \"grey\", \"green\")\r\n# Give the explode values as static input and store it in another variable. \r\nexplode_vals = (0.1, 0, 0.2, 0, 0)\r\nfigre, axis = plt.subplots()\r\n# Applying the above given colors to the pie chart.\r\naxis.pie(gvn_percentges, colors = gvn_colors, explode=explode_vals, labels=fruit_lst,\r\n autopct='%1.1f%%',shadow=True, startangle=90)\r\n# Show the piechart using the show() function.\r\nplt.show()\r\n\r\n<\/pre>\nOutput:<\/strong><\/p>\n
<\/p>\n
<\/p>\n
To Display the Color Codes<\/strong><\/p>\n
Along with your pie-chart, you can display a box containing the pie-color chart’s scheme. This is very beneficial if your pie chart has a lot of pieces.<\/p>\n
\nDisplay the color codes using the legend() function.<\/div>\n<\/div>\nExample<\/strong><\/p>\n
# Import matplotlib.pyplot module as plt using the import keyword.\r\nimport matplotlib.pyplot as plt\r\n# Give some random list(fruits) as static input and store it in a variable.\r\nfruit_lst = [\"Apple\", \"mango\", \"banana\", \"orange\", \"grapes\"]\r\n# Give the percentage list as static input and store it in another variable.\r\ngvn_percentges = [20, 15, 40, 10, 15]\r\n# Give the colors as static input and store it in another variable.\r\ngvn_colors = (\"pink\", \"yellow\", \"skyblue\", \"grey\", \"green\")\r\n# Give the explode values as static input and store it in another variable. \r\nexplode_vals = (0.1, 0, 0.2, 0, 0)\r\nfigre, axis = plt.subplots()\r\n# Customized pie chart\r\naxis.pie(gvn_percentges, colors = gvn_colors, explode=explode_vals, labels=fruit_lst,\r\n autopct='%1.1f%%',shadow=True, startangle=90)\r\npatches, texts, auto = axis.pie(gvn_percentges, colors = gvn_colors, explode=explode_vals, labels=fruit_lst,\r\n autopct='%1.1f%%',shadow=True, startangle=90)\r\n# Display the color codes using the legend() function\r\nplt.legend(patches, labels, loc=\"best\")\r\n# Show the piechart using the show() function.\r\nplt.show()\r\n\r\n<\/pre>\nOutput:<\/strong><\/p>\n
<\/p>\n
<\/p>\n","protected":false},"excerpt":{"rendered":"
A pie chart is a circular statistical picture graph\u00a0divided into slices to show numerical proportions. The arc length of each slice in a pie chart is proportionate to the quantity it represents. The area of the wedge is defined by the length of the wedge’s arc. The area of a wedge represents the percentage of …<\/p>\n