KnowledgeBoat Logo
OPEN IN APP

Chapter 2

Data Visualization using Matplotlib

Class 12 - Informatics Practices Preeti Arora



Fill in the Blanks

Question 1

Data Visualization refers to the graphical or visual representation of information and data using visual elements like charts, graphs and maps, etc.

Question 2

Pyplot is a collection of methods with Matplotlib library which allows the user to construct 2D plots easily and interactively.

Question 3

The line chart is a graph of plotted points on two axes connected by a straight line.

Question 4

Title is the text that appears on the top of the plot and defines what the chart is about.

Question 5

The axes of a plot can be labelled using xlabel() and ylabel() functions.

Question 6

A histogram is a summarization tool for discrete or continuous data.

Question 7

Pyplot module's hist() let us create histograms.

Question 8

In a bar chart, each column represents a group defined by a categorical variable.

Question 9

barh() function is used to create horizontal bar chart.

Question 10

The area on which actual plot will appear is defined by axes.

Question 11

Bins describe the number of data points that fall within a specified range of values.

Question 12

To change the orientation of the histogram, we can use orientation argument with hist().

Question 13

Legends are used to explain different sets of data plotted in different colors or marks in the chart.

State True or False

Question 1

The Matplotlib is a Python interface.

Answer

False

Reason — Matplotlib is an open-source 2D plotting library for the Python programming language that helps in visualizing figures.

Question 2

To save the plot, we have to use save graph() function.

Answer

False

Reason — To save the plot, we have to use savefig() function.

Question 3

Plot can be saved in a pdf format.

Answer

True

Reason — Matplotlib supports saving plots in various formats, including PDF. We can use the savefig() function and specify the file extension as .pdf to save the plot in PDF format. For example, to save the bar_plot as a PDF file, we use the following statement : plt.savefig("bar_plot.pdf").

Question 4

We can specify different colors for different bars of a bar chart.

Answer

True

Reason — In Matplotlib, we can specify different colors for different bars of a bar chart by passing a list of colors to the bar() function. The syntax is plt.bar(x, y, color=[color1, color2,.....]).

Question 5

To use pyplot for data visualization, we have to import it by giving import command: import matplotlib.pyplot

Answer

True

Reason — To use pyplot for data visualization, we need to import it from the matplotlib library using the command import matplotlib.pyplot.

Question 6

Pyplot is a Python library.

Answer

False

Reason — Pyplot is a module of matplotlib library of Python containing collection of methods which allows a user to create 2D plots and graphs easily and interactively.

Question 7

To specify a common width for all bars in a bar graph, we have to use thick argument.

Answer

False

Reason — To specify a common width for all bars in a bar graph, we have to use the width argument.

Question 8

To add a title to the plot, we have to call function header().

Answer

False

Reason — The title() function is used to set a title for the plot.

Question 9

Markers are data points in the graphs.

Answer

True

Reason — The data points being plotted on a graph/chart are called markers.

Question 10

Line style argument of plot() function is not required in scatter chart.

Answer

True

Reason — When creating scatter charts using Matplotlib's plot() function, the linestyle argument is skipped because scatter plots do not use line styles.

Question 11

When we don't specify X or Y limits for a plot, then pyplot does not automatically decide limits as per values being plotted.

Answer

False

Reason — When we don't specify X or Y limits for a plot, pyplot automatically decides the limits based on the values being plotted. It sets the limits to the minimum and maximum values of the data, so that all the data points are visible in the plot.

Multiple Choice Questions

Question 1

Which Python package is used for 2D graphics?

  1. matplotlib.pyplot
  2. matplotlib.pip
  3. matplotlib.numpy
  4. matplotlib.plt

Answer

matplotlib.pyplot

Reason — A Python package matplotlib.pyplot is used for creating 2D graphics, including plots, charts, and graphs.

Question 2

The most popular data visualization library in Python is:

  1. pip
  2. matinfolib
  3. matplotlib
  4. matpiplib

