0% found this document useful (0 votes)
2 views69 pages

Data Visualization

The document provides a comprehensive overview of data visualization, specifically using the Matplotlib library in Python. It covers essential concepts such as the definition of data visualization, key functions for creating various types of plots (like bar charts, histograms, and box plots), and the specific arguments used to customize these plots. Additionally, it includes multiple-choice questions, fill-in-the-blanks, and true/false questions to reinforce understanding of the material.

Uploaded by

Sanjay Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views69 pages

Data Visualization

The document provides a comprehensive overview of data visualization, specifically using the Matplotlib library in Python. It covers essential concepts such as the definition of data visualization, key functions for creating various types of plots (like bar charts, histograms, and box plots), and the specific arguments used to customize these plots. Additionally, it includes multiple-choice questions, fill-in-the-blanks, and true/false questions to reinforce understanding of the material.

Uploaded by

Sanjay Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Data Visualization

Question 1

What is data visualization ?

Answer

Data visualization basically refers to the graphical or visual representation of information and
data using visual elements like charts, graphs, maps and so forth.

Question 2

Name the Python library generally used for data visualization.

Answer

The Python library generally used for data visualization is Matplotlib library.

Question 3

Is Pyplot a Python library ? What is it ?

Answer

Yes, Pyplot is a Python library. Pyplot is a collection of methods within matplotlib library which
allows user to construct 2D plots easily and interactively.

Question 4

Name the function you will use to create a horizontal bar chart.

Answer

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

Question 5

Which argument will you provide to change the following in a line chart ?

(i) width of the line

(ii) color of the line

Answer
(i) For changing the width of the line, we use the linewidth argument with the plot() function
as: <[Link]>.plot(<data1>, [,data2], linewidth = <width> )
(ii) For changing the color of the line, we use the color argument with the plot() function
as: <[Link]>.plot(<data1>, [,data2], <color code>)

Question 6

What is a marker ? How can you change the marker type and color in a plot ?

Answer

The data points being plotted on a graph/chart are called markers. To change the marker type and
color, we use following additional optional arguments in plot function : marker = <valid
marker type>, markeredgecolor = <valid color>.

Question 7

Using which function of Pyplot can you plot histograms ?

Answer

With Pyplot, a histogram is created using hist() function.

Question 8

Are bar charts and histograms the same ?

Answer

No, bar charts and histograms are not same. A bar chart or bar graph is a chart or graph that
presents categorical data with rectangular bars with heights or lengths proportional to the values
that 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.

Question 9

Name various types of histogram plots that you can create using Pyplot.

Answer

The types of histogram plots that we can create using Pyplot are cumulative histogram, step type
histogram, stacked bar type histogram, horizontal histogram.

Question 10

What is a frequency polygon ?


Answer

A frequency polygon is a type of frequency distribution graph. In a frequency polygon, the


number of observations is marked with a single point at the midpoint of an interval.

Question 11

What is the use of box plot ?

Answer

The box plot is used to show the range and the middle half of the ranked data.

Question 12

Using which function of Pyplot, can you create box plots ?

Answer

Pyplot module's boxplot() function allows to create box plots.


Multiple Choice Questions

Question 1

Which library is imported to draw charts in Python ?

1. csv
2. matplotlib
3. numpy
4. pandas

Answer

matplotlib

Reason — The library imported to draw charts in Python is Matplotlib.

Question 2

PyPlot is an interface of Python's ............... library.

1. seaborn
2. plotly
3. ggplot
4. matplotlib
Answer

matplotlib

Reason — PyPlot is an interface of Python's Matplotlib library that allows users to construct 2D
plots easily and interactively.

Question 3

For 2D plotting using a Python library, which library interface is often used ?

1. seaborn
2. plotly
3. matplotlib
4. [Link]

Answer

[Link]

Reason — The [Link] interface is commonly used for 2D plotting in Python using
the Matplotlib library.

Question 4

Which of the following is not a valid chart type ?

1. histogram
2. statistical
3. pie
4. box

Answer

statistical, box

Reason — Pie charts and histograms are valid chart types used for data visualization. Statistical
and box plots are not valid chart types.

Question 5

Which of the following is not a valid plotting function of Pyplot ?

1. plot()
2. bar()
3. line()
4. pie()

Answer

line()

Reason — The line() function is not a valid plotting function of Pyplot. In Matplotlib's Pyplot
module, the correct function for creating line plots is plot().

Question 6

Which of the following plotting functions does not plot multiple data series ?

1. plot()
2. bar()
3. pie()
4. barh()

Answer

pie()

Reason — The pie() function in Matplotlib's Pyplot module can plot only one data sequence.
On the other hand, functions like plot(), bar(), and barh() can plot multiple data series in a
single chart.

Question 7

The plot which tells the trend between two graphed variables is the ............... graph/chart.

1. line
2. scatter
3. bar
4. pie

Answer

line

Reason — A line chart or line graph is a type of chart which displays information as a series of
data points called "markers" connected by straight line segments.

Question 8

The plot which tells the correlation between two variables which may not be directly related
is ............... graph/chart.
1. line
2. scatter
3. bar
4. pie

Answer

scatter

Reason — A scatter chart is a graph of plotted points on two axes that show the relationship
between two sets of data.

Question 9

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

1. quartile
2. histogram
3. mean
4. median

Answer

histogram

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


provides a visual interpretation of numerical data by indicating the number of data points that lie
within a range of values.

Question 10

A visual representation of the statistical five number summary of a given dataset is known
as ............... .

1. histogram
2. frequency distribution
3. box plot
4. frequency polygon

