Loading...
Please wait while we prepare your content
Please wait while we prepare your content
Solutions for Informatics Practices, Class 12, CBSE
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.
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.
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.
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.
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.
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
.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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".
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:
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()
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.
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?
(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?
(iv) The number of markers in the above line chart are:
(v) Which of the following methods will result in displaying 'My first graph!' in the above graph?
(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!".
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.
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.
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")
.
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.
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.
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.
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.
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.
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.
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()
Figure | Axes |
---|---|
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. |
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:
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()
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()
The various methods used with 'pyplot' object are as follows:
(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.
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.
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()
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', )
.
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').
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.
The three libraries in Python used for data analysis are as follows:
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")
.
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,.....])
.
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.