Answer

matplotlib

Reason — Matplotlib is the most popular data visualization library in Python.

Question 3

Matplotlib allows you to create:

  1. table
  2. charts
  3. maps
  4. infographics

Answer

charts

Reason — Matplotlib is used for creating static, animated and interactive 2D-plots or figures in Python. It is a plotting library that provides a wide range of chart types, including line plots, scatter plots, bar charts, histograms etc.

Question 4

Which of the following is not a visualization under Matplotlib?

  1. Line plot
  2. Histogram
  3. Bar plot
  4. Table plot

Answer

Table plot

Reason — Matplotlib is a plotting library that provides various visualization tools, including line plots, scatter plots, histograms, and bar plots.

Question 5

Which of the following commands is used to install Matplotlib for coding?

  1. import plt.matplotlib as plot
  2. import plot.matplotlib as pt
  3. import matplotlib.plt as plot
  4. import matplotlib.pyplot as plt

Answer

import matplotlib.pyplot as plt

Reason — The correct command to import Matplotlib for coding is import matplotlib.pyplot as plt. This is the standard way to import Matplotlib, where matplotlib.pyplot is the module that provides the plotting functions, and as plt assigns the alias plt to the pyplot module.

Question 6

Which of the following methods should be employed in the code to display a plot()?

  1. show()
  2. display()
  3. execute()
  4. plot()

Answer

show()

Reason — In Matplotlib, the show() method is used to display a plot.

Question 7

Which of the following statements is used to create a histogram of 'step' type with 20 bins?

  1. plt.hist(x, bins = 20, histtype = "barstacked")
  2. plt.hist(x, bins=20)
  3. plt.hist(x, bins=20, histtype="step")
  4. plt.hist(x, bins=20, histtype=hist())

Answer

plt.hist(x, bins=20, histtype="step")

Reason — The histtype parameter in the hist() function is used to specify the type of histogram to be created. In this case, histtype="step" is used to create a step histogram. The bins=20 parameter specifies that the histogram should be divided into 20 bins. Hence, the correct statement is plt.hist(x, bins=20, histtype="step").

Question 8

The part of chart which identifies different sets of data plotted on plot by using different colors is called:

  1. legends
  2. title
  3. axes
  4. figure

Answer

legends

Reason — A legend is a part of a chart that identifies different sets of data plotted on the plot by using different colors, symbols.

Question 9

Which of the following is an incorrect example of savefig() function?

  1. plt.savefig("bar1.pdf" )
  2. plt.savefig("bar1.png")
  3. plt.savefig("bar1.eps")
  4. plt.savefig("bar1.ppt")

Answer

plt.savefig("bar1.ppt")

Reason — The savefig() function in matplotlib is used to save a figure to a file. It supports various file formats such as PDF, PNG, EPS, SVG, etc. However, PPT (PowerPoint) is not a supported file format for saving figures in matplotlib.

Question 10

Which of the following plots makes it easy to visualize a trend in data over intervals of time.

  1. Box plot
  2. Histogram
  3. Line Chart
  4. Bar chart

Answer

Line Chart

Reason — A line chart is a type of plot that displays data as a series of points connected by lines, making it easy to visualize trends in data over intervals of time.

Question 11

COVID-19 patient analysis in the Mumbai region is to be plotted. The command used to give title to X-axis as "No. of Patients" in the graph is:

  1. plt.show()
  2. plt.plot("No. of Patients")
  3. plt.xlabel("No. of Patients")
  4. plt.title("No. of Patients")

Answer

plt.xlabel("No. of Patients")

Reason — The plt.xlabel() function is used to set the label for the x-axis of a plot. In this case, the command plt.xlabel("No. of Patients") sets the x-axis label to "No. of Patients", which is suitable for a COVID-19 patient analysis in the Mumbai region.

Question 12

