1 00:00:00,066 --> 00:00:00,799 2 00:00:00,800 --> 00:00:06,933 Hello and welcome to a video on line graphs. Very often when working with data, we will want to visualize this data 3 00:00:06,933 --> 00:00:06,966 4 00:00:06,966 --> 00:00:13,232 before doing anything further to it. The reasons for this are multitude. Perhaps we want to know if our data has any outliers 5 00:00:13,233 --> 00:00:13,266 6 00:00:13,266 --> 00:00:18,999 in it or with the shape of our distribution is. Whatever our reasons, there are lots of ways to visualize our data. 7 00:00:19,000 --> 00:00:19,300 8 00:00:19,300 --> 00:00:25,466 Let's try a simple line graph today. These are useful when you want to highlight changes over time in your data, such 9 00:00:25,466 --> 00:00:31,466 as sales numbers. Let's pretend we're working for a furniture sales company and they're looking at 10 00:00:31,466 --> 00:00:36,532 last year's numbers. To do this, we'll use map plot lib. We'll generate some sample data. 11 00:00:36,533 --> 00:00:40,133 12 00:00:40,133 --> 00:00:46,199 So we're going to import numpy as np. And this is just to generate the sample data down here. And we're going 13 00:00:46,200 --> 00:00:51,200 to import mapplotlib.pyplot as PLT. And that's to actually create our plot below. 14 00:00:51,200 --> 00:00:52,333 15 00:00:52,333 --> 00:00:58,499 Let's run this code. And this generated our months 1 through 12 January 16 00:00:58,500 --> 00:00:58,633 17 00:00:58,633 --> 00:01:04,699 through December and our sales, which are randomly generated numbers between a certain 18 00:01:04,700 --> 00:01:05,166 19 00:01:05,166 --> 00:01:11,166 value and another certain value. If you're curious about this code, feel free to play with it and run it on your 20 00:01:11,166 --> 00:01:17,332 own. Next, let's make our plot. Notice there are many things that 21 00:01:17,333 --> 00:01:17,366 22 00:01:17,366 --> 00:01:23,132 we can do to a line graph to change the color of the line, label or data, change the titles, et cetera. First of 23 00:01:23,133 --> 00:01:23,933 24 00:01:23,933 --> 00:01:30,099 all, we're going to set our figure size. This isn't really necessary. You can actually take this line out if you want to, 25 00:01:30,100 --> 00:01:30,466 26 00:01:30,466 --> 00:01:36,532 but we want our plot to be nice and big this time. PLT.plot actually creates the 27 00:01:36,533 --> 00:01:42,599 plot and changes and let's it know what we want for our x-value, what we want for our y-value, which in 28 00:01:42,600 --> 00:01:47,166 this case we have to find above. So x is this, why is this? 29 00:01:47,166 --> 00:01:48,766 30 00:01:48,766 --> 00:01:54,166 Our marker style is set here, our line style is set here. We have our color set to blue, 31 00:01:54,166 --> 00:01:54,932 32 00:01:54,933 --> 00:02:00,966 label equals sales trend. We've also titled our plot, given it 33 00:02:00,966 --> 00:02:00,999 34 00:02:01,000 --> 00:02:07,133 x labels, y labels, and given it ticks. The ticks are the things at the bottom 35 00:02:07,133 --> 00:02:07,166 36 00:02:07,166 --> 00:02:11,766 that tell you what your y-ax or your x-axis is. 37 00:02:11,766 --> 00:02:13,466 38 00:02:13,466 --> 00:02:19,566 Then we also have our legend and we'll set our grid to true for this. So I'll run 39 00:02:19,566 --> 00:02:25,632 this plot and you can see now we have monthly sales over a year and 40 00:02:25,633 --> 00:02:26,599 41 00:02:26,600 --> 00:02:32,600 we started pretty poorly in month one and just generally grew all the way up to month 12 42 00:02:32,600 --> 00:02:33,133 43 00:02:33,133 --> 00:02:39,466 which I mean isn't that what you want to see. Our pretend company is doing very well in their first year. 44 00:02:39,466 --> 00:02:43,932 45 00:02:43,933 --> 00:02:49,233 If you wanted to use data from a pandas data frame you would simply change the x and y to the values you want to use. 46 00:02:49,233 --> 00:02:49,999 47 00:02:50,000 --> 00:02:56,033 In data visualization we are primarily concerned with how easy the information is to read. However we may also 48 00:02:56,033 --> 00:02:56,066 49 00:02:56,066 --> 00:03:02,166 have company guidelines to follow. Let's say we work for a company our furniture company who wants to produce 50 00:03:02,166 --> 00:03:08,366 this graph to certain specifications. To improve readability on this plot we've rotated the month labels 45 51 00:03:08,366 --> 00:03:08,399 52 00:03:08,400 --> 00:03:14,100 degrees, changed the color of the line for aesthetic reasons and remove the vertical red lines. We've also changed 53 00:03:14,100 --> 00:03:14,600 54 00:03:14,600 --> 00:03:18,900 the points of the labels because this is the style our pretend company has asked for. 55 00:03:18,900 --> 00:03:20,633 56 00:03:20,633 --> 00:03:26,833 So we've changed a couple of things here, x and y are still the same. We've left the figure size the same but this will make 57 00:03:26,833 --> 00:03:32,966 our points pixels instead of those circles. Our line is still a solid line. We've changed the 58 00:03:32,966 --> 00:03:39,199 color to forest green and down here in our x-ticks we've added 59 00:03:39,200 --> 00:03:45,000 an argument to change the rotation of the ticks by 45 degrees. 60 00:03:45,000 --> 00:03:48,466 61 00:03:48,466 --> 00:03:53,832 And now here under plt.grid we've selected axis = y 62 00:03:53,833 --> 00:03:55,599 63 00:03:55,600 --> 00:03:57,733 so it's only going to use the y-axis. Okay we'll 64 00:03:57,733 --> 00:04:02,699 65 00:04:02,700 --> 00:04:08,900 go ahead and run that and this is the style that our company 66 00:04:08,900 --> 00:04:14,900 has asked for. You can find lists for different 67 00:04:14,900 --> 00:04:14,933 68 00:04:14,933 --> 00:04:21,199 options you have for plots by Googling matplotlib.pyplot options. This will bring up lists of different markers, 69 00:04:21,200 --> 00:04:21,233 70 00:04:21,233 --> 00:04:28,799 colors, et cetera. An excellent resource for matte-plot-lib is their website, https://matplotlib.org/ 71 00:04:28,800 --> 00:04:29,533 72 00:04:29,533 --> 00:04:33,666 Feel free to take a look there and see what customization options are available. Let's go there now. 73 00:04:33,666 --> 00:04:40,399 74 00:04:40,400 --> 00:04:45,633 You can see here we've got our documentation. 75 00:04:45,633 --> 00:04:46,599 76 00:04:46,600 --> 00:04:52,700 It'll tell you how to install matte-plot-lib, give you API references, 77 00:04:52,700 --> 00:04:52,733 78 00:04:52,733 --> 00:04:58,899 and then it'll give you tutorials, user guides, 79 00:04:58,900 --> 00:04:58,933 80 00:04:58,933 --> 00:05:03,266 examples, references, all that good stuff, plot types, 81 00:05:03,266 --> 00:05:06,899 82 00:05:06,900 --> 00:05:13,033 so there's a lot to explore here. All right, have fun making your first line graphs. 83 00:05:13,033 --> 00:05:14,533 and we'll see you in the next video.