You are on page 1of 8
Data Visualization When data is shown in the form of pictures, it becomes easy for the user to understand it, Representing the data in the form of pictures or graphs is called ‘data visualization’ For this purpose, we can use pyplot submodule of the matplotlib module. Installation of matplotlib is already discussed in Chapter 2. For complete information about this module, you can refer to the page: https: //matplotlib .org/api/pyplot-sunmary. htm] Bar Graph A bar graph represents data in the form of vertical or horizontal bars. It is useful to compare the quantities. Now, let us see how to create a bar graph with employee id numbers on X-axis and their salaries on Y-axis. We will take this data from the data frame. First of all, we should import the packages as: jmpore matplotlib-pyplot/as ple /# for drawing the graph ‘import pandas as pd. # for creating the data frame ‘Suppose the data frame is created from ‘empdata’ as: f= pd:batarrameCenpdata) This data frame ‘df contains various pieces of data like empid, ename, sal and doj. We want to take only the ‘empid’ and ‘sal’ for the purpose of drawing the graph. This is done as: x= dfprenpia'] y = dff'sal'] We can draw the bar graph by calling bar() function of pyplot module python bar.py MICROSOFT INC ‘ples salons Employee is From the above output, we can infer that the employee with id number 1002 is drawing the highest salary and next to him the employee with id number 1003. We can create bar graphs from more than one data set that are coming from multiple data frames. For example, we can plot the empid and salaries for 2 departments: Sales team and Production team. For this purpose, we can call the bar() function two times as: pltvbar Gyyylabel="sales dept", “colors'red") plt.barGd, yl, label='Production dept', color="green') Program 2: A program to display employee id numbers on X-axis and their salaries on Y- axis in the form of a bar graph for two departments of a company. # bar graph with ids, salaries for’ two depts import matplotlib.pyplot as pit # rake employee ids and sataries for sales dept x = [1001, 1003, 1006, 1007, 1009, 1011} y = (10000, 23000.50,'18000'33, 16500.50, 12000.75, 9999.99] # take employes ids and salaries for Production dept x1 = [1002, 1004, 1010, 1008, 1014, 1015] yl = [5000, 6000, 4500.00, 12000, 9000, 10000] # create bar graphs pit-barCey, label='sales dept! color="red’) plt-bar(xd, yl, label="Production dept’, color="green') # set the labels and legend pit.xlabel (‘emp id") Scanned by CamScanner Data Science Using Python] 709 bel (*salaries') oleate USurcnosort INc') pit.TegendO) display the bar graph pre-show output: c:\> "python bars py MICROSOFT INC | aoe le fae eto From the above output, we can understand that the Sal les department penple are drawing more salaries when compared to those of the Productio: in department. Histogram Histogram shows distributions of values. Histo; Useful to show values grouped in bins or interval: ach employee in a company and show it in the form ofa histogram to know how mary employees are there in the range of 0-10 years, 10-20 years, ete. For this purpose, we should take the employee ages and bins (or intervals) as: pages 224553059, 5! 16, 5745543543, 50, 40534) 33525519) Bins 2" f0r10°36 30,40, 50,60] | #'of 10'years? range The bins list contains 0,10,20... This indicates the ranges from 0 to 9, 10 to 19, etc., excluding the outer limit. To draw the histogram, we should use hist() function as: gram is similar to bar graph but it is s. For example, we can collect the age of bit-hist (enplages,!ibinsyhisttype='bat! rwidth=0:8, colors*cyan') Here, histtype is ‘bar’ to show the histogram in the form of bars. The other types can be arstacked!, ‘step’, ‘stepfilled’. The option rwidth’= 0.8 indicates that the bar's width is 80%. There will be a Bap of 10% space before and after the bar. If this is decteased, then ‘the bar's width will be narrowed. Scanned by CamScanner Program 3: A program to display a histogram showing the number of employees in specific age groups. # histogram of employee ages import matplotlib.pyplot as plt # take individual employee ages and range of ages emp_ages = [22,45,30,59)58,56,57,45,43, §,80,40, 34, 33,25,191 bins = [0,10,20,30,40,50,60) # create histogram of bar type 7 pItuhistCenp_ages, bins, hisetypes'bar', rwidth=0. feet 11 = plt.xlabel ("employee ages’ Plt ylabel(*no, of employees") plt.titleC*oRACLe corp"). pit. Tegend@ # draw the histogram plt.showQ) Output: C:\> python histo.py ORACLE CoRP ‘no, of employees. 0 0 20 30 40 employee ages #€1>/) tals) Scanned by CamScanner creating 4 Pie Chart shows a circle is divi a pie chart s! ‘cle that is divided into ae sectors and each sector repres 7 pti of ts me For example, we can take different departments in 2 company and nel em yecs. . uppose, there are 4 departments and their employees are in the wie gt » 20%, 15% and 15%, These can be represented as slices in a pie chart. qo create a pie chart, we can use pie() function as: pltpie (slices, labels=dept: a Soe eccentrics ee, Seo startangle=90, explode=(0, Here, labels=depts indicates a list of labels. colors-cols indicates a list of colors. startangle-90 indicates that the pie chart will start at 90 degrees (12 o'clock position). If this option is not mentioned, then it starts by default at 0 degrees (3 o'clock position). ‘the option explode=(0,0.2, 0,0) indicates whether the slices in the pie chart should stand out or not, Here, 0 indicates that the corresponding slide will not come out of the chart. But the next 0.2 indicates that the second slice should come out slightly. Its values 0.1 to 1 indicate how much it should come out of the chart. ‘The option shadow=True indicates that the pie chart should be displayed with a shadow. ‘This will improve the look of the chart. The option autopct=".11%6' indicates how to display the percentages on the slices. Here, %.1 indicates that the percentage value should be displayed with 1 digit after decimal point. The next two % symbols indicate that only one symbol is to be displayed. Program 4: A program to display a pie chart showing the percentage of employees in each department of a company. + to display enployees of different depts’ in pie chart Faport matplotlib.pypiot as Plt # take the percentages of employees of 4 departments Slices = [50, 20, ie 15] # take the departments names 14 ‘ depts - ['sales', ‘Production, "HR's Finance] | # take the colors for each department, . cols = ['magenta’, ‘cyan’, ‘brown’, gold") # create a pie chart 0.2 Pit eietstives, labels-depts, colorsscols: startangle=90, .2, 0, 0), shadow=true, autopct="%.1f%6" ) explode=(0, Scanned by CamScanner # set titles and legend Plt. titleC*wzpro') pit. TegendQ) # show the pie chart pit. show() Output: €:\> python pie.py ‘The graph clearly shows that 50% of the employees in the company are from the Sales department. Also, we want to grab the focus on Production department. Hence, that slice is dragged out. Creating Line Graph ‘A line graph is a simple graph that shows the results in the form of lines. To create a line graph, we need x and y coordinates. For example, plt.plot(x, y, "colorname") Here, the plot() function creates the line graph. x and y represent lists of coordinates for X- and Y-axes. ‘colorname’ indicates the color name to be used for the line. Program 5: A program to create a line graph to show the profits of a company in various years. “# to uyselay profits of a eonpany; year-wise ‘import matplotlib.pyplot as pit Scanned by CamScanner pclence using rytnon "2015 : 017") , 9.75] 2012', ‘201: 2014 yeaties = (9, 10, 10.5,'8.8, 10! if te the line graph fieplot(years, profits, ‘blue") # set title-and labels pit.title('xvZ COMPANY") plt-xlabel('vears') © pit.ylabel('Profits in Million Rs*) # show the line chart pit. show Output ci\> python’ Tine: py XYZ COMPANY Profits in Milion Rs zoe 20132014201 SCOTS Al €|>|| $/Q/=|! 3) From the above output, we can understand that the profits of the i company are the highest during the year 2016 and the least during the year 2015, Scanned by CamScanner

You might also like