Answer

box plot

Reason — A box plot provides a visual representation of the statistical five-number summary of
a given dataset. It includes the highest and lowest numbers, the median, and the upper and lower
quartiles.
Question 11

Which of the following functions is used to create a line chart ?

1. line()
2. plot()
3. chart()
4. plotline()

Answer

plot()

Reason — The plot() function is used to create a line chart or line graph.

Question 12

Which of the following function will produce a bar chart ?

1. plot()
2. bar()
3. plotbar()
4. barh()

Answer

bar(), barh()

Reason — With Pyplot, a bar chart is created using bar() and barh() functions.

Question 13

Which of the following function will create a vertical bar chart ?

1. plot()
2. bar()
3. plotbar()
4. barh()

Answer

bar()

Reason — The bar() function will create a vertical bar chart.


Question 14

Which of the following function will create a horizontal bar chart ?

1. plot()
2. bar()
3. plotbar()
4. barh()

Answer

barh()

Reason — The barh() function will create a horizontal bar chart.

Question 15

To specify the style of line as dashed, which argument of plot() needs to be set ?

1. line
2. width
3. style
4. linestyle

Answer

linestyle

Reason — To specify the style of the line as dashed in Matplotlib's plot() function, we need to
set the linestyle argument to 'dashed'.

Question 16

The data points plotted on a graph are called ............... .

1. points
2. pointers
3. marks
4. markers

Answer

markers

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

A ............... graph is a type of chart which displays information as a series of data points
connected by straight line segments.

1. line
2. bar
3. pie
4. box plot

Answer

line

Reason — A line chart or line graph is a type of chart which displays information as a series of
data points called "markers" connected by straight line segments.

Question 18

To create scatter charts using plot(), which argument is skipped ?

1. marker
2. linestyle
3. markeredgecolor
4. linewidth

Answer

linestyle

Reason — When creating scatter charts using Matplotlib's plot() function,


the linestyle argument is skipped because scatter plots do not use line styles.

Question 19

In scatter(), which argument is used to specify the size of data points ?

1. size
2. s
3. marker
4. markersize

Answer

s
Reason — The s argument in scatter() is used to specify the size of data points.

Question 20

Which argument of bar() lets you set the thickness of bar ?

1. thick
2. thickness
3. width
4. barwidth

Answer

width

Reason — The width argument allows to control the thickness of the bars in a bar chart created
using the bar() function in Matplotlib.

Question 21

To change the width of bars in a bar chart, which of the following arguments with a float value is
used ?

1. hwidth
2. width
3. breath
4. barwidth

Answer

width

Reason — The width argument with a float value is used to change the width of bars in a bar
chart created using the bar() function in Matplotlib.

Question 22

Which function lets you set the title of the plot ?

1. title()
2. plottitle()
3. graphtitle()
4. all of these

Answer
title()

Reason — The title() function sets the title of the plot in Matplotlib.

Question 23

The command used to give a heading to a graph is ............... .

1. [Link]()
2. [Link]()
3. [Link]()
4. [Link]()

Answer

[Link]()

Reason — The [Link]() command is used in Matplotlib's Pyplot module to give a heading
or title to a graph.

Question 24

Which function would you use to set the limits for x-axis of the plot ?

1. limits()
2. xlimits()
3. xlim()
4. lim()

Answer

xlim()

Reason — The xlim() function is used to set the limits for the x-axis of a plot in Matplotlib.

Question 25

Which function is used to show legends ?

1. display()
2. show()
3. legend()
4. legends()

Answer
legend()

Reason — The legend() function in Matplotlib's Pyplot module is used to show legends in a
plot.

Question 26

Which argument must be set with plotting functions for legend() to display the legends ?

1. data
2. label
3. name
4. sequence

Answer

label

Reason — The label argument must be set with plotting functions for the legend() function to
display the legends correctly.

Question 27

Which function is used to create a histogram ?

1. histo()
2. histogram()
3. hist()
4. histtype

Answer

hist()

Reason — The hist() function is used to create a histogram.

Question 28

Which argument in hist() is used to create a stacked bar type histogram ?

1. histt
2. histtype
3. type
4. barstacked
Answer

histtype

Reason — The histtype argument in Matplotlib's hist() function is used to create a stacked
bar type histogram. Setting histtype = 'barstacked' creates a histogram where bars for each
bin are stacked on top of each other, representing different categories or subgroups within the
data.

Question 29

Which of the following functions can plot only one data series ?

1. plot()
2. bar()
3. boxplot()
4. pie()

Answer

pie()

Reason — The pie() function in Matplotlib's Pyplot module can plot only one data series. On
the other hand, functions like plot(), bar(), and boxplot() can plot multiple data series in a
single chart.

Question 30

Which argument must be provided to create wedges out of a pie chart ?

1. label
2. autopct
3. explode
4. wedge

Answer

explode

Reason — The explode argument is used in pie charts to visually separate one or more wedges
from the rest of the pie chart.

Question 31

Which argument should be set to display percentage share of each pie on a pie chart ?
1. label
2. autopct
3. explode
4. wedge

Answer

autopct

Reason — To view the percentage of share in a pie chart, we need to add an


argument autopct with a format string, such as "%1.1F%%".

Question 32

Which function creates a box plot ?

1. box()
2. plot()
3. boxplot()
4. showbox()

Answer

boxplot()

Reason — The boxplot() function is used to create box plots in Matplotlib.

Question 33

Which argument of boxplot() is used to create a filled boxplot ?

1. fill
2. box
3. patch_artist
4. patch

Answer

patch_artist

