Matplotlib Worksheet
Matplotlib Worksheet
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
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.
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"(- . ).