Ch 4 Plotting Data Using Mathplotlib 2024-25
Ch 4 Plotting Data Using Mathplotlib 2024-25
Syllabus 2024-25 :
Purpose of plotting;
drawing and saving following types of plots using Matplotlib
line plot, bar graph, histogram
Customizing plots: adding label, title, and legend in plots.
Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 7002070313. Last Updated : Wednesday, 26 Jun 2024 2
Introduction to Data Visualization – Class Notes, 2024-25
Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 7002070313. Last Updated : Wednesday, 26 Jun 2024 3
Introduction to Data Visualization – Class Notes, 2024-25
16. Write a program to draw single line along with proper labels, titles and
legends.
Ans. import matplotlib.pyplot as plt
x = [1,2,3] # x axis values
y = [2,4,1] # corresponding y axis values
plt.plot(x, y) # plotting the points
plt.xlabel('x - axis') # naming the x axis
plt.ylabel('y - axis') # naming the y axis
plt.title('My First Graph!') # giving a title to my graph
plt.legend() #involves a legend
plt.show() # function to show the plot
17. Write a program to draw two lines along with proper labels, titles and
legends.
Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 7002070313. Last Updated : Wednesday, 26 Jun 2024 4
Introduction to Data Visualization – Class Notes, 2024-25
18. Write a program to draw Multiple lines with multiple colors given explicitly
using numpy.
Ans. import matplotlib.pyplot as plt
import numpy as np
y = np.arange(1, 3)
plt.plot(y, 'y')
plt.plot(y+1, 'm')
plt.plot(y+2, 'c')
plt.show()
Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 7002070313. Last Updated : Wednesday, 26 Jun 2024 5
Introduction to Data Visualization – Class Notes, 2024-25
20. Write a program to draw two or more lines with different widths and colors
with suitable legends.
Ans. import matplotlib.pyplot as plt
x1 = [10,20,30] # line 1 points
y1 = [20,40,10]
x2 = [10,20,30] # line 2 points
y2 = [40,10,30]
plt.xlabel('x - axis') # Set the x axis label of the current
axis
plt.ylabel('y - axis') # Set the y axis label of the current
axis.
# Set a title
plt.title('Two or more lines with different widths and
colors with suitable legends')
# Display the figure.
plt.plot(x1,y1, color='blue', linewidth = 3, label =
'line1-width-3')
plt.plot(x2,y2, color='red', linewidth = 5, label = 'line2-
width-5')
plt.legend() # show a legend on the plot
plt.show()
Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 7002070313. Last Updated : Wednesday, 26 Jun 2024 6
Introduction to Data Visualization – Class Notes, 2024-25
Linestyle values
'-' or 'solid' solid line (default)
'--' or 'dashed' dashed line
'-.' or 'dashdot' dash-dot line
':' or 'dotted' dotted line
'None' or ' ' or '' no line
22. How to customise or add details to the plot such as title, axis, labels, legend,
grid etc. after plt.plot() line?
Ans. After the plt.plot() line, add details such as a title, axis labels, legend, grid,
and tick labels.
• plt.title('<title string>‘)
• plt.xlabel('<x-axis label string>‘)
• plt.ylabel('<y-axis label string>‘)
• plt.legend(['list','of','strings'])
• ptl.grid(<True or False>)
Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 7002070313. Last Updated : Wednesday, 26 Jun 2024 7
Introduction to Data Visualization – Class Notes, 2024-25
25. Which function is used to draw bar graph with pylot module?
Ans. With Pyplot, you can use the bar() or barh() function to draw bar graphs.
26. Write the function to draw vertical and horizontal bars with its arguments.
Ans. The bar() function takes arguments that describes the layout of the Vertical
bars. Syntax-
plt.bar(x, y, width, color)
The barh() function takes arguments that describes the layout of the
Horizontal bars. Syntax-
plt.barh(x, y,height,color)
Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 7002070313. Last Updated : Wednesday, 26 Jun 2024 8
Introduction to Data Visualization – Class Notes, 2024-25
27. What is the default width and height of the vertical and horizontal bars
respectively?
Ans. The default width of the Vertical bar is 0.8
The default height of the Horizontal bar is 0.8
28. Write a program to draw four vertical bars.
Ans. #Program to draw 4 vertical bar
import matplotlib.pyplot as plt
x = ["A", "B", "C", "D"]
y = [3, 8, 1, 10]
plt.bar(x,y)
plt.show()
Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 7002070313. Last Updated : Wednesday, 26 Jun 2024 9
Introduction to Data Visualization – Class Notes, 2024-25
Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 7002070313. Last Updated : Wednesday, 26 Jun 2024 10
Introduction to Data Visualization – Class Notes, 2024-25
Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 7002070313. Last Updated : Wednesday, 26 Jun 2024 11
Introduction to Data Visualization – Class Notes, 2024-25
37. Write a program to plot a histogram with different formatting and suitable
title and labels and also to save the Graph/plot to the default location i.e.
source file location.
Ans. import numpy as np
import matplotlib.pyplot as plt
data_students=[1,11,21,31,41,51]
plt.hist(data_students,bins=[0,10,20,30,40,50,60],weights=[1
0,1,0,33,6,8],facecolor='yellow',edgecolor="red")
plt.title("Histogram for Students data“, fontsize=15)
plt.xlabel('Value‘, fontsize=15)
plt.ylabel('aFrequency‘, fontsize=15)
plt.savefig("student.png") #Save graph
plt.show()
Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 7002070313. Last Updated : Wednesday, 26 Jun 2024 12
Introduction to Data Visualization – Class Notes, 2024-25
Or,
Name the different image file format that can be used to save the Matplotlib
plot.
Or,
What is dpi argument in savefig() function?
Ans. Matplotlib plots can be saved as image files using
the plt.savefig() function and gets saved in the current working
directory.
The plt.savefig() function needs to be called right above
the plt.show() line.
All the features of the plot must be specified before the plot is saved as an
image file.
If the figure is saved after the plt.show() command; the figure will not
be saved until the plot window is closed.
plt.savefig(image.png,dpi=300)
Image file format (.png, .jpg, .svg. .pdf etc) based on the extension
specified in the filename.
dpi stands for dots per inch : dpi=300 specifies how many dots per
inch (image resolution) are in the saved image.
Example:
plt.savefig(‘plot.pdf')
plt.savefig(‘plot.svg‘)
plt.savefig(‘plot.png')
39. Write a program to save a plot in .png format.
Ans. import matplotlib.pyplot as plt
x = [0, 2, 4, 6]
y = [1, 3, 4, 8]
plt.plot(x,y)
plt.xlabel('x values')
plt.ylabel('y values')
plt.title('plotted x and y values')
plt.legend(['line 1'])
# save the figure
plt.savefig('plot.png', dpi=300, bbox_inches='tight')
plt.show()
Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 7002070313. Last Updated : Wednesday, 26 Jun 2024 13
Introduction to Data Visualization – Class Notes, 2024-25
40. Write the various graph methods used with matplotlib.pyplot modules.
Ans. Line Plot – plt.plot()
Vertical Bar Plot – plt.bar()
Horizontal Bar Plot – plt.barh()
Histogram – plt.hist()
Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 7002070313. Last Updated : Wednesday, 26 Jun 2024 14
Introduction to Data Visualization – Class Notes, 2024-25
Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 7002070313. Last Updated : Wednesday, 26 Jun 2024 16
Introduction to Data Visualization – Class Notes, 2024-25
Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 7002070313. Last Updated : Wednesday, 26 Jun 2024 17
Introduction to Data Visualization – Class Notes, 2024-25
plt.show()
Ans.
Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 7002070313. Last Updated : Wednesday, 26 Jun 2024 18
Introduction to Data Visualization – Class Notes, 2024-25
Alternative answer :
mtp.show()
Ans. mtp.bar(Months, Profits)
10. A pie chart is to be drawn (using pyplot) to represent Population of
States. Fill in the blank with correct statement using a matplotlib
method to draw the pie chart with labels for the pie slices as the
names of the States and the size of each pie slice representing the
corresponding Population of the States (in crores), as per the
following table : [AI, 2021, 1 Marks]
Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 7002070313. Last Updated : Wednesday, 26 Jun 2024 19
Introduction to Data Visualization – Class Notes, 2024-25
plt.show()
Ans. plt.pie(Population, labels=States)
11. The table below shows the Marks of two students for the four unit
tests for academic session 2019-2020. Fill in the blanks to draw a
line graph with Test Names on the X axis and Marks on the Y
axis. [AI, 2021, 2 Marks]
Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 7002070313. Last Updated : Wednesday, 26 Jun 2024 20
Introduction to Data Visualization – Class Notes, 2024-25
b) Write code to draw the following bar graph representing the total
number of medals won by Australia. [AI, 2021, Compt, 3 Marks]
Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 7002070313. Last Updated : Wednesday, 26 Jun 2024 21
Introduction to Data Visualization – Class Notes, 2024-25
Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 7002070313. Last Updated : Wednesday, 26 Jun 2024 22
Introduction to Data Visualization – Class Notes, 2024-25
20. Write Python code to plot a bar chart for India‘s medal tally as
shown below: [SQP, 2022-23, 5 Mark]
Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 7002070313. Last Updated : Wednesday, 26 Jun 2024 23
Introduction to Data Visualization – Class Notes, 2024-25
Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 7002070313. Last Updated : Wednesday, 26 Jun 2024 24
Introduction to Data Visualization – Class Notes, 2024-25
23. Write Python code to draw the following bar graph representing the total
sales in each quarter. Add the Title, Label for X-axis and Y-axis.
Use the following data for plotting the graph: [AI, 2023, 5 Marks]
sales=[450,300,500,650]
qtr=["QTR1","QTR2","QTR3","QTR4"]
Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 7002070313. Last Updated : Wednesday, 26 Jun 2024 25
Introduction to Data Visualization – Class Notes, 2024-25
Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 7002070313. Last Updated : Wednesday, 26 Jun 2024 26
Introduction to Data Visualization – Class Notes, 2024-25
Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 7002070313. Last Updated : Wednesday, 26 Jun 2024 27
Introduction to Data Visualization – Class Notes, 2024-25
Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 7002070313. Last Updated : Wednesday, 26 Jun 2024 28
Introduction to Data Visualization – Class Notes, 2024-25
plt.xlabel("Fruits") #Statement 6
plt.ylabel("Price") #Statement 7
plt.savefig("fruits.png") #Statement 8
plt.show() #Statement 9
28. Write suitable Python code to draw the following line chart "CO2 Emission"
having titleand label for X and Y axis as shown below.
Also give suitable Python statement to save this chart with the name,
emission.png [AI, 2024, 5 Mark]
Ans. import matplotlib.pyplot as plt #Statement 1
month=["April", "May", "June", "July", "August"] #Statement 2
percent=[40,50,30,60,20] #Statement 3
plt.plot(month, percent) #Statement 4
plt.title("Month wise CO2 emission") #Statement 5
plt.xlabel("Month") #Statement 6
plt.ylabel("Percentage") #Statement 7
plt.savefig("emission.png") #Statement 8
plt.show() #Statement 9
Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 7002070313. Last Updated : Wednesday, 26 Jun 2024 29