Data Visualization using Pyplot in Python solved assignment for CBSE Class 12. Short and Application based questions and answers from Sumita Arora Computer Science (Python) Class XII book.

### Short Answer questions / Conceptual Questions:Data Visualization using Pyplot in Python

**Q,1. What is the significance of data visualization ?**

**Answer:**

Data visualization gives us** **a clear idea of what the information means by giving it visual context through maps or graphs.

**Q.2.How does Python support data visualization?**

**Answer:**

Python supports data visuliation through the pyplot interface of its matplotlib library. Pyplot is a collection of methods within matplotlib which allows to construct 2D plots easily and interactively.

**Q.3.What is the use of matplotlib and pyplot ?**

**Answer:**

The matplotlib is a python library that provides many interfaces and functionality for 2D graphics. Pyplot is a matplotlib interface. It is a collection of methods within matplotlib which allows to construct 2D plots easily and interactively. These two together help to visualize data in python.

**Q.4.What are the popular ways of plotting data?**

**Answer:**

Some popular ways of plotting data are in the form of line charts , bar charts, pie charts , scatter charts etc.

### Short Answer questions / Conceptual Questions:Data Visualization Class 12 using Pyplot in Python

**Q,1. What is the significance of data visualization ?**

**Answer:**

Data visualization gives us** **a clear idea of what the information means by giving it visual context through maps or graphs.

**Q.2.How does Python support data visualization?**

**Answer:**

Python supports data visuliation through the pyplot interface of its matplotlib library. Pyplot is a collection of methods within matplotlib which allows to construct 2D plots easily and interactively.

**Q.3.What is the use of matplotlib and pyplot ?**

**Answer:**

The matplotlib is a python library that provides many interfaces and functionality for 2D graphics. Pyplot is a matplotlib interface. It is a collection of methods within matplotlib which allows to construct 2D plots easily and interactively. These two together help to visualize data in python.

**Q.4.What are the popular ways of plotting data?**

**Answer:**

Some popular ways of plotting data are in the form of line charts , bar charts, pie charts , scatter charts etc.

**Q.5.What are ndarrays ?How are they different from python lists?**

**Answer:**

An ndarray is a multidimensional container of items of the same type and size. The number of dimensions and items in an array is defined by its shape, which is a tuple of N positive integers that specify the sizes of each dimension. Lists in python can contain elements of different types while ndarray can only contain elements of the same type. Practically ndarrays are faster than lists.

**Q.6.Compare bar() and barh() functions.**

**Answer:**

Both bar() and barh() are used to plot bar charts. barh() just plots horizontal bar charts. e.g.

bar(‘cities’,’popul’) #cities in the horizontal axis and bars grows upwards

barh(‘cities’,’popul’)#cities in vertical axis and bars grow toward the right.

**Q.7. What is the role of legends in a graph/chart?**

**Answer:**

When we plot multiple ranges on a single plot, it becomes necessary that legends are specified. Legends are color or mark linked to a specific data range plotted. legend() function is used to add legends to a plot.

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

**Answer:**

If the legend() function is used without specifying any labels then the warning “No handles with labels found to put in legend” will be raised.

**Q.9.What do you understand by x limit and ylimit ? How are theses linked to data being plotted?**

**Answer:**

xlimit and ylimit are used to set limit on the x & y axis range on which the data being plotted is shown. xlim() & ylim() functions are used for this.e.g.

plt.xlim(-2.0,4.0)

The data on the chart will now be plotted only for the range from -2 to 4.

**Q.10.When should you use (i) a line chart (ii) a bar chart (iii)a pie chart?**

**Answer:**

i)line chart

Line charts should be used when we need to connect all the data points and see patterns.

ii)bar chart

Bar charts should be used when we need to visualize categorical data. It helps to see trends.

iii)Pie chart

A pie chart shows how some total amount is divided among distinct categories as a circle (the namesake pie) divided into radial slices. Each category is associated with a single slice whose size corresponds with the category’s proportion of the total.

**Q.11. Write code in python to create a list L1 having even elements between 1-10. Create another list L2 that stores the cubes of element of list L2.**

**Answer:**

L1=list(range(0,11,2))

L2=[i*3 for i in L1]

**Q.12.Repeat the previous question but this time use numpy functions to create the two sequences.**

**Answer:**

L1=np.array((range(0,11,2))

L2= numpy.array(L1)*3

**Q.13. A list temp contains average temperatures for seven days of last week. You want to see how the temperature changes L last seven days . which chart type will you plot for the same and why?**

**Answer:**

A bar chart should be used to plot such data because the data is categorised in 7 week days. This bars will be adequate for showing the average temperature on these week days.

**Q.14.Write code to practically produce a chart for question 8.**

Answer:import matplotlib.pyplot as plt simple=[] for i in range (20): simple.append(i) plt.plot(list(range(20)),simple) plt.legend() plt.show()

**Q.15.A company has three offices across India: Delhi,Mumbai,Kolkata. Each of the offices has sent a compiled list sharing sales in quater1, quater2,quater3 and quater4.(a) suggest the best chart type to plot all the above data?Answer:**

A bar Graph.

**(b)Why did you choose that specific chart type?**

Answer:

Answer:

In this a direct compare of the all quarter for various company will be done easily.

**(c)Can you create a pie chart from above data?**

Answer:

Answer:

Yes,we can have three pie charts each for the different city branch.

**Q.16. Write code to produce a chart plotting all of the above data?**

Answer:import matplotlib.pyplot as plt import numpy as np curve[] for i in range(20): curve.append(i*2) #Line chart plt.plot(list(range(20)),curve) plt.show() #pie chart langs=['c','c++','Java','Python','PHP'] students=[23,17,35,29,12] plt.pie( students, labels+langs,autopct='%1.2f%%') plt.show()

**Application based question**:Data Visualization Class 12 using Pyplot in Python

**Q.2. Execute the following codes and find out what happens ?(a)A=np.arange(2,20,2)B=np.log(A)plt.plot(A,B)(b)A=np.arange(2,20,2)B=np.log(A)plt.bar(A,B)(c)A=np.arange(2,20,2)B=np.log(A)plt.pie(A,B)will any code produce error?why?**

**Solution:**

(a) No error

(b) No error

(c)No error, will show improper visualization due to the wrong value as parameter passed on.

**Q.3. Add titles for the X-axis, Y-axis and for the whole chart in above codes.**

(a)

A=np.arange(2,20,2)

B=np.log(A)

plt.plot(A,B)

plt.title(‘log’)

plt.xlabel(“X axis”)

plt.ylabel(“y axis”)

plt.show()

(b)

A=np.arange(2,20,2)

B=np.log(A)

plt.bar(A,B)

plt.title(‘log’)

plt.xlabel(“X axis”)

plt.ylabel(“y axis”)

plt.show()

**Answer:**

import matplotlib.pyplot as plt

import numpy as np

#code for A vs B’s every value*1.2

A=np.arange(2,20,2)

B=np.log(A)

B1=B*1.2

plt.plot(A,B)

plt.plot(A,B1)

plt.show()

#code for A vs B’s every value*-1.2

A=np.arange(2,20,2)

B=np.log(A)

B1=B*1.2

plt.plot(A,B)

plt.plot(A,B1)

plt.show()

** Q.5.Why is following command producing error?(plt is the alias name of importes library matplotlib.pyplot and A is an ndarray created in question 2)plt.pie(A,A+2,A+3,A+4)**

**Answer:**

Improper argument passed is the cause of error while plt.pie() requires different argument.

Thanks for viewing Data Visualization Class 12 using Pyplot in Python . Also check other chapter solution and MCQS of python.