0% found this document useful (0 votes)
48 views

Matplotlib

The document contains multiple problem statements and solutions related to data analysis and visualization using Matplotlib library in Python. Each problem statement provides sample input data and asks to create appropriate charts like bar plot, line plot, histogram etc to analyze and compare the data. The solutions include the code to import required libraries, define data, and use Matplotlib functions to plot the requested charts with proper labels, titles and legends. Screenshots of output plots are also provided.

Uploaded by

krrish chotala
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Matplotlib

The document contains multiple problem statements and solutions related to data analysis and visualization using Matplotlib library in Python. Each problem statement provides sample input data and asks to create appropriate charts like bar plot, line plot, histogram etc to analyze and compare the data. The solutions include the code to import required libraries, define data, and use Matplotlib functions to plot the requested charts with proper labels, titles and legends. Screenshots of output plots are also provided.

Uploaded by

krrish chotala
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Problem statement: Collect and store total medals won by 10 countries in Olympic games

and represent it in form of bar chart with title to compare an analyze data.

Solution:
Source Code:
import matplotlib.pyplot as plt
medals = [213,224,300,59,100,140,256,98,60,24]
country = ['Ger','Itly','USA','Jamca','Japan',
'India','China','Aus','Arg','Ethopia']
plt.bar(country, medals)
plt.title('Olympics Medal Tally')
plt.show()
Screenshot:

Output
Problem statement: Techtipnow Automobiles is authorized dealer of different Bikescompanies. They record the entire
sale of bikes month wise as give below:
Jan Feb Mar Apr May Jun
Honda 23 45 109 87 95 100
Suzuki 45 57 75 60 50 30
Tvs 97 80 84 68 80 108

To get proper analysis of sale performance create multiple line chart on a common plotwhere all bike sale data
are plotted.
Display appropriate x and y axis labels, legend and chart title.

import matplotlib.pyplot as plt

month = ['jan','feb','mar','apr','may', 'jun']honda =


[23,45,109,87,95,100]
suzuki = [45,57,75,60,50,30]
tvs = [97,80,84,68,80,108]
plt.plot(month,honda, label = 'honda')
plt.plot(month,suzuki, label = 'suzuki')
plt.plot(month,tvs, label = 'tvs') plt.title('Techtipnow
Automobiles Sale Analysis')plt.xlabel('Month')
plt.ylabel('No of Bikes')
plt.legend(loc = 'lower center')
plt.show()
Output
Problem statement: Given the school result data, analyses the performance of the student
on different parameters, e.g. subject wise or class wise. Create a dataframe for the above,
plot appropriate chart with title and legend.
Eng Math Phy Chm IT
9 78 89 69 92 96
10 89 91 84 90 98
11 90 80 76 82 90
12 94 98 90 96 100

Solution:
Source Code:
import pandas as pd
import matplotlib.pyplot as plt

d = {'eng':[78,89,90,94],'math':[89,91,80,98],
'phy':[69,84,76,90],'chm':[92,90,82,96],
'IT':[96,98,90,100]}
df = pd.DataFrame(d,[9,10,11,12])
print(df)
df.plot(kind = 'bar',title = 'Class Wise Marks Analysis',
xlabel = 'Class', ylabel = 'Marks')
df1 = df.T
df1.plot(kind = 'bar',title = 'Subject Wise Marks Analysis',
xlabel = 'Class', ylabel = 'Marks')
plt.legend(loc = 'lower center')
plt.show()
Screenshot:

Output
Problem statement: The following seat bookings are the daily records of a month
December from PVR cinemas:
124,124,135,156,128,189,200,150,158, 150,200,124,143,142,130,130, 170,
189,200,130, 142,167,180,143,143, 135,156,150,200,189,189,142
Construct a histogram from above data with 10 bin..
Solution:
Source Code:
import pandas as pd
import matplotlib.pyplot as plt
L = [124,124,135,156,128,189,200,150,158,
150,200,124,143,142,130,130, 170,
189,200,130, 142,167,180,143,143,
135,156,150,200,189,189,142]
plt.hist(L)
plt.title("Booking Records @ PVR")
plt.show()

Screenshot:
Output
Problem statement: Take data of your interest from an open source (e.g. data.gove.in),
aggregate and summarize it. Then plot it using different plotting functions of the Matplotlib
library.
Solution:
Source Code:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('c:\sample\Crime_data.csv')
print(df)
plt.bar(df.Crime,df.Boys,width = 0.25)
plt.bar(0.25,df.Girls,width = 0.25)
plt.bar(0.5,df.Transgender,width = 0.25)
df.plot(kind = 'bar', x = 'Crime')
df.plot(kind = 'bar', x= 'Crime', y= ['Boys','Girls', 'Transgender'])
plt.show()

Screenshot:

Output

You might also like