Python Matplotlib ............... is used to compare different categorical or discrete variables.

  1. line plot
  2. bar graph
  3. histogram
  4. boxplot

Answer

bar graph

Reason — A bar graph is a type of plot that uses rectangular bars with heights or lengths proportional to the values they represent to compare different categorical or discrete variables.

Assertions and Reasons

Question 1

Assertion (A): Data Visualization helps users in analyzing a large amount of data in a simple way.

Reasoning (R): Data Visualization makes complex data more accessible, understandable and usable.

  1. Both A and R are true and R is the correct explanation of A.
  2. Both A and R are true but R is not the correct explanation of A.
  3. A is true but R is false.
  4. A is false but R is true.

Answer

Both A and R are true and R is the correct explanation of A.

Explanation
Data visualization refers to the graphical or visual representation of information and data using visual elements like charts, graphs, and maps. These visual tools help in analyzing a large amount of data in a simple way. Because of this, data visualization makes complex data more accessible, understandable, and usable.

Question 2

Assertion (A): import matplotlib.pyplot as plt is used to import pyplot module.

Reasoning (R): Matplotlib is a Python library and pyplot is a module that contains functions to create various plots.

  1. Both A and R are true and R is the correct explanation of A.
  2. Both A and R are true but R is not the correct explanation of A.
  3. A is true but R is false.
  4. A is false but R is true.

Answer

Both A and R are true and R is the correct explanation of A.

Explanation
The command to import pyplot is import matplotlib.pyplot as plt. Matplotlib is a Python 2D plotting library for creating publication-quality figures. Pyplot is a module within the Matplotlib library that contains a collection of methods which allow users to create 2D plots and graphs easily and interactively.

Question 3

Assertion (A): Data Visualization refers to the graphical representation of information and data using visual elements like charts, graphs and maps, etc.

Reasoning (R): To install matplotlib library, we can use the command - pip install matplotlib.

  1. Both A and R are true and R is the correct explanation of A.
  2. Both A and R are true but R is not the correct explanation of A.
  3. A is true but R is false.
  4. A is false but R is true.

Answer

Both A and R are true but R is not the correct explanation of A.

Explanation
Data visualization refers to the graphical or visual representation of information and data using visual elements like charts, graphs, and maps. To install the Matplotlib library, we use the command pip install matplotlib.

Question 4

Assertion (A): A histogram is basically used to represent data provided in the form of groups spread in non-continuous ranges.

Reasoning (R): matplotlib.pyplot.hist() function is used to compute and create histogram of a variable.

  1. Both A and R are true and R is the correct explanation of A.
  2. Both A and R are true but R is not the correct explanation of A.
  3. A is true but R is false.
  4. A is false but R is true.

Answer

A is false but R is true.

Explanation
A histogram is used to represent data provided in the form of discrete or continuous ranges. The matplotlib.pyplot.hist() function is used to compute and create a histogram of a variable.

Question 5

Assertion (A): legend (labels = ['Text']) is used to give title to the graph.

Reasoning (R): plt.savefig("path") will save the current graph in png or jpeg format.

  1. Both A and R are true and R is the correct explanation of A.
  2. Both A and R are true but R is not the correct explanation of A.
  3. A is true but R is false.
  4. A is false but R is true.

Answer

A is false but R is true.

Explanation
The statement legend(labels=['Text']) is used to add a legend to the graph, not a title. The title of the graph is set using plt.title('Title Text'). The statement plt.savefig("path") saves the current graph to the specified path in PNG or JPEG format.

Question 6

Assertion (A): In histogram, X-axis is about bin ranges whereas Y-axis talks about frequency.

Reasoning (R): The bins (intervals) must be adjacent and are often (but are not required to be) of equal size.

  1. Both A and R are true and R is the correct explanation of A.
  2. Both A and R are true but R is not the correct explanation of A.
  3. A is true but R is false.
  4. A is false but R is true.

Answer

Both A and R are true and R is the correct explanation of A.