Reason — The patch_artist argument in the boxplot() function is used to create a filled box
plot. When set to True, it fills the boxes of the box plot with a color, making them more visually
distinct.
Fill in the Blanks
Question 1

A histogram is a plot that shows the underlying frequency distribution of a set of continuous
data.

Question 2

Pyplot interface is a collection of methods within matplotlib library of Python.

Question 3

Pyplot's plot() function is used to create line charts.

Question 4

Pyplot's barh() function is used to create horizontal bar charts.

Question 5

Pyplot's scatter() function is used to create scatter charts.

Question 6

Pyplot's hist() function is used to create histogram.

Question 7

The datapoints plotted on a graph are called markers .

Question 8

The linewidth argument of plot() specifies the width for the line.

Question 9

The linestyle argument of plot() specifies the style of the line.

Question 10

The width argument of bar() specifies the bar width.

Question 11

The xticks() function is used to specify ticks for x-axis.


Question 12

To save a plot, savefig() function is used.

Question 13

The orientation argument of hist() is set to create a horizontal histogram.

Question 14

The showmeans argument shows the arithmetic mean on a boxplot.

Question 15

The notch argument in a boxplot() creates a notched boxplot.

Question 16

The loc argument of legend() provides the location of legend.

Question 17

Using Python Matplotlib histogram can be used to count how many values fall into each
interval. (line plot / bar graph / histogram)

True/False Questions

Question 1

PyPlot is a sub-library of matplotlib library.

Answer

True

Reason — PyPlot is a sub-library of Matplotlib library. It allows users to construct 2D plots


easily and interactively. Pyplot essentially reproduces plotting functions and behavior of
MATLAB.

Question 2

Statement import [Link] is a valid statement for working on pyplot functions.

Answer
False

Reason — The statement import [Link] is the valid and commonly used way to
import the PyPlot submodule from the Matplotlib library.

Question 3

By default, pie chart is printed in elliptical or oval shape.

Answer

True

Reason — By default, a pie chart is printed in an elliptical or oval shape.

Question 4

The default shape of pie chart cannot be changed from oval.

Answer

False

Reason — By default, a pie chart is printed in an elliptical or oval shape. It can be changed to a
circle by using the axis() function of Pyplot and passing the 'equal' argument to it.

Question 5

A line chart can be plotted using pyplot library's line() function.

Answer

False

Reason — In Matplotlib's Pyplot library, the function used to plot a line chart is plot().

Question 6

A line chart can be plotted using pyplot library's plot() function.

Answer

True

Reason — In Matplotlib's Pyplot library, the function used to plot a line chart is plot().

Question 7
A bar chart can be plotted using pyplot library's bar() function.

Answer

True

Reason — In Matplotlib's Pyplot library, the function used to plot a bar chart is bar().

Question 8

A bar chart can be plotted using pyplot library's barh() function.

Answer

True

Reason — The barh() function in Matplotlib's Pyplot library is used to create horizontal bar
charts.

Question 9

It is not possible to plot multiple series of values in the same bar graph.

Answer

False

Reason — It is possible to plot multiple series of values in the same bar graph using Matplotlib's
Pyplot library because the bar() and barh() functions support handling multiple datasets.

Question 10

A standard marker of representing a non-number data in Python libraries is NaN.

Answer

True

Reason — A standard marker for representing missing or non-number data in Python libraries is
NaN (Not a Number).

Question 11

If the linestyle argument is missing along with markerstyle-string in a plot(), a scatter type chart
get created.

Answer
True

Reason — When both the linestyle argument and the marker argument (markerstyle-string)
are not specified in the plot() function, the resulting chart can resemble a scatter plot. In this
case, the points will be plotted without connected lines, similar to how a scatter plot displays data
points.

Question 12

The bar() function can also create horizontal bar charts.

Answer

False

Reason — The bar() function in Matplotlib's Pyplot library can create vertical bar charts, while
the barh() function creates horizontal bar charts.

Question 13

The pie() function can plot multiple data series.

Answer

False

Reason — The pie() function in Matplotlib's Pyplot module can plot only one data series.

Question 14

The plot is always as per the data series being plotted irrespective of the xlim().

Answer

False

Reason — The plot appearance can be affected by the data series being plotted, but it can also be
influenced by functions such as xlim() which determine the range of values shown on the x-
axis.

Question 15

Frequency polygon is created from histogram.

Answer

True
Reason — When a frequency polygon is drawn manually, it is based on the data that would be
used to create a histogram.

Question 16

What is not true about Data Visualization ?

(a) Graphical representation of information and data.

(b) Helps users in analyzing a large amount of data in a simpler way.

(c) Data Visualization makes complex data more accessible, understandable, and usable.

(d) No library needs to be imported to create charts in Python language.

Answer

No library needs to be imported to create charts in Python language.

Reason — To create charts and visualizations in Python, we need to import libraries such as
Matplotlib.

Assertions and Reasons

Question 1

Assertion. The matplotlib library of Python is used for data visualization.

Reason. The PyPlot interface of matplotlib library is used for 2D plotting.

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 Matplotlib library is used for data visualization in Python, providing a variety of tools and
functionalities for creating different types of plots, charts, and graphs. The Pyplot module, which
is a collection of methods within the Matplotlib library, allows users to construct 2D plots easily
and interactively.

Question 2
Assertion. A scatter chart simply plots the data points on a chart to show the trend in the data.

Reason. A line chart connects the plotted data points with a line.

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
The scatter chart is a graph of plotted points on two axes that show the relationship between two
sets of data. On the other hand, a line chart, or line graph, is a type of chart that displays
information as a series of data points called 'markers' connected by straight line segments.

