Skip to main content

CS-Chapter-16-Data Visualization Using Pyplot.

Chapter-16


Question 1.

Which is a python package used for 2D graphics?

a) matplotlib.pyplot

b) matplotlib.pip

c) matplotlib.numpy

d) matplotlib.plt

Answer:

a) matplotlib.pyplot




Question 2.

Identify the package manager for Python packages, or modules.

a) Matplotlib

b) PIP

c) plt.show()

d) python package

Answer:

b) PIP




Question 3.

Read the following code: Identify the purpose of this code and choose the right option from the following.

C:\Users\YourName\AppData\Local\Programs\Python\Python36-32\Scripts > pip – version

a) Check if PIP is Installed

b) Install PIP

c) Download a Package

d) Check PIP version

Answer:

d) Check PIP version




Question 4.

Read the following code: Identify the purpose of this code and choose the right option from the following.

C:\Users\Your Name\AppData\Local\Programs\Python\Python36-32\Scripts > pip list

a) List installed packages

b) list command

c) Install PIP

d) packages installed

Answer:

a) List installed packages




Question 5.

To install matplotlib, the following function will be typed in your command prompt. What does “-U” presents?

Python -m pip install -U pip

a) downloading pip to the latest version

b) upgrading pip to the latest version

c) removing pip

d) upgrading matplotlib to the latest version

Answer:

b) upgrading pip to the latest version




Question 6.

Observe the output figure. Identify the coding for obtaining this output.


a) import matplotlib.pyplot as plt

plt.plot([1,2,3],[4,5,1])

plt.show()


b) import matplotlib.pyplot as plt 

plt.plot([1,2],[4,5])

plt.show()


c) import matplotlib.pyplot as plt

plt.plot([2,3],[5,1])

plt.show()


d) import matplotlib.pyplot as plt 

plt .plot ([1,3], [4,1 ])

plt.show()


Answer:

a) import matplotlib.pyplot as plt

plt.plot([1,2,3],[4,5,1])

plt.show()




Question 7.


Read the code:

a. import matplotlib.pyplot as pit

b. plt.plot(3,2)

c. plt.show()


Identify the output for the above coding

Answer:








Question 8.

Which key is used to run the module?

a) F6

b) F4

c) F3

d) F5

Answer:

d) F5




Question 9.

Identify the right type of chart using the following hints.

Hint 1: This chart is often used to visualize a trend in data over intervals of time.

Hint 2: The line in this type of chart is often drawn chronologically.

a) Line chart

b) Bar chart

c) Pie chart

d) Scatter plot

Answer:

a) Line chart




Question 10.

Read the statements given below. Identify the right option from the following for pie chart. Statement A : To make a pie chart with Matplotlib, we can use the plt.pie() function.

Statement B : The autopct parameter allows us to display the percentage value using the Python string formatting.

a) Statement A is correct

b) Statement B is correct

c) Both the statements are correct

d) Both the statements are wrong

Answer:

b) Statement B is correct




II Answer the following questions (2 marks)


Question 1

Define: Data Visualization.

Answer:


  • Data Visualization is the graphical representation of information and data.


  • The objective of Data Visualization is to communicate information visually to users.


  • Data visualization uses statistical graphics.




Question 2.

List the general types of data visualization.

Answer:


  • Charts
  • Tables
  • Graphs
  • Maps
  • Infographics
  • Dashboards




Question 3.

List the types of Visualizations in Matplotlib.

Answer:


There are many types of Visualizations under Matplotlib. Some of them are: Line plot


  • Scatter plot
  • Line plot
  • Histogram
  • Box plot
  • Bar chart and
  • Pie chart




Question 4.

How will you install Matplotlib?

Answer:

  • Matplotlib can be installed using pip software.
  • Pip is a management software for installing python packages.
  • Importing Matplotlib using the command: import matplotlib.pyplot as plt
  • Matplotlib can be imported in the workspace.




Question 5.

Write the difference between the following functions: plt. plot([1,2,3,4]), plt. plot ([1,2,3,4],

[14, 9,16]).

Answer:

plt.plot([1,2,3,4])
plt.plot([1,2,3,4], [1,4,9,16])
It refers y value as [1,2,3,4] It refers x and y values as ([1,2,3,4], [1,4,9,16])
Indirectly it refers x values as [0,1,2,3] (0,1) (1,1) (2,3) (3,4) Directly x and y values are given as (1,1) (2,4) (3,9) (4,16)




III. Answer the following questions (3 marks)


Question 1.

Draw the output for the following data visualization plot.

import matplotlib.pyplot as plt

plt.bar([1,3,5,7,9],[5,2,7,8,2], label=”Example one”)

plt.bar([2,4,6,8,10],[8,6,2,5,6], label=:”Example two”, color=’g’)

plt.legend()

plt.xlabel(‘bar number’)

plt.ylabel(‘bar height’)

plt.title(‘Epic Graph\nAnother Line! Whoa’)

plt.show()


Output:



Question 2.

Write any three uses of data visualization.