Explanation
A histogram's X-axis shows the bin ranges, which are intervals for the data, and the Y-axis shows the frequency of data points within each bin. These bins must be adjacent to each other and are often of equal size, although they don't have to be.

Question 7

Assertion (A): Bar graph and histogram are same.

Reasoning (R): A bar graph represents categorical data using rectangular bars. A histogram represents data which is grouped into continuous number ranges and each range corresponds to a vertical bar.

  1. Both A and R are true and R is the correct explanation of A.
  2. Both A and R are true but R is not the correct explanation of A.
  3. A is true but R is false.
  4. A is false but R is true.

Answer

A is false but R is true.

Explanation
Bar charts and histograms are not the same. A bar chart or bar graph is a chart that presents categorical data with rectangular bars, where the heights or lengths of the bars are proportional to the values they represent. On the other hand, a histogram is a type of graph that provides a visual interpretation of numerical data by indicating the number of data points that lie within a range of values, and this corresponds to a vertical bar.

Question 8

Assertion (A): Marker has different elements i.e., style, color, size, etc.

Reasoning (R): We can customize line of a line chart by using marker property of plot() function.

  1. Both A and R are true and R is the correct explanation of A.
  2. Both A and R are true but R is not the correct explanation of A.
  3. A is true but R is false.
  4. A is false but R is true.

Answer

A is true but R is false.

Explanation
A marker in a chart or graph can have various elements such as style (e.g., circle, square, triangle), color, size, and others. The marker property is used to customize the markers (points) in a chart, not the line itself. The line in a line chart can be customized using other properties such as linestyle, linewidth, color, etc.

Case/Source Based Questions

Question 1

Hindustan Departmental Stores sell items of daily use such as shampoo, soap and much more. They record the entire sale and purchase of goods month-wise so as to get a proper analysis of profit or loss in their business transactions.

Following is the csv file containing the "Company Sales Data".

Hindustan Departmental Stores sell items of daily use such as shampoo, soap and much more. They record the entire sale and purchase of goods month-wise so as to get a proper analysis of profit or loss in their business transactions. Data Visualization using Matplotlib, Informatics Practices Preeti Arora Solutions CBSE Class 12

Read the total profit of all months and show it using a line plot. Total profit data has been provided for each month. Generated line plot must include the following properties:

  • X label name = Month Number
  • Y label name = Total profit

Answer

import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("C:\\company_sales_data.csv")
profitList = df['total_profit'].tolist()
monthList = df['month_number'].tolist()
plt.plot(monthList, profitList, label = 'Month-wise Profit data of last year')
plt.xlabel('Month number')
plt.ylabel("Profit in dollars")
plt.xticks(monthList)
plt.title('Company profit per month')
plt.yticks([100000, 200000, 300000, 400000, 500000])
plt.show()
Output
Hindustan Departmental Stores sell items of daily use such as shampoo, soap and much more. They record the entire sale and purchase of goods month-wise so as to get a proper analysis of profit or loss in their business transactions. Data Visualization using Matplotlib, Informatics Practices Preeti Arora Solutions CBSE Class 12

Question 2

Anirudh is trying to write a code to plot line graph shown in the figure below. Help him fill in the blanks in the code and get the desired output.

Anirudh is trying to write a code to plot line graph shown in the figure below. Help him fill in the blanks in the code and get the desired output. Data Visualization using Matplotlib, Informatics Practices Preeti Arora Solutions CBSE Class 12
import matplotlib.pyplot as plt             # statement 1
x = [1, 2, 3]                               # statement 2
y = [2, 4, 1]                               # Statement 3
plt.plot(x, y, color = 'g')                 # statement 4
...............                             # statement 5
...............                             # statement 6
# giving a title to graph
plt. ............... ('My first graph!')    # statement 7
# Function to show the plot 
...............                             # statement 8

(i) Which of the above statements is responsible for plotting the values on canvas?

  1. Statement 8
  2. Statement 4
  3. Statement 1
  4. None of these

