0% found this document useful (0 votes)
143 views11 pages

Matplotlib Worksheet

The document provides a comprehensive guide on using Python's Matplotlib library for data visualization, including code examples for plotting bar charts, line graphs, and histograms. It covers key functions such as plot(), show(), and savefig(), and explains the differences between bar graphs and histograms. Additionally, it discusses legends and their positioning in graphs, along with a brief overview of data visualization and its importance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
143 views11 pages

Matplotlib Worksheet

The document provides a comprehensive guide on using Python's Matplotlib library for data visualization, including code examples for plotting bar charts, line graphs, and histograms. It covers key functions such as plot(), show(), and savefig(), and explains the differences between bar graphs and histograms. Additionally, it discusses legends and their positioning in graphs, along with a brief overview of data visualization and its importance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

NEW INDIAN MODEL SCHOOL, SHARJAH

DATA VISUALISATION WORKSHEET

Sub:- INFORMATICS PRACTICES


1. Write a code to plot a bar chart to depict the pass percentage of students in
CBSE exams for the years 2015 to 2018 as shown below.

Ans:
import matplotlib.pyplot as plt
x=['2015', '2016', '2017', '2018']
y=[82,83,85,90]
yvalue=[10,20,30,40,50,60,70,80,90,100]
plt.bar(x,y, align='center', color='Blue')
plt.yticks(yvalue)
plt.xlabel('Years')
plt.ylabel('Pass Percentage')
plt.show()
2. The command used to give a heading to a graph is _________
a. plt.show() b. plt.plot() c. plt.xlabel() d. plt.title()
3. Using Python Matplotlib _____ can be used to count how many values fall into
each interval
a. line plot b. bar graph c. histogram
4. Out of the following, which function cannot be used for customization of charts
in Python?
a. xlabel() b. colour() c. title() d. xticks()
5. What is the minimum number of arguments required for plot() function in
matplotlib?
a. 1 b. 2 c. 3 d. 4
6. _____________ is the function to save the graph.
a. Savefig() b. Savefigure() c. Savegraph() d. Savechart()
7. Which graph should be used where each column represents a range of values,
and the height of a column corresponds to how many values are in that range?
a. plot b. line c. bar d. histogram
8. Consider the following graph. Write the code to plot it.

Ans:
import matplotlib.pyplot as plt
x=[2,3,4,5,6,7]
y=[1,2,3,4,5,6]
plt.plot(x,y)
plt.show()
9. Draw the following bar graph representing the number of students in each
class.
Ans:
import matplotlib.pyplot as plt
x=['VII','VIII','IX','X']
y=[40,45,35,44]
plt.bar(x, y)
plt.show()
10. Consider the following graph. Write the code to plot it.

Ans:
import matplotlib.pyplot as plt
x=[1,2,3]
y=[4,5,1]
plt.plot(x,y)
plt.show()
11. Read the statements given below and identify the right option to draw a
histogram.
Statement A: To make a Histogram with Matplotlib, we can use the plt.hist()
function.
Statement B: The bin parameter is compulsory to create histogram.
a. Statement A is correct
b. Statement B is correct
c. Statement A is correct, but Statement B is incorrect
d. Statement A is incorrect, but Statement B is correct
12. Write Python code to plot a bar chart for India’s medal tally as shown below.
Also give suitable python statement to save this chart.

Ans:
import matplotlib.pyplot as plt
x=['Gold','Silver','Bronze']
y=[20,15,18]
plt.bar(x,y)
plt.ylabel('Medal')
plt.xlabel('Medal Type')
plt.title('Indian Medal tally in Olympics')
plt.savefig('Medal.png')
plt.show()
13. Mr. Sharma is working in a game development industry and he was
comparing the given chart on the basis of the rating of the various games
available on the play store.

