DATA
VISUALIZATION
(matplotlib.pyplot)
Data Visualization
 Purpose of plotting;
 drawing and saving following types of plots using
matplotlib – line plot, bar graph, histogram, pie
chart, frequency polygon, box plot and scatter
plot.
 Customizing plots: color, style (dashed, dotted),
width; adding label, title, and legend in plots.
1
Sangita Panchal
Purpose of Plotting 2
Sangita Panchal
 One of the most popular uses for Python is data analysis.
Data scientists want a way to visualize their data to get a
better grasp of the data, and to convey their results to
someone.
 Matplotlib is a Python library used for plotting. It is used to
create 2d Plots and graphs easily and control font properties,
line controls, formatting axes, etc. through Python script.
Line Chart 3
Sangita Panchal
import matplotlib.pyplot as plt
plt.plot([1,2,3],[4,5,1])
plt.show()
import matplotlib.pyplot as plt
plt.plot([4,5,1])
plt.show()
Line Chart 3
Sangita Panchal
import matplotlib.pyplot as plt
plt.plot([‘A’,’B’,’C’],[4,5,1])
plt.show()
import matplotlib.pyplot as plt
plt.plot([1,5],[2,6])
plt.show()
Line Chart
Try all the commands
4
Sangita Panchal
import matplotlib.pyplot as plt
x = ['A1','B1','C1']
y = [31,27,40]
x1 = ['A1','B1','C1','D1']
y1 = [12,20,19,17]
plt.plot(x,y,label = "Sr. School")
plt.plot(x1,y1,label = "Jr.School")
plt.title("Bus Info")
plt.ylabel("No. of Students")
plt.xlabel("Bus No.")
plt.legend()
plt.savefig("businfo")
plt.show()
Title
xlabel
ylabel Legend
Line Chart 5
Sangita Panchal
Save the figure
plt.savefig(“linechart.pdf”)
# saves in the current directory
plt.savefig(“c:datamultibar.pdf”)
# saves in the given path
plt.savefig(“c:datamultibar.png”)
# saves in the given path in .png format
Bar Chart 9
Sangita Panchal
import matplotlib.pyplot as plt
# Students in each section
x = ['A','B','C','D','E']
y = [31,27,40,43,45]
plt.bar(x,y,label = "No.of students")
plt.title("Section wise number of students")
plt.ylabel("No. of Students")
plt.xlabel("Section")
plt.legend()
plt.show()
Bar Chart Horizontal 10
Sangita Panchal
import matplotlib.pyplot as plt
# Students in each section
x = ['A','B','C','D','E']
y = [31,27,40,43,45]
plt.barh(x,y,label = "No.of students")
plt.title("Section wise number of students")
plt.ylabel("No. of Students")
plt.xlabel("Section")
plt.legend()
plt.show()
Bar Chart Stacked 11
Sangita Panchal
import matplotlib.pyplot as plt
# Number of students in each bus
x = [‘A’, ‘B’, ‘C’, ‘D’]
y = [31,27,40,32]
y1 = [12,20,19,17]
plt.bar(x,y,label = "Sr. School")
plt.bar(x,y1,label = "Jr.School")
plt.title("Bus Info")
plt.ylabel("No. of Students")
plt.xlabel("Bus No.")
plt.legend()
plt.show()
Bar Chart – Multiple 12
Sangita Panchal
import matplotlib.pyplot as plt
import numpy as np
# Number of students in each bus
x = np.arange(1,5)
y = [31,27,40,32]
y1 = [12,20,19,17]
plt.bar(x+0,y,label = "Sr. School",width = 0.4)
plt.bar(x+0.4,y1,label = "Jr.School",width = 0.4)
plt.title("Bus Info")
plt.ylabel("No. of Students")
plt.xlabel("Bus No.")
plt.legend()
plt.show()
Histogram 13
Sangita Panchal
import matplotlib.pyplot as plt
import numpy as np
# Marks obtained by 50 students
x = [5,15,20,25,35,45,55]
y = np.arange(0,61,10)
w = [2,3,8,9,12,6,10]
plt.hist(x,bins = y, weights = w,
label = "Marks Obtained")
plt.title("Histogram")
plt.ylabel("Marks Range")
plt.xlabel("No. of Students")
plt.legend()
plt.show()
Histogram 13
Sangita Panchal
import matplotlib.pyplot as plt
import numpy as np
# Marks obtained by 50 students
x = [5,15,20,25,35,45,55,70,84,90,93]
y = [0,33,45,60,75,90,100]
w = [2,3,8,9,12,6,10,5,8,12,7]
plt.hist(x,bins = y, weights = w,
label = "Marks Obtained",
edgecolor = "yellow")
plt.title("Histogram")
plt.ylabel("Marks Range")
plt.xlabel("No. of Students")
plt.legend()
plt.show()
Difference between Bar graph and Histogram 13
Sangita Panchal
Difference between Bar graph and Histogram 14
Sangita Panchal
Histogram Bar
Histogram is defined as a type of bar chart that to
show the frequency distribution of continuous data.
It indicates the number of observations which lie in-
between the range of values, known as class or bin.
Take the observations and split them into logical
series of intervals called bins.
X-axis indicates, independent variables i.e. bins
Y-axis represents dependent variables i.e.
occurrences.
Rectangle blocks i.e. bars are shown on the x-axis,
whose area depends on the bins.
A bar graph is a chart that represents the comparison
between categories of data.
It displays grouped data by way of parallel rectangular
bars of equal width but varying the length.
Each rectangular block indicates specific category and
the length of the bars depends on the values they
hold. The bars do not touch each other.
Bar diagram can be horizontal or vertical, where a
horizontal bar graph is used to display data varying
over space whereas the vertical bar graph represents
time series data. It contains two axis, where one axis
represents the categories and the other axis shows
the discrete values of the data.
Difference between Bar graph and Histogram 15
Sangita Panchal
Histogram Bar
A graphical representation to
show the frequency of numerical
data.
A pictorial representation of data
to compare different category of
data.
Bars touch each other, hence
there are no spaces between
bars.
Bars do not touch each other,
hence there are spaces between
bars.
The width of bar may or may not
same (depends on bins)
The width of bar remain same.
CBSE Questions 16
Sangita Panchal
Fill in the blanks :
1. The command used to give a heading to a graph is _________
a. plt.show()
b. plt.plot()
c. plt.xlabel()
d. plt.title()
2. Using Python Matplotlib _________ can be used to count how
many values fall into each interval
a. line plot
b. bar graph
c. histogram
CBSE Questions 17
Sangita Panchal
Fill in the blanks :
1. The command used to give a heading to a graph is _________
a. plt.show()
b. plt.plot()
c. plt.xlabel()
d. plt.title()
2. Using Python Matplotlib _________ can be used to count how
many values fall into each interval
a. line plot
b. bar graph
c. histogram
plt.title()
histogram
CBSE Questions 18
Sangita Panchal
Rashmi wants to plot a bar graph for the department name on x
– axis and number of employees in each department on y-axis.
Complete the code to perform the following:
(i) To plot the bar graph in statement 1
(ii) To display the graph on screen in statement 2
import matplotlib as plt
x = [‘Marketing’, ‘Office’, ‘Manager’, ‘Development’]
y = [ 35, 29, 30, 25]
_______________________________ Statement 1
_______________________________ Statement 2
CBSE Questions 19
Sangita Panchal
Rashmi wants to plot a bar graph for the department name on x
– axis and number of employees in each department on y-axis.
Complete the code to perform the following:
(i) To plot the bar graph in statement 1
(ii) To display the graph on screen in statement 2
import matplotlib as plt
x = [‘Marketing’, ‘Office’, ‘Manager’, ‘Development’]
y = [ 35, 29, 30, 25]
_______________________________ Statement 1
_______________________________ Statement 2
plt.bar(x,y)
plt.show()
CBSE Questions 20
Sangita Panchal
Anamica wants to draw a line chart using a list of elements
named L. Complete the code to perform the following
operations:
(i) To plot a line chart using the list L
(ii) To give a y-axis label to the line chart as “List of Numbers”
import matplotlib.pyplot as plt
L = [10, 12, 15, 18, 25, 35, 50, 76, 89]
_______________________________ Statement 1
________________________________ Statement 2
plt.show()
CBSE Questions 21
Sangita Panchal
Anamica wants to draw a line chart using a list of elements
named L. Complete the code to perform the following
operations:
(i) To plot a line chart using the list L
(ii) To give a y-axis label to the line chart as “List of Numbers”
import matplotlib.pyplot as plt
L = [10, 12, 15, 18, 25, 35, 50, 76, 89]
_______________________________ Statement 1
________________________________ Statement 2
plt.show()
plt.plot(L)
plt.ylabel(“List of Numbers”)
CBSE Questions 22
Sangita Panchal
Write a code to plot the speed of a passenger train as shown in
the figure given below.
CBSE Questions 23
Sangita Panchal
Write a code to plot the speed of a passenger train as shown in
the figure given below.
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(1, 5)
plt.plot(x, x*2.0, label='Normal')
plt.plot(x, x*3.0, label='Fast')
plt.plot(x, x/2.0, label='Slow')
plt.legend()
plt.show()
CBSE Questions 24
Sangita Panchal
Consider the following graph . Write the code to plot it.
CBSE Questions 25
Sangita Panchal
Consider the following graph . Write the code to plot it.
SOLUTION 1
import matplotlib.pyplot as plt
plt.plot([2,7],[1,6])
plt.show()
SOLUTION 2
import matplotlib.pyplot as plt
Y = [1,2,3,4,5,6]
X = [2,3,4,5,6,7]
plt.plot (X, Y)
CBSE Questions 26
Sangita Panchal
Draw the following bar graph representing the number of
students in each class.
CBSE Questions 27
Sangita Panchal
Draw the following bar graph representing the number of students in
each class.
import matplotlib.pyplot as plt
Classes = ['VII','VIII','IX','X']
Students = [40,45,35,44]
plt.bar(classes, students)
plt.show()
CBSE Questions 28
Sangita Panchal
Write a code to plot a bar chart to depict the pass
percentage of students in CBSE exams for the last four years
as shown below:
CBSE Questions 29
Sangita Panchal
Write a code to plot a bar chart to depict the pass
percentage of students in CBSE exams for the last four years
as shown below:
import matplotlib.pyplot as plt
objects= ["Year1","Year2","Year3","Year4"]
percentage=[82,83,85,90]
plt.bar(objects, percentage)
plt.ylabel("Pass Percentage")
plt.xlabel("Years")
plt.show()
CBSE Questions 30
Sangita Panchal
Mr. Vinay wants to plot line charts to compare the trend in the price of diesel and
petrol over the last few weeks from the following data in Python Lists. Help him
write the complete code by filling in the blanks :
(i) To plot the price of diesel in statement 1
(ii) To plot the price of petrol in statement 2
(iii) To give a suitable heading to the graph in statement 3
(iv) To display the legends in statement 4
(v) To display the graph in statement 5
import matplotlib.pyplot as plt
petrol = [ 80 , 82 , 82.50 , 81, 81.50 ]
diesel = [ 73 , 77 , 74 , 74.5 , 75.4 ]
_____________________ Statement 1
_____________________ Statement 2
_____________________ Statement 3
_____________________ Statement 4
_____________________ Statement 5
CBSE Questions 31
Sangita Panchal
Mr. Vinay wants to plot line charts to compare the trend in the price of diesel and
petrol over the last few weeks from the following data in Python Lists. Help him
write the complete code by filling in the blanks :
(i) To plot the price of diesel in statement 1
(ii) To plot the price of petrol in statement 2
(iii) To give a suitable heading to the graph in statement 3
(iv) To display the legends in statement 4
(v) To display the graph in statement 5
import matplotlib.pyplot as plt
petrol = [ 80 , 82 , 82.50 , 81, 81.50 ]
diesel = [ 73 , 77 , 74 , 74.5 , 75.4 ]
_____________________ Statement 1
_____________________ Statement 2
_____________________ Statement 3
_____________________ Statement 4
_____________________ Statement 5
plt.plot(diesel)
plt.plot(petrol)
plt.title(‘Price of Diesel vs petrol’)
plt.legend(‘Diesel’,’Petrol’)
plt.show()
CBSE Questions 32
Sangita Panchal
Mr. Vinay wants to plot line charts to compare the trend in the price of diesel and petrol over
the last few weeks from the following data in Python Lists. Help him write the complete code
by filling in the blanks :
Following is the data ( in hours ) of screen time spent on a device by sample students:
6,6,5,7,9,7,4,5,4,5,7,8,4,5,7,6,5,7,8,4,3,3,6,5.
The data is plotted to generate a histogram as follows:
Fill in the blanks to complete the code:
(i) To plot the histogram from the given data in Statement1
(ii) To save the figure as “Plot3.png” in Statement2
import matplotlib.pyplot as plt
hours =[6,6,5,7,9,7,4,5,4,5,7,8,4,5,7,6,5,7,8,4,3,3,6,5]
______________________ # Statement1
______________________ # Statement2
CBSE Questions 33
Sangita Panchal
Mr. Vinay wants to plot line charts to compare the trend in the price of diesel and petrol over
the last few weeks from the following data in Python Lists. Help him write the complete code
by filling in the blanks :
Following is the data ( in hours ) of screen time spent on a device by sample students:
6,6,5,7,9,7,4,5,4,5,7,8,4,5,7,6,5,7,8,4,3,3,6,5.
The data is plotted to generate a histogram as follows:
Fill in the blanks to complete the code:
(i) To plot the histogram from the given data in Statement1
(ii) To save the figure as “Plot3.png” in Statement2
import matplotlib.pyplot as plt
hours =[6,6,5,7,9,7,4,5,4,5,7,8,4,5,7,6,5,7,8,4,3,3,6,5]
______________________ # Statement1
______________________ # Statement2
plt.hist(hours)
plt.savefig(‘plot3.png’)
Subplots 34
Sangita Panchal
import matplotlib.pyplot as plt
fig,a = plt.subplots(2,2)
import numpy as np
x = np.arange(1,5)
y = [25,23,19,16]
y1 = [15,20,25,35]
fig.suptitle("Four Graphs")
a[0][0].plot(x,y,color = "g")
a[0][0].set_title('Line chart')
a[0][1].bar(x,y,color = "r")
a[0][1].set_title('Bar chart')
a[1][0].hist(y1,weights = [9,12,15,25],bins
=[10,20,30,40],color = "y",edgecolor = "b")
a[1][0].set_title('Histogram chart')
a[1][1].barh(x,y)
a[1][1].set_title('Horizontal Bar chart')
plt.show()
Data Visualization 2020_21