(ii) Statements 5 & 6 are used to give names to X-axis and Y-axis as shown in Fig.1. Which of the following can fill those two gaps?

1.

plt.xlabel('X - axis') 
plt.ylabel('Y - axis')

2.

plt.xtitle('x - axis')
plt.ytitle('y - axis')

3.

plt.xlable('x - axis')
pit.ylable('x - axis') 

4.

plt.xlabel('x axis')
plt.ylabel('y axis')

(iii) Raman has executed code with first 7 statements but no output is displayed. Which of the following statements will display the graph?

  1. plt.display()
  2. plt.show()
  3. matplotlib.pyplot.display()
  4. Both (b) & (c)

(iv) The number of markers in the above line chart are:

  1. Zero
  2. Three
  3. Infinite
  4. Not defined

(v) Which of the following methods will result in displaying 'My first graph!' in the above graph?

  1. legend()
  2. label()
  3. title()
  4. Both (a) & (c)

Answer

(i) Statement 4

Reason — The plt.plot() statement is used to plot y versus x data on the canvas in Matplotlib.

(ii)

plt.xlabel('X - axis') 
plt.ylabel('Y - axis')

Reason — The xlabel() and ylabel() functions are used to give labels to x-axis and y-axis respectively.

(iii) plt.show()

Reason — The plt.show() statement is used to display the graph.

(iv) Three

Reason — There are three markers in the line chart, corresponding to the three data points (1, 2), (2, 4), and (3, 1).

(v) title()

Reason — The plt.title() method is used to set the title of the graph, which in this case is "My first graph!".

Solutions to Unsolved Questions

Question 1

Plot a line chart for depicting the population for the last 5 years as per the specifications given below:

  • plt.title("My Title") will add a title "My Title" to your plot.

  • plt.xlabel("Year") will add a label "Year" to your X-axis.

  • plt.ylabel("Population") will add a label "Population" to your Y-axis.

  • plt.yticks([1, 2, 3, 4, 5]) set the numbers on the Y-axis to be 1, 2, 3, 4, 5. Pass it and label as a second argument. For example, if we use this code plt.yticks([1, 2, 3, 4, 5], ["1M", "2M", "3M", "4M", "5M"]), it will set the labels 1M, 2M, 3M, 4M, 5M on the Y-axis.

  • plt.xticks() — works the same as plt.yticks(), but for the X-axis.

Answer

import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4, 5], ['1M', '2M', '3M', '4M', '5M'])
plt.title("My Title")
plt.xlabel("Year")
plt.ylabel("Population")
plt.xticks([1, 2, 3, 4, 5], ["2019", "2020", "2021", "2022", "2023"])
plt.yticks([1, 2, 3, 4, 5], ['1M', '2M', '3M', '4M', '5M'])
plt.show()
Output
Plot a line chart for depicting the population for the last 5 years as per the specifications given below: Data Visualization using Matplotlib, Informatics Practices Preeti Arora Solutions CBSE Class 12

Question 2

What is Matplotlib?

Answer

Matplotlib is an open source 2D plotting Python library for creating static, animated and interactive figures.

Question 3

What do you mean by pyplot?

Answer

Pyplot is a module of matplotlib library of Python containing collection of methods which allows a user to create 2D plots and graphs easily and interactively.

Question 4

How many types of graphs are plotted using pyplot?

Answer

The types of graphs that can be plotted using the pyplot module are as follows:

  1. Line plots
  2. Scatter plots
  3. Bar charts
  4. Histograms
  5. Pie charts
  6. Box plots

Question 5

Which function is used to show the graph?

Answer

The show() function is used to show the graph.

Question 6

Differentiate between figure and axes.

Answer

FigureAxes
Figure is the outermost area of Matplotlib graph.Axes is the individual plot within the figure.
It contains one or more than one axes.It contains two or three axis objects.
It contains plots, legend, axis label, ticks, title etc.It contains title, an x-label and a y-label.
It provides a canvas for the plot.It displays the data in a specific format.