Question 3

Assertion. Both scatter() and plot() functions of PyPlot can create scatter charts.

Reason. The plot() function can create line charts as well as scatter charts.

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
Both the scatter() and plot() functions in Pyplot can create scatter charts.
The plot() function in Pyplot can create both line charts and scatter charts. When specifying
marker styles without providing a linestyle argument, the plot() function will create a scatter
chart.

Question 4

Assertion. For the same sets of data, you can create various charts using plot(), scatter(), pie(),
bar() and barh().

Reason. All the data sets of a plot(), scatter(), bar() cannot be used by pie() ; it will work with
only a single set of data.
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
We can create various charts using plot(), scatter(), bar(), and barh() for the same
datasets, but not using pie(). The pie() function specifically works with a single set of data,
whereas the other functions can handle multiple datasets or series.

Question 5

Assertion. Five-point statistical summary of a data set can be visually represented.

Reason. The boxplot() function can plot the highest and lowest numbers of a data range, its
median along with the upper and lower quartiles.

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 five-point statistical summary of a dataset can be visually represented through a box plot. A
box plot is used to display the range and middle half of ranked data. It uses five important
numbers from the data range: the extremes (highest and lowest numbers), the median, and the
upper and lower quartiles, comprising the five-number statistical summary.

Question 6

Assertion. Line graph is a tool for comparison and is created by plotting a series of several
points and connecting them with a straight line.

Reason. You should never use a line chart when the chart is in a continuous data set.

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 line graph is a tool for comparison, created by plotting a series of data points called 'markers'
and connecting them with straight lines. This makes it easier to compare different data points and
observe patterns. Line charts are suitable for continuous data sets, displaying information as a
series of data and not restricted to discontinuous data sets.

Type A: Short Answer Questions/Conceptual Questions

Question 1

Name the library of which the PyPlot is an interface.

Answer

PyPlot is an interface provided by the Matplotlib library.

Question 2

Write the statement to import PyPlot in your script.

Answer

The statement to import PyPlot in our script is as follows:

import [Link] as plt

Question 3

Name the functions to create the following :

(a) line chart

(b) bar chart

(c) horizontal bar chart

(d) histogram

(e) scatter chart

(j) boxplot
(g) pie chart

Answer

(a) line chart: plot() function


(b) bar chart: bar() function
(c) horizontal bar chart: barh() function
(d) histogram: hist() function
(e) scatter chart: scatter() function
(j) boxplot: boxplot() function
(g) pie chart: pie() function

Question 4

What is a line chart ?

Answer

A line chart, or line graph, is a type of chart that displays information as a series of data points
called 'markers' connected by straight line segments.

Question 5

What is a scatter chart ? How is it different from line chart ?

Answer

The scatter chart is a graph of plotted points on two axes that show the relationship between two
sets of data. With a scatter plot, a mark or marker (usually a dot or small circle), represents a
single data point. With one mark (point) for every data point a visual distribution of the data can
be seen. Depending on how tightly the points cluster together, we may be able to discern a clear
trend in the data.

The difference is that with a scatter plot, the decision is made from the data points such that the
individual points should not be connected directly together with a line but, instead express a
trend.

Question 6

What is the utility of pie chart ?

Answer

A pie chart is used to show parts in relation to the whole, often representing percentage shares
and numerical proportions.

Question 7
What is a bar chart ? How is it useful as compared to the line chart ?

Answer

A bar graph / bar chart is a graphical display of data using bars of different heights.

Compared to a line chart, which connects data points with lines, a bar chart is useful for
comparing discrete categories rather than showing continuous trends over time. Bar charts are
effective for highlighting differences in values between categories and are particularly useful
when dealing with categorical data or comparing data across different groups or time periods.

Question 8

What is a histogram ? What is its usage/utility ?

Answer

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


visual interpretation of numerical data by indicating the number of data points that lie within a
range of values (called "bins"). Histograms are a great way to show results of continuous data,
such as: weight, height, how much time, and so forth.

Question 9

What is a boxplot ? Which situations are more appropriate for boxplot ?

Answer

A boxplot is a graphical representation of the distribution of a dataset through five summary


statistics: the extremes (the highest and the lowest numbers), the median, and the upper and
lower quartiles.

Box plots are suitable for visualizing the spread of data, identifying outliers, comparing data
distribution between different groups or categories, and assessing symmetry in a dataset.

Question 10

What is a frequency polygon ? What is it utility ?

Answer

A frequency polygon is a type of frequency distribution graph. In a frequency polygon, the


number of observations is marked with a single point at the midpoint of an interval. A straight
line then connects each set of points. Frequency polygons make it easy to compare two or more
distributions on the same set of axes.

Question 11
Name the function to label axes.

Answer

The functions to label axes in a plot using Matplotlib's Pyplot library are xlabel() for the x-axis
and ylabel() for the y-axis.

Question 12

Name the function to give title to a plot.

Answer

The function title() in Matplotlib's Pyplot library is used to add a title to a plot.

Question 13

Name the function to set figure size of a plot.

Answer

The figure() function in Matplotlib's Pyplot library is used to set figure size of a plot.

Question 14

Name the function to set limits for the axes.

Answer

The function to set limits for the axes in a plot using Matplotlib's Pyplot library is xlim() for the
x-axis and ylim() for the y-axis.

Question 15

Name the function to show legends on a plot.

Answer

The legend() function in Matplotlib's Pyplot library is used to display a legend on the plot.

Question 16

Name the function to add ticks on axes.