Answer:


  • Data Visualization help users to analyze and interpret the data easily.
  • It makes complex data understandable and usable.
  • Various Charts in Data Visualization helps to show relationship in the data for one or more variables




Question 3.

Write the coding for the following:

a) To check if PIP is Installed in your PC.

b) To Check the version of PIP installed in your PC.

c) To list the packages in matplotlib.

Answer:


a) To check if PIP is Installed in your PC.


  1. Incommand prompt type pip – version.
  2. If it is installed already, you will get version.
  3. Command : Python – m pip install – U pip


b) To Check the version of PIP installed in your PC.

C:\ Users\ YourName\ AppData\ Local\ Programs\ Py thon\ Py thon36-32\ Scripts>pip-version


c) To list the packages in matplotlib.

C:\ Users\ Y ou rName\ AppData\ Local\ Programs\ Py thon\ Py thon36-32\ Scripts>pip list




Question 4.

Write the plot for the following pie chart output.

Coding:

import matplotlib.pyplot as plt

slices = [7,2,2,13]

activities = [„sleeping‟, „eating‟, „working‟, „playing‟]

plt.pie(slices, labels=activities, atopct = „y.1.1 f%%‟)

plt.title(„Interesting Graph Ceck It Out‟)

plt.show()


Output:




IV. Answer the following questions (5 Marks)


Question 1.

Explain in detail the types of pyplots using Matplotlib.

Answer:

Matplotlib allows us to create different kinds of plots ranging from histograms and scatter plots to bar graphs and bar charts.


Line Chart:


  • 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.
  • A Line Chart is often used to visualize a trend in data over intervals of time – a time series – thus the line is often drawn chronologically.

Program for Line plot:

import matplotlib.pyplot as pit years = [2014, 2015, 2016, 2017, 2018] total_populations = [8939007, 8954518,, 8960387,8956741,8943721]

plt. plot (“years, total_populations)

plt. title (“Year vs Population in India”)

plt.xlabel (“Year”)

plt.ylabel (“Total Population”)

plt.showt


In this program,

Plt.titlet() →→7 specifies title to the graph

Plt.xlabelt) →→ specifies label for X-axis

Plt.ylabelO →→ specifies label for Y-axis

Output:


Bar Chart:


  • A BarPlot (or Bar Chart) is one of. the most common type of plot. It shows the relationship between a numerical variable and a categorical variable.
  • Bar chart represents categorical data with rectangular bars. Each bar has a height corresponds to the value it represents.
  • The bars can be plotted vertically or horizontally.
  • It is useful when we want to compare a given numeric value on different categories.
  • To make a bar chart with Matplotlib, we can use the plt.bart) function.


Program:

import matplotlib.pyplot as pit

# Our data

labels = [“TAMIL”, “ENGLISH”, “MATHS”, “PHYSICS”, “CHEMISTRY”, “CS”]

usage = [79.8,67.3,77.8,68.4,70.2,88.5]

# Generating the y positions. Later, we’ll use them to replace them with labels. y_positions = range (len(labels))

# Creating our bar plot

plt.bar (y_positions, usage)

plt.xticks (y_positions, labels)

plt.ylabel (“RANGE”)

pit.title (“MARKS”)

plt.show()


Output:


Pie Chart:


  • Pie Chart is probably one of the most common type of chart.
  • It is a circular graphic which is divided into slices to illustrate numerical proportion.
  • The point of a pie chart is to show the relationship of parts out of a whole.
  • To make a Pie Chart with Matplotlib, we can use the plt.pie () function.
  • The autopct parameter allows us to display the percentage value using the Python string formatting.


Program:

import matplotlib.pyplot as pit

sizes = [89, 80, 90,100, 75]

labels = [“Tamil”, “English”, “Maths”,

“Science”, “Social”]

plt.pie (sizes, labels = labels,

autopct = “%.2f”)

plt.axesfj.set aspect (“equal”)

plt.showt()


Output:




Question 2.

Explain the various buttons in a matplotlib window.

Answer:

Various buttons in a matplotlib window:


Home Button:

The Home Button will help one to begun navigating the chart. If you ever want to return back to the original view, you can click on this.


Forward/Back buttons:

Forward/Back buttons can be used like the Forward and Back buttons in browser. Click these to move back to the previous point you were at, or forward again.


Pan Axis:

This cross-looking button allows us to click it, and then click and drag graph around.


Zoom :

The Zoom button lets us click on it, then click and drag a square would like to zoom into specifically.

Zooming in will require a left click and drag. Zoom out with a right click and drag.


Configure Subplots:

Configure Subplots button allows us to configure various spacing options with figure and plot.


Save Figure :

Save Figure button will allow us to save figure in various forms.




Question 3.

Explain the purpose of the following functions:


Answer:

a) plt.xlabel:

plt.xlabel Specifies label for X -axis

b) plt.ylabel:

plt.ylabel is used to specify label for y-axis

c) plt.title :

plt.title is used to specify title to the graph or assigns the plot title.

d) plt.legend():

plt.legend() is used to invoke the default legend with plt

e) plt.show():

plhshowQ is used to display the plot.