Question 7

What is the use of subplot() function? Write its parameters.

Answer

The subplot() function is used to display multiple charts in the same window.

The syntax of the subplot() function is: subplot(nrows, ncols, index).

The parameters are:

  1. nrows: The number of rows in the grid of subplots.
  2. ncols: The number of columns in the grid of subplots.
  3. index: The index of the subplot to create, starting from 1 and increasing from left to right, top to bottom.

Question 8

Write a Python program to draw a line with a suitable label in the X-axis and Y-axis, and a title.

Answer

import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4, 5], [2, 4, 6, 8, 10])
plt.title("Line Graph")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
Output
Write a Python program to draw a line with a suitable label in the X-axis and Y-axis, and a title. Data Visualization using Matplotlib, Informatics Practices Preeti Arora Solutions CBSE Class 12

Question 9

Write a Python program to plot two or more lines with legends, different widths and colors.

Answer

import matplotlib.pyplot as plt
x1 = [10,20,30]
y1 = [20,40,10]
plt.plot(x1, y1, color='blue', linewidth=3, label='line1')
x2 = [10,20,30]
y2 = [40,10,30]
plt.plot(x2, y2, color='red', linewidth=4, label='line2')
plt.legend()
plt.show()
Output
Write a Python program to plot two or more lines with legends, different widths and colors. Data Visualization using Matplotlib, Informatics Practices Preeti Arora Solutions CBSE Class 12

Question 10

Write a Python program to plot two or more lines and set the line markers.

Answer

import matplotlib.pyplot as plt
x1 = [10,20,30]
y1 = [20,40,10]
plt.plot(x1, y1, marker='o', label='line1-circle')
x2 = [10,20,30]
y2 = [40,10,30]
plt.plot(x2, y2, marker='s', label='line2-square')
plt.show()
Output
Write a Python program to plot two or more lines and set the line markers. Data Visualization using Matplotlib, Informatics Practices Preeti Arora Solutions CBSE Class 12

Question 11

Write a Python program to display a bar chart of the number of students in a class. Use different colors for each bar.

Sample data:

Class: I, II, III, IV, V, VI, VII, VIII, IX, X
Strengths: 40, 43, 45, 47, 49, 38, 50, 37, 43, 39

Answer

import matplotlib.pyplot as plt
classes = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X']
strengths = [40, 43, 45, 47, 49, 38, 50, 37, 43, 39]
plt.bar(classes, strengths, color=['red', 'green', 'blue', 'yellow', 'orange', 'purple', 'pink', 'brown', 'gray', 'black'])
plt.title('Number of Students in Each Class')
plt.xlabel('Class')
plt.ylabel('Number of Students')
plt.show()
Output
Write a Python program to display a bar chart of the number of students in a class. Use different colors for each bar. Data Visualization using Matplotlib, Informatics Practices Preeti Arora Solutions CBSE Class 12

Question 12

Write a Python program to display a horizontal bar chart of the number of students in a class.

Sample data:

Class: I, II, III, IV, V, VI, VII, VIII, IX, X
Strengths: 40, 43, 45, 47, 49, 38, 50, 37, 43, 39

Answer

import matplotlib.pyplot as plt
classes = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X']
strengths = [40, 43, 45, 47, 49, 38, 50, 37, 43, 39]
plt.barh(classes, strengths)
plt.title('Number of Students in Each Class')
plt.xlabel('Number of Students')
plt.ylabel('Class')
plt.show()
Output
Write a Python program to display a horizontal bar chart of the number of students in a class. Data Visualization using Matplotlib, Informatics Practices Preeti Arora Solutions CBSE Class 12

Question 13

Plot a line graph for: y2 = 4*x

