Looking for a Tutor Near You?

Post Learning Requirement »
x

Choose Country Code

x

Direction

x

Ask a Question

x

Hire a Tutor

Presentation On PyPlot

Loading...

For the student of Class 12 Computer Science CBSE

Kushal K / Kolkata

22 years of teaching experience

Qualification: M.Tech. (IT) from Bengal Engg. and Science University (BESU, Shibpur, Howrah), M.C.A., M.Sc (Comp. Sc.). DOEACC-A-Level

Teaches: Computer Science, School Level Computer, B.Tech Tuition, BCA Tuition, IT, Computer, C / C++, Python Programming, Artificial Intelligence, Data Science, MCA Subjects

Contact this Tutor
  1. Study Material for XII Computer Science on Data Visualization using Pyplot Prepared by Kushal Roy
  2. What is Data Visualization? o Data visualization refers to the graphical or visual representation of information and data using visual elements like charts, graphs , maps etc. It is immensely useful in decision making to understand the meaning of data to drive business decision. For data visualization in Python, the Matplotlib library's pyplot is used. Pyplot is a collection of methods within matplotlib library(of python) to construct 2D plot easily and interactively. Matplotlib library is preinstalled with Anaconda distribution. In order to use pyplot for data visualization, user needs to import the pyplot module as follows: import matplotlib.pyplot as plt
  3. Working with pyplot methods o The pyplot interface provides many methods for 2D plotting of data in multiple ways such as line chart, bar chart, pie chart, scatter chart etc. But before proceeds, it is recommended to have an idea of using numpy library and some of it s functions, as: Numpy offers some useful function to create arrays of data, which provides useful while plotting data. Numpy arrays which is like a list supports vectorized operation that is not supported by list object in python. To use numpy arrays one needs to import the respective module as: import numpy as np We can also create a numpy array from a list object as follows: import numpy as np ar= np.array(L) print(ar)
  4. o 0 Editor - CSPlot1,py O pandas4py 1 # Demo for vectorized 2 import numpy as np 3 4 ar=np. array(L) -ILL) 6 print( "Array: 7 X Help Source Console Object operation Usage Help to to array list Variable explorer File explorer IPython console Console IfA List Array= [1 2 3 4] 8 # adding some scalar quantity 9 ar=ar+10 10 print( "Modified array- # adding some scalar quantity 11 12 L=L+IO 13 print("Modified List:" , L) 15 Modified array- [11 12 13 14] (most recent call last): Traceback line in File 1, File "C:\Users\Kushal\Anaconda3\lib\site-packages\spyder_kernelE In runfile line 704, execfile(filenamej namespace File "C:\Users\Kushal\Anaconda3\lib\site-packages\spyder_kernelE line in execfile 108, filename, exec File "C:/Users/Kushal/CSPlot1.py", line 12, L=L+IO namespace in
  5. array vs, List : Comparison Both the data structures allow indexing, slicing, iterating and mutability. But: List is built-in data structure in python where as to use an array in Python, we need to import this data structure from the NumPy package. List does not support vectorized operation while array supports the same. Arrays are great for numerical operations with compared to list Lists can be re-sized where array can not be List is heterogeneous where array is homogeneous structure Nested list is possible but nested array is not possible
  6. Creating numpy array o =numpy.arange([start,] stop [,step] [,dtype]) arr=np.arange(1,7,2,np.int32 In runfile( 'C:/LIsers/Kushal/CSPlot1.py' arr=np.arange(7) In [7]: runfile( 'C:/Users/Kushal/CSPlot1.py' =numpy.linspace(, , ) [11]: runfile( 'C:/Users/Kushal/CSPlot1.pyT 2.2 2.4 2.6 2.8 3. ]
  7. o 1 2 3 5 import numpy as np import matplotlib pyplo± as PI ± arr=np. 6) 4 print( "values are end " print ( 6 print( "plotting 'the values in graph. PI t plot (arr) 7 In [IS]: run file( 'C: / Users/ Kusha1/CSP10t1 py' values are 2.2 2.4 2.6 2.8 3. J plotting the values in graph. 30 2.8 26 2.4 2.2 2.0 1 2 3 4 5
  8. Some useful numpy functions: Function Name sin(x) cos(x) arcsin(x) floor(x) ceil(x) trunc(x) exp(x) exp2(x) log(x) log10(x) log2(x) Description Trigonometric sine, element wise Trigonometric cosine, element wise Inverse sine element wise Returns floor of the input, element wise Returns ceiling of the input, element wise Returns truncated value of the input, element wise Calculate exponential of all values in the array Calculate 2* *p for all p in input array Natural logarithm element wise Returns base 10 log of the input array Base 2 log of x
  9. o In Example Console IJA [24]: runfile( I' c: / Users/Kusha1/CSP10t1. py' 14 1.2 0.6 0.4 0.2 0.0 15 20 25 import numpy as np 3.0 3.5 4.0 4.5 5.0 import matplotlib. pyplot y=np log(x) PI t plot (x * as PI t
  10. Commonly used chart type: Line chart: It displays information as a series of data points called 'Markers' by straight line segment using the plot( ). A line chart is often used to visualize a trend in data over intervals of time Bar chart: It is the graphical display of categorical data using bars of different height using bar( ). It can be either vertical or horizontal Pie chart: It is a circular statistical graph which is divided into slices to illustrate the numerical proportion. Scatter chart: Scatter plots are used to plot data points on horizontal and vertical axis in the attempt to show how much one variable is affected by another(correlation).
  11. Commonly used chart type: Bar Gra ph Sulphur Dioxide Emissions WORLDWIDE oat tb lie ua Line Graph Pie Plot
  12. Customization of Plot( ) import matplotlib.pyplot as plt plt.plot(x, y, color='green', linestyle='dashed', linewidth = 3, marker='o', markerfacecolor='blue', markersize=12) x- plt.ylim(l , 8) plt.xlim(1,8) plt.xlabel('x - axis') plt.ylabel('y - axis') plt.title('customizations ! ') plt.show() 3 Customizations! axis
  13. What is legend? The legend of a graph reflects the data displayed in the graph's Y-axis, also called the graph series. With a chart, a legend is an area of a chart describing each of the parts of the chart. A Legend is a representation of legend keys entries the plotted or on area of chart or graph which are linked to the data table of the chart or graph.
  14. import matplotlib.pyplot as plt state active = ['Gujarat', 'Delhi', 'Rajasthan'] recovery = plt.plot(state, active, color='red', linestyle='solid', linewidth = 3, marker='o', markerfacecolor='red', markersize=12 ) plt.plot(state, recovery, color=' green', linestyle='dashed', linewidth = 3, marker='*', markerfacecolor='blue', markersize=12) plt.xlabel('State') plt.ylabel('Number of people') plt.title('COVID-19 State wise Status') plt.legend(['Active Case', 'Recovery']) plt.grid(True, color='blue') ax=plt.axes() ax.set_facecolor("k") plt.show()
  15. 114120 sn a 6F0 00 0 0 0006 00001
  16. import matplotlib.pyplot as plt langs = 'C++', 'Java', 'Python', 'PHP'] students plt.bar(langs, students, color = ['red', 'green', ' blue', 'magenta', 'orange'] ) plt.xlabel('Languages') plt.ylabel('Number of Student') plt.title('Admission Info in each course') plt.show() Bar Chart Admission Info in each course 35 30 25 20 15 10 5 c Java Languages Python
  17. import matplotlib.pyplot as plt # frequencies ages= 32,21, 20,] # setting the ranges and no. of intervals range = (0, 100) bins = 10 # plotting a histogram plt.hist(ages, bins, range, color = 'green', histtype = 'bar', rwidth=0.8) plt.xlabel('age') My histogram plt.ylabel('No. of people') 7 plt.title('My histogram') plt.show() Histogram 6 5 .2 1 20 40 80 100 age
  18. Difference between Bar Graph & Histogram 45 40 35 30 15 10 5 0 14 25 20 Class 5 Bar Graph 1 unit length = 5 girls Class 6 Class 7 Class 8 Classes ................................+ 3 o z x' Class 9 5 3 2 1 o Histogram 5 10 15 20 Marks obtained 25 x 30 In Bar Graph • Bars have equal space • On the y-axis, we have numbers & on the x-axis, we have data which can be anything. In Histogram • Bars are fixed • On the y-axis, we have numbers & on the x-axis, we have data which is continuous & will always be number.
  19. import matplotlib.pyplot as plt Scatter Diagram # x-axis values # y-axis values # plotting points as a scatter plot x- plt.scatter(x, y, label= "Stars", color= "red", marker= s=80) My scatter plot! # x-axis label plt.xlabel('x - axis') # frequency label plt.ylabel('y - axis') # plot title plt.title('My scatter plot! ') # showing legend plt.legend() # function to show the plot plt.show() 12 10 8 6 4 2 * Rars 2 4 6 axis 8 10
  20. import matplotlib.pyplot as plt activities = ['eat', 'sleep', 'work', 'play'] # defining labels slices # portion covered by each label colors # color for each label # plotting the pie chart plt.pie(slices, labels = activities, colors=colors, startangle=90, shadow = True, autopct = 1. plt.legend() plt.show() Pie Chart eat 29.2% sleep eat sleep Tv,ork 33.3% work
  21. Assignment 1: 1. 2. A bar chart is drawn(using pyplot) to represent sales data of various models of cars, for a month. Write appropriate statements in Python to provide labels Month - June and Sale done to x and y axis respectively. Write a code to plot a bar chart to depict the pass percentage of students in CBSE exams for the years 2015 to 2018 as shown below 100 90 80 '70 30 20 10 2016 2017 2018
  22. Assignment 2: Give the output from the given python code: import matplotlib.pyplot as plt import numpy as np objects Y —Pos performance = ('Python', 'C++', 'Java', 'Perl', 'Scala', 'Lisp') = np.arange(len(objects)) plt.bar(y_pos, performance, align='center', alpha=0.5) plt.xticks(y_pos, objects) plt.ylabel('Usage') plt.title('Programming language usage') plt.show()