He is trying to write a code to plot the graph. Help Mr. Sharma to fill in the blanks
of the code and get the desired output.
import__________________________ #Statement 1
Games=["Subway Surfer","Temple Run","Candy Crush","Bottle Shot","Runner
Best"]
Rating=[4.2,4.8,5.0,3.8,4.1]
plt.______________(Games,Rating) #Statement 2
plt.xlabel("Games")
plt.______________("Rating") #Statement 3
plt._______________ #Statement 4

i. Choose the right code from the following for statement 1.


i. matplotlib as plt ii. pyplot as plt
iii. matplotlib.pyplot as plt iv. matplotlib.plt as pyplot
ii. Identify the name of the function that should be used in statement 2 to plot
the above graph.
i. line() ii. bar() iii. hist() iv. barh()
iii. Choose the correct option for the statement 3.
i. title("Rating") ii. ytitle("Rating") iii. ylabel("Rating")iv.
yaxis("Rating")
iv. Choose the right function/method from the following for the statement 4.
i. display() ii. print() iii. bar() iv. show()
v. In case Mr. Sharma wants to change the above plot to the any other shape,
which statement, should he change.
i. Statement 1 ii. Statement 2iii. Statement 3 iv. Statement 4
14. Write a python program to plot a line chart based on the given data to depict
the changing weekly average temperature in Delhi for four weeks.
Week=[1,2,3,4]
Avg_week_temp=[40,42,38,44]
Ans:
import matplotlib.pyplot as plt
Week=[1,2,3,4]
Avg_week_temp=[40,42,38,44]
plt.plot(Week,Avg_week_temp)
plt.show()
15. Gaurav has written a Python Code to create a bar plot as given below using
the following data :
import _______as ______ #Statement 1
City=['Delhi','Beijing','Washington','Tokyo','Moscow']
Gender=['Male','Female']
Happiness_Index_Male=[60,40,70,65,85]
Happiness_Index_Female=[30,60,70,55,75]
plt.bar([0.25,1.25,2.25,3.25,4.25],Happiness_Index_Male,color='blue',label="Mal
e",width=.5)
plt._____([.75,1.75,2.75,3.75,4.75],Happiness_Index_Female,color='Green',widt
h=.5,label="Female") #Statement 2
pos=range(len(City))
print(pos)
plt.xticks(pos,City,fontsize=10)
plt.xlabel('City', fontsize=16)
plt.ylabel('Happiness_Index', fontsize=16)
_______ #Statement 3
_______ #Statement 4
_______ #Statement 5
i. Identify the suitable code to be used in the blank space in line marked as
Statement1.
a.matplotlib as plt b.numpy as np
c.pandas as pd d.matplotlib.pyplot as plt
ii. What is the name of the function to plot the required bar graph in the line
marked as Statement 2
a. hist() b. pie() c. bar() d. scatter()
iii. Fill in the blank in statement 3 to set Chart Title as “Happiness Index across
cities by gender “and font size as 18.
a. plt.xtitle("Happiness Index across cities by gender",fontsize=18)
b. plt.title("Happiness Index across cities by gender",fontsize=18)
c. plt.ytitle("Happiness Index across cities by gender",fontsize=18)
d. plt.show("Happiness Index across cities by gender",fontsize=18)
iv. Identify the suitable code for line marked as Statement 4 to display the
legends as shown in the plot.
a.plt.showlegend() b. plt.legend() c.plt.display() d. plt.show()
v. Fill in the blank marked in Statement 5 to display the plot.
a.plt.plot() b. plt.showplot() c.plt.display() d. plt.show()
16. Fill in the blank with the correct statement to plot a bar graph using a
matplotlib method, so that Company ABC can see the graphical presentation of its
Profit figures for the 2nd quarter of the financial year 2019 (i.e. August,
September, October, November). 1
import matplotlib.pyplot as mtp
Months = ['AUG', 'SEP', 'OCT', 'NOV'] #X Axis
Profits = [125, 220, 230, 175] #Y Axis
________________________________
mtp.show()
Ans:
mtp.bar(Months,Profits)
17. The table shows the Marks of 2 students for the 4 unit tests. Fill in the blanks
to draw a line graph with Test Names on the X axis and Marks on the Y axis.
import matplotlib.pyplot as plt
Tests = ___________________ #Assign Test Names
Rohit = ___________________ #Assign Marks of Rohit
Suman = ___________________ #Assign Marks of Suman
plt.plot(Tests, Rohit, Suman)
__________ #Label Y axis as Marks
__________ #Add legends "Rohit", "Suman" for the lines
plt.show()
Ans:
Tests = [“Unit1”,“Unit12,“Unit3”,“Unit4”] #Assign Test Names
Rohit=[85,88,89,87]#Assign Marks of Rohit
Suman=[97,99,90,92] #Assign Marks of Suman
plt.ylabel(“Marks”)#Label Y axis as Marks
plt.legend() #Add legends "Rohit", "Suman" for the lines
18. Which Python package is used for 2D graphics ?
(A) matplotlib.pyplot (B) pyplot.lib (C) matplotlib.py (D)
matplotlib.plt
19. Using Python Matplotlib __________ can be used to display information as a
series of data points.
(A) line chart (B) bar graph (C) histogram (D) None of the above
20. Consider the following graph. Write the code to plot it, label the X & Y Axis

Ans:
import matplotlib.pyplot as plt
x=[1,2,3,4,5]
y=[12,14,13,15,19]
plt.plot(x,y)
plt.xlabel('Tests')
plt.ylabel('Marks Secured')
plt.show()
21. Write code to draw the following bar graph representing the total number of
medals won by Australia.

Ans:
import matplotlib.pyplot as plt
x=[‘Gold’,’Silver’,’Bronze’,’Total’]
y=[75,50,50,200]
plt.bar(x,y)
plt.xlabel('Medals won by Austraila')
plt.ylabel('Marks won')
plt.title(‘AUSTRALIA MEDAL PLOT’)
plt.show()
22. What is Data Visualization? Discuss briefly. Also mention the name of one of
the most commonly used Python library for data visualization.

Ans: Data visualization means graphical or pictorial representation of the data


using graph, chart, etc.

Matplotlib is the most commonly used python library for data visualization.
23. Mention the purpose of the following functions briefly:
(i) plot() (ii) show() (iii) savefig()
Ans:
(i) plot(): The plot() function of the pyplot module is used to create a figure. The
plot() function by default plots a line chart.
(ii) show(): The show() function is used to display the figure created using
the plot() function.
(iii) savefig(): A figure can be saved by using savefig() function
24.The default line width is 1 pixel showing a thin line.

25. The line style parameter can take a string such as "solid"(-), "dotted"(:),
"dashed" (- - ) or "dashdot"(- . ).

26. Difference between bar graph and histogram?


BAR GRAPH HISTOGRAM
A bar chart represents categorical data Histogram charts are used to describe
(data that has some labels associated distributions
with it)
Bar graph represented using Histogram represented as bars,
rectangular bars with length showing what portion of the dataset
proportional to the values that they falls in each category of bins(intervals)
represent.
In order to show comparisons, we Histograms are column-charts, where
prefer Bar charts each column represents a range of
values, and the height of a column
corresponds to how many values are in
that range
The bars can be plotted vertically or The height of each column in the
horizontally. histogram is then proportional to the
number of data points its bin contains.
Eg: each student mark is represented in bar graph, but histogram is used to
represent how many students got mark between a range.
27. What is legend(). How will you change the position of the label.
Legends are used to explain what each line means in the current figure.
plt.legend(loc='upper left')
specify any of the following legend locations:
upper right, upper left, lower left, lower right, right, center left, center right,
lower center. upper center, center

You might also like