Answer
The functions to add ticks on axes in a plot using Matplotlib's Pyplot library are xticks() for
the x-axis and yticks() for the y-axis.
Type B: Application Based Questions

Question 1

What is the significance of data visualization ?

Answer

Patterns, trends and correlations that might go undetected in text-based data can be exposed and
recognized easier with data visualization techniques or tools such as line chart, bar chart, pie
chart, histogram, scatter chart etc. Thus with data visualization tools, information can be
processed in efficient manner and hence better decisions can be made.

Question 2

How does Python support data visualization ?

Answer

Python supports data visualizations by providing some useful libraries for visualization. Most
commonly used data visaulization library is matplotlib. Matplotlib is a Python library, also
sometimes known as the plotting library. The matplotlib library offers very extensive range of
2D plot types and output formats. It offers complete 2D support along with limited 3D graphic
support. It is useful in producing publication quality figures in interactive environment across
platforms. It can also be used for animations as well. There are many other libraries of Python
that can be used for data Visualization but matplotlib is very popular for 2D plotting.

Question 3

What is the use of matplotlib and pyplot ?

Answer

For data visualization in Python, the matplotlib library's Pyplot interface is used. Matplotlib is a
Python library that provides interfaces and functionalities for 2D graphics, similar to MATLAB's
in various forms. It offers both a quick way to visualize data in Python and creates publication-
quality figures in many formats. The Matplotlib library offers various named collections of
methods. Pyplot, as one such interface, enables users to construct 2D plots easily and
interactively.

Question 4

What are the popular ways of plotting data ?


Answer

The popular ways of plotting data include line charts, bar charts, histograms, scatter plots, pie
charts, box plots.

Question 5

Compare bar() and barh() functions.

Answer

bar() function barh() function

This function is used to create vertical bar This function is used to create horizontal bar
charts. charts.

In a vertical bar chart, the bars are plotted


In a horizontal bar chart, the bars are plotted
along the vertical axis (y-axis) with their
along the horizontal axis (x-axis) with their
lengths representing the values being
lengths representing the values being plotted.
plotted.

The first sequence given in the bar() forms The first sequence given in the barh() forms
the x-axis and the second sequence values the y-axis and the second sequence values
are plotted on y-axis. are plotted on x-axis.

Question 6

What is the role of legends in a graph/chart ?

Answer

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 7

What will happen if you use legend() without providing any label for the data series being
plotted ?

Answer
Using legend() function without labels results in default labels (e.g., "line 1," "line 2"). This
can confuse viewers as it lacks meaningful information about the data series being plotted.

Question 8

What do you understand by xlimit and ylimit ? How are these linked to data being plotted ?

Answer

The xlimit and ylimit determine which data values are visible on the x-axis and y-axis in a plot or
chart respectively. Only the data values that fall within these limits will be plotted. If no data
value maps to the specified x-limits or y-limits, nothing will show on the plot for that particular
axis range.

Question 9

When should you use

(i) a line chart

(ii) a bar chart

(iii) a scatter chart

(iv) pie chart

(v) boxplot ?

Answer

(i) Line Chart — Use a line chart to show trends or changes over time. It's suitable for
displaying continuous data series and highlighting patterns or fluctuations.

(ii) Bar Chart — Use a bar chart to compare categories or groups. It's effective for displaying
discrete data and showing differences or relationships between items.

(iii) Scatter Chart — Use a scatter chart to visualize relationships between two variables. It's
helpful for identifying correlations or trends in data points.

(iv) Pie Chart — Use a pie chart to represent parts of a whole. It's useful for showing the
proportion or distribution of different categories within a dataset.

(v) Boxplot — The box plot is used to show the range and the middle half of ranked data while
identifying outliers or variability.

Question 10
A list namely temp contains average temperatures for seven days of last week. You want to see
how the temperature changed in 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 11

What is 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:
[Link](x, bins = None, cumulative = False, histtype = 'bar',
align = 'mid', orientation = 'vertical', ).

Question 12

What are 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 13

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 14

What is cumulative histogram ? How do you create it using PyPlot ?

Answer

A cumulative histogram is a graphical representation in which each bin displays the count of data
points within that bin as well as the counts of all smaller bins. The final bin in this histogram
indicates the total number of data points in the dataset.

In Matplotlib's hist function, we can create a cumulative histogram by setting


the cumulative parameter to True. The syntax is as follows: [Link](x,
bins = None, histtype='barstacked', cumulative=True).

Question 15

What is frequency polygon ? How do you create it ?

Answer

A frequency polygon is a type of frequency distribution graph. In a frequency polygon, the


number of observations is marked with a single point at the midpoint of an interval, and a
straight line then connects each set of points.

We can create frequency polygon in following two ways:

1. Drawing Frequency Polygon Manually


2. Creating Frequency Polygon through a Line Chart

Question 16

What is 5 point summary ?

Answer

The five-point summary is a descriptive statistics tool that provides a concise summary of the
distribution of a dataset. It consists of five important numbers of a data range:

1. the minimum range value


2. the maximum range value
3. the upper quartile
4. the lower quartile
5. the median

Question 17
What is Boxplot ? How do you create it in Pyplot ?

Answer

A boxplot is a visual representation of the statistical five number summary of a given data set,
including the extremes (the highest and the lowest numbers), the median, the upper and lower
quartiles.

With Pyplot, a boxplot is created using boxplot() function. The syntax is as


follows : [Link](x, notch = None, vert = None, meanline =
None, showmeans = None, showbox = None,).
Type C: Practical/Knowledge Based Questions

Question 1(a)

Execute the following codes and find out what happens ? (Libraries have been imported already ;
plt is the alias name for [Link])