Answer

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(1, 5)
y = np.sqrt(4 * x)
plt.plot(x, y)
plt.title('Line Graph of y² = 4x')
plt.xlabel('x')
plt.ylabel('y')
plt.show()
Output
Plot a line graph for: y^2 = 4\*x. Data Visualization using Matplotlib, Informatics Practices Preeti Arora Solutions CBSE Class 12

Question 14

Write a Python program to plot the function y = x2 using the Matplotlib library.

Answer

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(1, 5)
y = x ** 2
plt.plot(x, y)

plt.title('Line Graph of y = x²')
plt.xlabel('x')
plt.ylabel('y')

plt.show()
Output
Write a Python program to plot the function y = x^2 using the Matplotlib library. Data Visualization using Matplotlib, Informatics Practices Preeti Arora Solutions CBSE Class 12

Question 15

Name the various methods used with pyplot object.

Answer

The various methods used with 'pyplot' object are as follows:

  1. Plotting methods — plot(), scatter(), bar(), hist() and pie().
  2. Labeling and titling methods — xlabel(), ylabel() and title().
  3. Legend methods — legend().
  4. axis methods — axis(), xlim(), ylim(), xticks() and yticks().
  5. grid methods — grid().
  6. figure and axes methods — figure(), subplots().
  7. display and save methods — show(), savefig() and close().

Question 16

Write the specific purpose of the following functions used in plotting:

(a) show()

(b) legend()

Answer

(a) show() — The purpose of the show() function is to display the plot.

(b) legend() — The purpose of the legend() function is to add a legend to the plot. In a chart/graph, there may be multiple datasets plotted. To distinguish among various datasets plotted in the same chart, legends are used. Legends can be different colors/patterns assigned to different specific datasets. The legends are shown in a corner of a chart/graph.

Question 17

Plot a histogram of a class test of 40 students based on random sets of marks obtained by the students (MM=100).

Answer

import matplotlib.pyplot as plt
import numpy as np
marks = np.random.randint(0, 101, 40)
plt.hist(marks)
plt.title('Class Test Marks')
plt.xlabel('Marks')
plt.ylabel('Frequency')
plt.show()
Output
Plot a histogram of a class test of 40 students based on random sets of marks obtained by the students (MM=100). Data Visualization using Matplotlib, Informatics Practices Preeti Arora Solutions CBSE Class 12

Question 18

A list, namely temp contains average temperature for seven days of last week. You want to see how the temperature changes in the last seven days. Which chart type will you plot for the same and why?

Answer

A line chart is the suitable choice for visualizing how the temperature changed over the last seven days. The line chart shows trends over time and displays continuous data, making it ideal for representing temperature values. The chart's ability to connect data points allows viewers to easily observe temperature trends and understand variations across the seven-day period.

Question 19

Collect data about colleges in Delhi University or any other university of your choice and number of courses they run for Science, Commerce and Humanities, store it in a CSV file and present it using a bar plot.

Answer

import pandas as pd
import matplotlib.pyplot as plt

data = {"Stream": ["Science", "Commerce", "Humanities"], 
        "Number of Courses": [12, 10, 15] 
}

df = pd.DataFrame(data)
df.to_csv('du_colleges.csv', index=False)

df = pd.read_csv("du_colleges.csv")
plt.bar(df["Stream"], df["Number of Courses"])
plt.xlabel("Stream")
plt.ylabel("Number of Courses")
plt.title("Number of Courses in Each Stream")
plt.show()
Output
Collect data about colleges in Delhi University or any other university of your choice and number of courses they run for Science, Commerce and Humanities, store it in a CSV file and present it using a bar plot. Data Visualization using Matplotlib, Informatics Practices Preeti Arora Solutions CBSE Class 12

Question 20

What is a histogram? How do you create histograms in Python?

Answer

A histogram is a summarization tool for discrete or continuous data, providing a visual interpretation of numerical data by showing the number of data points that fall within a specified range of values.

The hist() function of the Pyplot module is used to create and plot a histogram from a given sequence of numbers. The syntax for using the hist() function in Pyplot is as follows:

matplotlib.pyplot.hist(x, bins = None, cumulative = False, histtype = 'bar', align = 'mid', orientation = 'vertical', ).

Question 21

What are the various types of histograms that can be created through hist() function?

Answer

The hist() function in Matplotlib's Pyplot module allows creating various types of histograms. These include the default bar histogram (histtype='bar'), step histogram (histtype='step'), stepfilled histogram (histtype='stepfilled'), barstacked histogram (histtype='barstacked').

Question 22

When should you create histograms and when should you create bar charts to present data visually?

Answer

Histograms are great for displaying specific ranges of values and are ideal for visualizing the results of continuous data, such as the ages of students in a class. Bar charts, on the other hand, are effective for comparing categorical or discrete data across different categories or groups, such as comparing the sales performance of different products.

Question 23(i)

Given the following set of data:

Weight measurements for 14 values of muffins (in grams)

78, 72, 69, 81, 63, 67, 65
79, 74, 71, 83, 71, 79, 80

Create a simple histogram from the above data.

Answer

import matplotlib.pyplot as plt
weights = [78, 72, 69, 81, 63, 67, 65, 79, 74, 71, 83, 71, 79, 80]
plt.hist(weights)
plt.title('Weight Distribution of muffins')
plt.show()
Output
Given the following set of data: Weight measurements for 14 values of muffins (in grams). Data Visualization using Matplotlib, Informatics Practices Preeti Arora Solutions CBSE Class 12

Question 23(ii)

Given the following set of data:

Weight measurements for 14 values of muffins (in grams)

78, 72, 69, 81, 63, 67, 65
79, 74, 71, 83, 71, 79, 80

Create a horizontal histogram from the above data.

Answer

import matplotlib.pyplot as plt
weights = [78, 72, 69, 81, 63, 67, 65, 79, 74, 71, 83, 71, 79, 80]
plt.hist(weights, orientation = 'horizontal')
plt.title('Weight Distribution of muffins')
plt.show()
Output
Given the following set of data: Weight measurements for 14 values of muffins (in grams). Data Visualization using Matplotlib, Informatics Practices Preeti Arora Solutions CBSE Class 12

Question 23(iii)

Given the following set of data:

Weight measurements for 14 values of muffins (in grams)

78, 72, 69, 81, 63, 67, 65
79, 74, 71, 83, 71, 79, 80

Create a step type of histogram from the above data.

Answer

import matplotlib.pyplot as plt
weights = [78, 72, 69, 81, 63, 67, 65, 79, 74, 71, 83, 71, 79, 80]
plt.hist(weights, histtype = 'step')
plt.title('Weight Distribution of muffins')
plt.show()
Output
Given the following set of data: Weight measurements for 14 values of muffins (in grams). Data Visualization using Matplotlib, Informatics Practices Preeti Arora Solutions CBSE Class 12

Question 23(iv)

Given the following set of data:

Weight measurements for 14 values of muffins (in grams)

78, 72, 69, 81, 63, 67, 65
79, 74, 71, 83, 71, 79, 80

Create a cumulative histogram from the above data.

Answer

import matplotlib.pyplot as plt
weights = [78, 72, 69, 81, 63, 67, 65, 79, 74, 71, 83, 71, 79, 80]
plt.hist(weights, cumulative = True)
plt.title('Weight Distribution of muffins')
plt.show()
Output
Given the following set of data: Weight measurements for 14 values of muffins (in grams). Data Visualization using Matplotlib, Informatics Practices Preeti Arora Solutions CBSE Class 12

Question 24

Kritika was asked to write the names of a few libraries in Python used for data analysis and one method of each. Help her write at least 3 libraries and their methods.

Answer

The three libraries in Python used for data analysis are as follows:

  1. Pandas library — array() function
  2. NumPy library — DataFrame() function
  3. Matplotlib library — plot() function
PrevNext