Data Visualization 2020_21

  • 1.
  • 2.
    Data Visualization  Purposeof plotting;  drawing and saving following types of plots using matplotlib – line plot, bar graph, histogram, pie chart, frequency polygon, box plot and scatter plot.  Customizing plots: color, style (dashed, dotted), width; adding label, title, and legend in plots. 1 Sangita Panchal
  • 3.
    Purpose of Plotting2 Sangita Panchal  One of the most popular uses for Python is data analysis. Data scientists want a way to visualize their data to get a better grasp of the data, and to convey their results to someone.  Matplotlib is a Python library used for plotting. It is used to create 2d Plots and graphs easily and control font properties, line controls, formatting axes, etc. through Python script.
  • 4.
    Line Chart 3 SangitaPanchal import matplotlib.pyplot as plt plt.plot([1,2,3],[4,5,1]) plt.show() import matplotlib.pyplot as plt plt.plot([4,5,1]) plt.show()
  • 5.
    Line Chart 3 SangitaPanchal import matplotlib.pyplot as plt plt.plot([‘A’,’B’,’C’],[4,5,1]) plt.show() import matplotlib.pyplot as plt plt.plot([1,5],[2,6]) plt.show()
  • 6.
    Line Chart Try allthe commands 4 Sangita Panchal import matplotlib.pyplot as plt x = ['A1','B1','C1'] y = [31,27,40] x1 = ['A1','B1','C1','D1'] y1 = [12,20,19,17] plt.plot(x,y,label = "Sr. School") plt.plot(x1,y1,label = "Jr.School") plt.title("Bus Info") plt.ylabel("No. of Students") plt.xlabel("Bus No.") plt.legend() plt.savefig("businfo") plt.show() Title xlabel ylabel Legend
  • 7.
    Line Chart 5 SangitaPanchal Save the figure plt.savefig(“linechart.pdf”) # saves in the current directory plt.savefig(“c:datamultibar.pdf”) # saves in the given path plt.savefig(“c:datamultibar.png”) # saves in the given path in .png format
  • 8.
    Bar Chart 9 SangitaPanchal import matplotlib.pyplot as plt # Students in each section x = ['A','B','C','D','E'] y = [31,27,40,43,45] plt.bar(x,y,label = "No.of students") plt.title("Section wise number of students") plt.ylabel("No. of Students") plt.xlabel("Section") plt.legend() plt.show()
  • 9.
    Bar Chart Horizontal10 Sangita Panchal import matplotlib.pyplot as plt # Students in each section x = ['A','B','C','D','E'] y = [31,27,40,43,45] plt.barh(x,y,label = "No.of students") plt.title("Section wise number of students") plt.ylabel("No. of Students") plt.xlabel("Section") plt.legend() plt.show()
  • 10.
    Bar Chart Stacked11 Sangita Panchal import matplotlib.pyplot as plt # Number of students in each bus x = [‘A’, ‘B’, ‘C’, ‘D’] y = [31,27,40,32] y1 = [12,20,19,17] plt.bar(x,y,label = "Sr. School") plt.bar(x,y1,label = "Jr.School") plt.title("Bus Info") plt.ylabel("No. of Students") plt.xlabel("Bus No.") plt.legend() plt.show()
  • 11.
    Bar Chart –Multiple 12 Sangita Panchal import matplotlib.pyplot as plt import numpy as np # Number of students in each bus x = np.arange(1,5) y = [31,27,40,32] y1 = [12,20,19,17] plt.bar(x+0,y,label = "Sr. School",width = 0.4) plt.bar(x+0.4,y1,label = "Jr.School",width = 0.4) plt.title("Bus Info") plt.ylabel("No. of Students") plt.xlabel("Bus No.") plt.legend() plt.show()
  • 12.
    Histogram 13 Sangita Panchal importmatplotlib.pyplot as plt import numpy as np # Marks obtained by 50 students x = [5,15,20,25,35,45,55] y = np.arange(0,61,10) w = [2,3,8,9,12,6,10] plt.hist(x,bins = y, weights = w, label = "Marks Obtained") plt.title("Histogram") plt.ylabel("Marks Range") plt.xlabel("No. of Students") plt.legend() plt.show()
  • 13.
    Histogram 13 Sangita Panchal importmatplotlib.pyplot as plt import numpy as np # Marks obtained by 50 students x = [5,15,20,25,35,45,55,70,84,90,93] y = [0,33,45,60,75,90,100] w = [2,3,8,9,12,6,10,5,8,12,7] plt.hist(x,bins = y, weights = w, label = "Marks Obtained", edgecolor = "yellow") plt.title("Histogram") plt.ylabel("Marks Range") plt.xlabel("No. of Students") plt.legend() plt.show()
  • 14.
    Difference between Bargraph and Histogram 13 Sangita Panchal
  • 15.
    Difference between Bargraph and Histogram 14 Sangita Panchal Histogram Bar Histogram is defined as a type of bar chart that to show the frequency distribution of continuous data. It indicates the number of observations which lie in- between the range of values, known as class or bin. Take the observations and split them into logical series of intervals called bins. X-axis indicates, independent variables i.e. bins Y-axis represents dependent variables i.e. occurrences. Rectangle blocks i.e. bars are shown on the x-axis, whose area depends on the bins. A bar graph is a chart that represents the comparison between categories of data. It displays grouped data by way of parallel rectangular bars of equal width but varying the length. Each rectangular block indicates specific category and the length of the bars depends on the values they hold. The bars do not touch each other. Bar diagram can be horizontal or vertical, where a horizontal bar graph is used to display data varying over space whereas the vertical bar graph represents time series data. It contains two axis, where one axis represents the categories and the other axis shows the discrete values of the data.
  • 16.
    Difference between Bargraph and Histogram 15 Sangita Panchal Histogram Bar A graphical representation to show the frequency of numerical data. A pictorial representation of data to compare different category of data. Bars touch each other, hence there are no spaces between bars. Bars do not touch each other, hence there are spaces between bars. The width of bar may or may not same (depends on bins) The width of bar remain same.
  • 17.
    CBSE Questions 16 SangitaPanchal Fill in the blanks : 1. The command used to give a heading to a graph is _________ a. plt.show() b. plt.plot() c. plt.xlabel() d. plt.title() 2. Using Python Matplotlib _________ can be used to count how many values fall into each interval a. line plot b. bar graph c. histogram
  • 18.
    CBSE Questions 17 SangitaPanchal Fill in the blanks : 1. The command used to give a heading to a graph is _________ a. plt.show() b. plt.plot() c. plt.xlabel() d. plt.title() 2. Using Python Matplotlib _________ can be used to count how many values fall into each interval a. line plot b. bar graph c. histogram plt.title() histogram
  • 19.
    CBSE Questions 18 SangitaPanchal Rashmi wants to plot a bar graph for the department name on x – axis and number of employees in each department on y-axis. Complete the code to perform the following: (i) To plot the bar graph in statement 1 (ii) To display the graph on screen in statement 2 import matplotlib as plt x = [‘Marketing’, ‘Office’, ‘Manager’, ‘Development’] y = [ 35, 29, 30, 25] _______________________________ Statement 1 _______________________________ Statement 2
  • 20.
    CBSE Questions 19 SangitaPanchal Rashmi wants to plot a bar graph for the department name on x – axis and number of employees in each department on y-axis. Complete the code to perform the following: (i) To plot the bar graph in statement 1 (ii) To display the graph on screen in statement 2 import matplotlib as plt x = [‘Marketing’, ‘Office’, ‘Manager’, ‘Development’] y = [ 35, 29, 30, 25] _______________________________ Statement 1 _______________________________ Statement 2 plt.bar(x,y) plt.show()
  • 21.
    CBSE Questions 20 SangitaPanchal Anamica wants to draw a line chart using a list of elements named L. Complete the code to perform the following operations: (i) To plot a line chart using the list L (ii) To give a y-axis label to the line chart as “List of Numbers” import matplotlib.pyplot as plt L = [10, 12, 15, 18, 25, 35, 50, 76, 89] _______________________________ Statement 1 ________________________________ Statement 2 plt.show()
  • 22.
    CBSE Questions 21 SangitaPanchal Anamica wants to draw a line chart using a list of elements named L. Complete the code to perform the following operations: (i) To plot a line chart using the list L (ii) To give a y-axis label to the line chart as “List of Numbers” import matplotlib.pyplot as plt L = [10, 12, 15, 18, 25, 35, 50, 76, 89] _______________________________ Statement 1 ________________________________ Statement 2 plt.show() plt.plot(L) plt.ylabel(“List of Numbers”)
  • 23.
    CBSE Questions 22 SangitaPanchal Write a code to plot the speed of a passenger train as shown in the figure given below.
  • 24.
    CBSE Questions 23 SangitaPanchal Write a code to plot the speed of a passenger train as shown in the figure given below. import matplotlib.pyplot as plt import numpy as np x = np.arange(1, 5) plt.plot(x, x*2.0, label='Normal') plt.plot(x, x*3.0, label='Fast') plt.plot(x, x/2.0, label='Slow') plt.legend() plt.show()
  • 25.
    CBSE Questions 24 SangitaPanchal Consider the following graph . Write the code to plot it.
  • 26.
    CBSE Questions 25 SangitaPanchal Consider the following graph . Write the code to plot it. SOLUTION 1 import matplotlib.pyplot as plt plt.plot([2,7],[1,6]) plt.show() SOLUTION 2 import matplotlib.pyplot as plt Y = [1,2,3,4,5,6] X = [2,3,4,5,6,7] plt.plot (X, Y)
  • 27.
    CBSE Questions 26 SangitaPanchal Draw the following bar graph representing the number of students in each class.
  • 28.
    CBSE Questions 27 SangitaPanchal Draw the following bar graph representing the number of students in each class. import matplotlib.pyplot as plt Classes = ['VII','VIII','IX','X'] Students = [40,45,35,44] plt.bar(classes, students) plt.show()
  • 29.
    CBSE Questions 28 SangitaPanchal Write a code to plot a bar chart to depict the pass percentage of students in CBSE exams for the last four years as shown below:
  • 30.
    CBSE Questions 29 SangitaPanchal Write a code to plot a bar chart to depict the pass percentage of students in CBSE exams for the last four years as shown below: import matplotlib.pyplot as plt objects= ["Year1","Year2","Year3","Year4"] percentage=[82,83,85,90] plt.bar(objects, percentage) plt.ylabel("Pass Percentage") plt.xlabel("Years") plt.show()
  • 31.
    CBSE Questions 30 SangitaPanchal Mr. Vinay wants to plot line charts to compare the trend in the price of diesel and petrol over the last few weeks from the following data in Python Lists. Help him write the complete code by filling in the blanks : (i) To plot the price of diesel in statement 1 (ii) To plot the price of petrol in statement 2 (iii) To give a suitable heading to the graph in statement 3 (iv) To display the legends in statement 4 (v) To display the graph in statement 5 import matplotlib.pyplot as plt petrol = [ 80 , 82 , 82.50 , 81, 81.50 ] diesel = [ 73 , 77 , 74 , 74.5 , 75.4 ] _____________________ Statement 1 _____________________ Statement 2 _____________________ Statement 3 _____________________ Statement 4 _____________________ Statement 5
  • 32.
    CBSE Questions 31 SangitaPanchal Mr. Vinay wants to plot line charts to compare the trend in the price of diesel and petrol over the last few weeks from the following data in Python Lists. Help him write the complete code by filling in the blanks : (i) To plot the price of diesel in statement 1 (ii) To plot the price of petrol in statement 2 (iii) To give a suitable heading to the graph in statement 3 (iv) To display the legends in statement 4 (v) To display the graph in statement 5 import matplotlib.pyplot as plt petrol = [ 80 , 82 , 82.50 , 81, 81.50 ] diesel = [ 73 , 77 , 74 , 74.5 , 75.4 ] _____________________ Statement 1 _____________________ Statement 2 _____________________ Statement 3 _____________________ Statement 4 _____________________ Statement 5 plt.plot(diesel) plt.plot(petrol) plt.title(‘Price of Diesel vs petrol’) plt.legend(‘Diesel’,’Petrol’) plt.show()
  • 33.
    CBSE Questions 32 SangitaPanchal Mr. Vinay wants to plot line charts to compare the trend in the price of diesel and petrol over the last few weeks from the following data in Python Lists. Help him write the complete code by filling in the blanks : Following is the data ( in hours ) of screen time spent on a device by sample students: 6,6,5,7,9,7,4,5,4,5,7,8,4,5,7,6,5,7,8,4,3,3,6,5. The data is plotted to generate a histogram as follows: Fill in the blanks to complete the code: (i) To plot the histogram from the given data in Statement1 (ii) To save the figure as “Plot3.png” in Statement2 import matplotlib.pyplot as plt hours =[6,6,5,7,9,7,4,5,4,5,7,8,4,5,7,6,5,7,8,4,3,3,6,5] ______________________ # Statement1 ______________________ # Statement2
  • 34.
    CBSE Questions 33 SangitaPanchal Mr. Vinay wants to plot line charts to compare the trend in the price of diesel and petrol over the last few weeks from the following data in Python Lists. Help him write the complete code by filling in the blanks : Following is the data ( in hours ) of screen time spent on a device by sample students: 6,6,5,7,9,7,4,5,4,5,7,8,4,5,7,6,5,7,8,4,3,3,6,5. The data is plotted to generate a histogram as follows: Fill in the blanks to complete the code: (i) To plot the histogram from the given data in Statement1 (ii) To save the figure as “Plot3.png” in Statement2 import matplotlib.pyplot as plt hours =[6,6,5,7,9,7,4,5,4,5,7,8,4,5,7,6,5,7,8,4,3,3,6,5] ______________________ # Statement1 ______________________ # Statement2 plt.hist(hours) plt.savefig(‘plot3.png’)
  • 35.
    Subplots 34 Sangita Panchal importmatplotlib.pyplot as plt fig,a = plt.subplots(2,2) import numpy as np x = np.arange(1,5) y = [25,23,19,16] y1 = [15,20,25,35] fig.suptitle("Four Graphs") a[0][0].plot(x,y,color = "g") a[0][0].set_title('Line chart') a[0][1].bar(x,y,color = "r") a[0][1].set_title('Bar chart') a[1][0].hist(y1,weights = [9,12,15,25],bins =[10,20,30,40],color = "y",edgecolor = "b") a[1][0].set_title('Histogram chart') a[1][1].barh(x,y) a[1][1].set_title('Horizontal Bar chart') plt.show()