A = [Link](2, 20, 2)
B = [Link](A)
[Link](A, B)
Will this code produce error ? Why/Why not ?

Answer

Executing the provided code will not produce an error. It will generate a plot of the logarithm of
A against A itself.

Output
Explanation

The line A = [Link](2, 20, 2) creates an array A using NumPy's arange() function. It
starts from 2, increments by 2, and includes values up to 20. This results in the array [2, 4, 6, 8,
10, 12, 14, 16, 18]. Next, the line B = [Link](A) calculates the natural logarithm of each
element in array A using NumPy's log() function and stores the results in array B.
Finally, [Link](A, B) plots the values in array A along the x-axis and the corresponding
values in array B along the y-axis using Matplotlib's plot() function.

Question 1(b)

Execute the following codes and find out what happens ? (Libraries have been imported already ;
plt is the alias name for [Link])

A = [Link](2, 20, 2)
B = [Link](A)
[Link](A, B)
Will this code produce error ? Why/Why not ?
Answer

Executing the provided code will not produce an error. However, the resulting plot might not be
as expected because the x-axis values are discrete and categorical, not continuous.

Output

Explanation

The line A = [Link](2, 20, 2) creates an array A using NumPy's arange() function. It
starts from 2, increments by 2, and includes values up to 20. This results in the array [2, 4, 6, 8,
10, 12, 14, 16, 18]. Next, the line B = [Link](A) calculates the natural logarithm of each
element in array A using NumPy's log() function and stores the results in array B.
Finally, [Link](A, B) creates a bar plot using Matplotlib's bar() function. It plots the values
in array A along the x-axis and the corresponding values in array B along the y-axis.

Question 1(c)
Execute the following codes and find out what happens ? (Libraries have been imported already ;
plt is the alias name for [Link])

X = [Link](1, 18, 2.655)


B = [Link](X)
[Link](X, Y)
Will this code produce error ? Why/Why not ?

Answer

The code will produce an error because the variable Y is not defined.
The corrected code is:

X = [Link](1, 18, 2.655)


B = [Link](X)
[Link](X, B)

Output
Explanation

The line X = [Link](1, 18, 2.655) creates an array X using NumPy's arange() function.
It starts from 1, increments by 2.655, and generates values less than 18. The resulting array will
look like [1., 3.655, 6.31, 8.965, 11.62, 14.275, 16.93]. Next, the line B = [Link](X) calculates
the natural logarithm of each element in array X using NumPy's log() function. Finally, the
line [Link](X, Y) attempts to use Matplotlib's scatter() function to create a scatter
plot. However, Y is not defined in code, leading to a NameError.

Question 2

Write the output from the given python code :

import [Link] as plt


Months = ['Dec', 'Jan', 'Feb', 'Mar']
Attendance = [70, 90, 75, 95]
[Link](Months, Attendance)
[Link]()
Answer

Output
Explanation

This code snippet uses Matplotlib to create a bar chart. The list Months contains the names of the
months ['Dec', 'Jan', 'Feb', 'Mar'], while the list Attendance holds corresponding attendance
values [70, 90, 75, 95]. The [Link]() function is then used to create a bar plot, where each bar
represents a month and its height corresponds to the attendance value. Finally, [Link]() is
called to display the plot.

Question 3

Write a program to add titles for the X-axis, Y-axis and for the whole chart in below code.

import [Link] as plt


Months = ['Dec', 'Jan', 'Feb', 'Mar']
Attendance = [70, 90, 75, 95]
[Link](Months, Attendance)
[Link]()
Answer
import [Link] as plt
Months = ['Dec', 'Jan', 'Feb', 'Mar']
Attendance = [70, 90, 75, 95]
[Link](Months, Attendance)
[Link]('Months')
[Link]('Attendance')
[Link]('Attendance Report')

[Link]()

Output

Question 4

[Link](A, B) produces (A and B are the sequences same as created in question 1) chart as :
Write code to produce charts as shown below:

Answer

import numpy as np
import [Link] as plt

A = [Link](2, 20, 2)
B = [Link](A)
C = [Link](A) * 1.2
[Link](A, B)
[Link](A, C)
[Link]()

Output
import numpy as np
import [Link] as plt

A = [Link](2, 20, 2)
B = [Link](A)
C = [Link](A) * (-1.2)
[Link](A, B)
[Link](A, C)
[Link]()

Output
Question 5

Write suitable Python code to create 'Favourite Hobby' Bar Chart as shown below :
Also give suitable python statement to save this chart.

Answer

import [Link] as plt


hobbies = ['Dance', 'Music', 'Painting', 'Playing Sports']
people_count = [300, 400, 100, 500]
[Link](hobbies, people_count)
[Link]('Hobbies')
[Link]('Number of People')
[Link]('Favourite Hobby')
[Link]('favourite_hobby_chart.png')
[Link]()

Output
Question 6

Consider the following graph. Write the Python code to plot it. Also add the Title, label for X
and Y axis.
Using the following data for plotting the graph

smarks = [10, 40, 30, 60, 55]


sname = ["Sahil", "Deepak", "Anil", "Ravi", "Riti"]
Answer

import [Link] as plt


smarks = [10, 40, 30, 60, 55]
sname = ["Sahil", "Deepak", "Anil", "Ravi", "Riti"]
[Link](sname, smarks)
[Link]('Student Name')
[Link]('Marks Scored')
[Link]('Marks Secured by Students in Term-1')
[Link]()

Output
Question 7(a)

Given a data frame df1 as shown below :

1990 2000 2010

