Matplotlib Pie Chart: Exercise-4 with Solution
Write a Python programming to create a pie chart of gold medal achievements of five most successful countries in 2016 Summer Olympics. Read the data from a csv file.
Sample data:
medal.csv
country,gold_medal
United States,46
Great Britain,27
China,26
Russia,19
Germany,17
Sample Solution:
Python Code:
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv['medal.csv']
country_data = df["country"]
medal_data = df["gold_medal"]
colors = ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#8c564b"]
explode = [0.1, 0, 0, 0, 0]
plt.pie[medal_data, labels=country_data, explode=explode, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=140]
plt.title["Gold medal achievements of five most successful\n"+"countries in 2016 Summer Olympics"]
plt.show[]
Sample Output:
Python Code Editor:
Have another way to solve this solution? Contribute your code [and comments] through Disqus.
Previous: Write a Python programming to create a pie chart with a title of the popularity of programming Languages. Make multiple wedges of the pie.
Next: Matplotlib
Scatter Plot Exercises
I have this CSV data file, I'm trying to make a pie chart using this data
I'm a beginner in python and don't understand how to create a pie chart using the three columns, please help!
working solution code would be more helpful!
My code:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv ['chart_work.csv']
product_data = df["Product Name;"]
bug_data = df["Number Of Bugs"]
colors = ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#8c564b"]
plt.pie[bug_data , labels=product_data , colors=colors,
autopct='%1.1f%%', shadow=True, startangle=140]
plt.show[]
the pie chart which is outputed by this code is distorted, any help?
Chart I'm getting:
Collect Country column data from dataframe object say df and store it in variable say “country_data”,Collect Cases column data from dataframe object say df and store it in variable say “cases_data”,Create pie chart using plt.pie method and pass country_data and cases_data as an argument to it,Write a program to create data frame for examination result and display row labels, column labels data types of each column and the dimensions.import matplotlib.pyplot as plt
import pandas as pd
# read data from CSV file
df = pd.read_csv["corona.csv"]
print[df]
country_data = df["Country"]
cases_data = df["Cases"]
plt.pie[cases_data, labels = country_data, autopct = '%1.1f%%']
plt.title["top 5 Countries affected by COvid-19 with highest number of Cases"]
plt.legend[]
plt.show[]
1._
import matplotlib.pyplot as plt import pandas as pd # read data from CSV file df = pd.read_csv["corona.csv"] print[df] country_data = df["Country"] cases_data = df["Cases"] plt.pie[cases_data, labels = country_data, autopct = '%1.1f%%'] plt.title["top 5 Countries affected by COvid-19 with highest number of Cases"] plt.legend[] plt.show[]
Below is Output
Country Cases Deaths Region 0 United States 43246791 6, 96, 918 North America 1 India 33531498 4, 45, 801 Asia 2 Brazil 21247094 5, 91, 518 South America 3 United Kingdom 7496543 1, 35, 455 Europe 4 Russia 7333557 2, 00, 625 Europe >>>
Suggestion : 2
Python Code:
import matplotlib.pyplot as plt import pandas as pd df = pd.read_csv['medal.csv'] country_data = df["country"] medal_data = df["gold_medal"] colors = ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#8c564b"] explode = [0.1, 0, 0, 0, 0] plt.pie[medal_data, labels = country_data, explode = explode, colors = colors, autopct = '%1.1f%%', shadow = True, startangle = 140] plt.title["Gold medal achievements of five most successful\n" + "countries in 2016 Summer Olympics"] plt.show[]
Suggestion : 3
Here, label is used to provide a name inside the respective portion in the chart. autopct shows the percentage for each portion.,So, this is how we can visualize our data using Python. If you have any doubts, don’t forget to mention them in the comment section below.,xlabel and ylable denote the type of data along the x-axis and y-axis respectively. plt.title allows us to mention a title for our graph. To show the graph, we use a function show[].,In today’s world, visualizing data is an important part of any domain. Visualized data is easy to understand that is why it is preferred over excel sheets. Python came to our rescue with its libraries like pandas and matplotlib so that we can represent our data in a graphical form. In this tutorial, we will be learning how to visualize the data in the CSV file using Python.
Now since you know how to read a CSV file, let’s see the code.
import pandas as pd import matplotlib.pyplot as plt csv_file = 'data.csv' data = pd.read_csv[csv_file]
We will now extract Genre and TotalVotes from this dataset.
Votes = data["TotalVotes"] Genre = data["Genre"]
Now, we will store these data into two different lists. We need to create two empty lists first.
Suggestion : 4
We’ll create a series of pie charts showing crimes in London boroughs. We’ll use a .csv file for plotting. You can download the “London Crime Data, 2008–2016” dataset on Kaggle [london_crime_by_lsoa.csv].,We’ll use the for loop to iterate rows and create a pie chart for each of them. We’ll iterate only 8 rows in this example so we’re using table.head[8] instead of table.,That’s it, our Matplotlib pie charts are ready. You can download the notebook on GitHub to get the full code.,You can download the latest version of Python for Windows on the official website.
To get other tools, you’ll need to install recommended Scientific Python Distributions. Type this in your terminal:
pip install numpy scipy matplotlib ipython jupyter pandas sympy nose
Create a folder that will contain your notebook [e.g. “matplotlib-pie-chart”] and open Jupyter Notebook by typing this command in your terminal [don’t forget to change the path]:
cd C: \Users\ Shark\ Documents\ code\ matplotlib - pie - chart py - m notebook
In the first line of the notebook, import all the necessary libraries:
import matplotlib.pyplot as plt import matplotlib as mpl import pandas as pd % matplotlib notebook
Suggestion : 5
Last Updated : 03 Mar, 2021,GATE CS 2021 Syllabus
To extract the data in CSV file, CSV module must be imported in our program as follows:
import csv with open['file.csv'] as File: Line_reader = csv.reader[File]