a 52 340 890

b 64 480 560

c 78 688 1102

d 94 766 889

Write code to create a scatter chart from the 1990 and 2010 columns of dataframe df1.

Answer
import pandas as pd
import [Link] as plt
data = {'1990': [52, 64, 78, 94],
'2000': [340, 480, 688, 766],
'2010': [890, 560, 1102, 889]}
df1 = [Link](data, index=['a', 'b', 'c', 'd'])
[Link](df1['1990'], df1['2010'])
[Link]()

Output

Question 7(b)

Given a data frame df1 as shown below :

1990 2000 2010

a 52 340 890
b 64 480 560

c 78 688 1102

d 94 766 889

Write code to create a line chart from the 1990 and 2000 columns of dataframe df1.

Answer

import pandas as pd
import [Link] as plt
data = {'1990': [52, 64, 78, 94],
'2000': [340, 480, 688, 766],
'2010': [890, 560, 1102, 889]}
df1 = [Link](data, index=['a', 'b', 'c', 'd'])
[Link](df1['1990'], df1['2000'])
[Link]()

Output
Question 7(c)

Given a data frame df1 as shown below :

1990 2000 2010

a 52 340 890

b 64 480 560

c 78 688 1102

d 94 766 889

Write code to create a bar chart plotting the three columns of dataframe df1.

Answer
import [Link] as plt
data = {'1990': [52, 64, 78, 94],
'2000': [340, 480, 688, 766],
'2010': [890, 560, 1102, 889]}
df1 = [Link](data, index=['a', 'b', 'c', 'd'])
[Link](kind = 'bar')
[Link]()

Output

Question 8

The score of four teams in 5 IPL matches is available to you. Write a program to plot these in a
bar chart.

Answer

import [Link] as plt


import numpy as np
Matches = ['Match 1', 'Match 2', 'Match 3', 'Match 4', 'Match
5']
Team_A = [150, 160, 170, 180, 190]
Team_B = [140, 150, 160, 170, 180]
Team_C = [130, 140, 150, 160, 170]
Team_D = [120, 130, 140, 150, 160]
X = [Link](len(Matches))
[Link](Matches, Team_A, width = 0.15)
[Link](X + 0.15, Team_B, width = 0.15)
[Link](X + 0.30, Team_C, width = 0.15)
[Link](X + 0.45, Team_D, width = 0.15)
[Link]('Matches')
[Link]('Scores')
[Link]('IPL Scores')
[Link]()
[Link]()

Output
Question 9

The score of a team in 5 IPL matches is available to you. Write a program to create a pie chart
from this data, showing the last match's performance as a wedge.

Answer

import [Link] as plt


Matches = ['Match 1', 'Match 2', 'Match 3', 'Match 4', 'Match
5']
Team = [150, 160, 170, 180, 190]
expl = [0, 0, 0, 0, 0.2]
[Link](Team, labels = Matches, explode = expl)
[Link]('Team A Scores')
[Link]()

Output

Question 10
The prices of a stock for 3 months are given. Write a program to show the variations in prices for
each month by 3 lines on same line chart. Make sure to add legends and labels. Show grid also.

Answer

import [Link] as plt


months = ['January', 'February', 'March']
prices_stock_A = [100, 120, 110]
prices_stock_B = [90, 110, 100]
prices_stock_C = [95, 115, 105]

[Link](months, prices_stock_A, label='Stock A', marker='o')


[Link](months, prices_stock_B, label='Stock B', marker='s')
[Link](months, prices_stock_C, label='Stock C', marker='^')
[Link]('Months')
[Link]('Prices')
[Link]('Stock Prices Variation')
[Link]()
[Link](True)
[Link]()

Output
Question 11

A distribution data stores about 1000 random number. Write a program to create a scatter chart
from this data with varying point sizes.

Answer

import numpy as np
import [Link] as plt
X = [Link](1, 100, size = (1000,))
Y = [Link](1, 100, size = (1000,))
sizes = [Link](10, 100, size=100)
[Link](X, Y, s = sizes, color = 'r')
[Link]()

Output
Question 12

Navya has started an online business. A list stores the number of orders in last 6 months. Write a
program to plot this data on a horizontal bar chart.

Answer

import [Link] as plt


orders = [150, 200, 180, 250, 300, 220]
months = ['January', 'February', 'March', 'April', 'May',
'June']
[Link](months, orders)
[Link]('Number of Orders')
[Link]('Month')
[Link]('Number of Orders in Last 6 Months')
[Link]()

Output
Question 13(a)

Given the following set of data :

Weight measurements for 16 small orders of French-fries (in


grams).

78 72 69 81 63 67 65 75
79 74 71 83 71 79 80 69
Create a simple histogram from the above data.

Answer

import [Link] as plt


weights = [78, 72, 69, 81, 63, 67, 65, 75, 79, 74, 71, 83, 71,
79, 80, 69]
[Link](weights)
[Link]('Weight Distribution of French Fries Orders')
[Link]()
Output

Question 13(b)

Given the following set of data :

Weight measurements for 16 small orders of French-fries (in


grams).

78 72 69 81 63 67 65 75
79 74 71 83 71 79 80 69
Create a horizontal histogram from the above data.

Answer

import [Link] as plt


weights = [78, 72, 69, 81, 63, 67, 65, 75, 79, 74, 71, 83, 71,
79, 80, 69]
[Link](weights, orientation = 'horizontal')
[Link]('Weight Distribution of French Fries Orders')
[Link]()

Output

Question 13(c)

Given the following set of data :

Weight measurements for 16 small orders of French-fries (in


grams).

78 72 69 81 63 67 65 75
79 74 71 83 71 79 80 69
Create a step type of histogram from the above data.

Answer

import [Link] as plt


weights = [78, 72, 69, 81, 63, 67, 65, 75, 79, 74, 71, 83, 71,
79, 80, 69]
[Link](weights, histtype = 'step')
[Link]('Weight Distribution of French Fries Orders')
[Link]()

Output

Question 13(d)

Given the following set of data :

Weight measurements for 16 small orders of French-fries (in


grams).

78 72 69 81 63 67 65 75
79 74 71 83 71 79 80 69
Create a cumulative histogram from the above data.
Answer

import [Link] as plt


weights = [78, 72, 69, 81, 63, 67, 65, 75, 79, 74, 71, 83, 71,
79, 80, 69]
[Link](weights, cumulative = True)
[Link]('Weight Distribution of French Fries Orders')
[Link]()

Output

Question 14(a)

Create an ndarray containing 16 values and then plot this array along with dataset of previous
question in same histogram, normal histograms.

Answer

import numpy as np
import [Link] as plt
weights = [78, 72, 69, 81, 63, 67, 65, 75, 79, 74, 71, 83, 71,
79, 80, 69]
random_array = [Link](16)
[Link](weights)
[Link](random_array)
[Link]('Normal Histograms')
[Link]()

Output

Question 14(b)

Create an ndarray containing 16 values and then plot this array along with dataset of previous
question in same histogram, cumulative histograms.

Answer

import numpy as np
import [Link] as plt
weights = [78, 72, 69, 81, 63, 67, 65, 75, 79, 74, 71, 83, 71,
79, 80, 69]
random_array = [Link](16)
[Link](weights, cumulative = True)
[Link](random_array, cumulative = True)
[Link]('Cumulative Histograms')
[Link]()

Output

Question 14(c)

Create an ndarray containing 16 values and then plot this array along with dataset of previous
question in same histogram, horizontal histograms.

Answer

import numpy as np
import [Link] as plt
weights = [78, 72, 69, 81, 63, 67, 65, 75, 79, 74, 71, 83, 71,
79, 80, 69]
random_array = [Link](16)
[Link](weights, orientation = 'horizontal')
[Link](random_array, orientation = 'horizontal')
[Link]('Horizontal Histograms')
[Link]()

Output

Question 15

Out of above plotted histograms, which ones can be used for creating frequency polygons ? Can
you draw frequency polygons from all the above histograms ?

Answer
Out of the above plotted histograms, none can be used for creating frequency polygons. We
cannot draw frequency polygons from all the above histograms because to construct a frequency
polygon, we need a step-type histogram. A frequency polygon is constructed by connecting the
midpoints of the tops of the bars of a histogram. Step-type histograms provide a clear outline to
draw these connections.

Question 16

Create/draw frequency polygon from the data used in above questions.

Answer

import numpy as np
import pandas as pd
import [Link] as plt
weights = [78, 72, 69, 81, 63, 67, 65, 75, 79, 74, 71, 83, 71,
79, 80, 69]
[Link](figsize = (10, 5))
n, edges, p = [Link](weights, bins = 40, histtype = 'step')
m = 0.5 * (edges[1:] + edges[:-1])
m = [Link]()
l = len(m)
[Link](0, m[0] - 10)
[Link](m[l-1] + 10)
n = [Link]()
[Link](0, 0)
[Link](0)
[Link](m, n, '-^')
[Link]()

Output
Question 17(a)

From the following ordered set of data :

63, 65, 67, 69, 69, 71, 71, 72, 74, 75, 78, 79, 79, 80, 81, 83
Create a horizontal boxplot.

Answer

import [Link] as plt


data = [63, 65, 67, 69, 69, 71, 71, 72, 74, 75, 78, 79, 79, 80,
81, 83]
[Link](data, vert = False)
[Link]()

Output
Question 17(b)

From the following ordered set of data :

63, 65, 67, 69, 69, 71, 71, 72, 74, 75, 78, 79, 79, 80, 81, 83
Create a vertical boxplot.

Answer

import [Link] as plt


data = [63, 65, 67, 69, 69, 71, 71, 72, 74, 75, 78, 79, 79, 80,
81, 83]
[Link](data)
[Link]()

Output
Question 17(c)

From the following ordered set of data :

63, 65, 67, 69, 69, 71, 71, 72, 74, 75, 78, 79, 79, 80, 81, 83
Show means in the boxplot.

Answer

import [Link] as plt


data = [63, 65, 67, 69, 69, 71, 71, 72, 74, 75, 78, 79, 79, 80,
81, 83]
[Link](data, showmeans = True)
[Link]()

Output
Question 17(d)

From the following ordered set of data :

63, 65, 67, 69, 69, 71, 71, 72, 74, 75, 78, 79, 79, 80, 81, 83
Create boxplot without the box.

Answer

import [Link] as plt


data = [63, 65, 67, 69, 69, 71, 71, 72, 74, 75, 78, 79, 79, 80,
81, 83]
[Link](data, showbox = False)
[Link]()

Output
Question 18

Sina has created ordered set of data from the number of new customers registered on his online
service centre in last 20 months.

Write a program to plot this data on a filled boxplot with means shown.

Answer

import [Link] as plt


data = [100, 120, 95, 110, 105, 130, 115, 125, 135, 140, 120,
110, 105, 115, 130, 125, 110, 115, 120, 135]
[Link](data, patch_artist = True, showmeans = True)
[Link]('Number of New Customers Registered in Last 20
Months')
[Link]()

Output

You might also like