Figures and Traces Video Lecture Transcript This transcript was automatically generated by Zoom, so there may be discrepancies between the video and the text. 15:51:50 Hi! Everybody! Welcome back! And then this video, we're gonna actually dive into learning more about Plotley by talking about the base objects that are used when creating a plot leaf figure so in particular, we'll introduce the figure class. 15:52:05 We'll demonstrate how you can create a chart on a figure, and then talk about some of the syntax and verbiage involved with making plot leaf figures. 15:52:15 So, just like with Matt plat. Lib. Plotley has its own figure, graph object, so graph object is a module within plot, lead that contains all the things that we're going to use to most of the things that we're gonna use to make our figures and plots so figures have sort 15:52:32 of a tree structure with 2 main branches being the data and the layout. 15:52:38 So the data is gonna contain everything about the data you're plotting, including how you're going to plan it. 15:52:45 So, for instance, it might have the horror. It will have the horizontal and vertical data. 15:52:49 And then, if you're making a scatter plot, it will have information about like the displaying of the scatterplot figure also has a layout which controls things like the background. 15:52:59 The grid marks the ticks, the titles, and so forth, and we'll dive more into both of these as we continue on with our our lectures in this section 15:53:08 So we're going to go ahead and make a figure, object by hand, just to demonstrate in order to do so, you need to first import the graph object. 15:53:16 The figure object from graph objects, so from bluntly sorry. 15:53:23 We first need to import graphic graph objects, so import Plotley, dot graph objects as. 15:53:33 And then, if you read the documentation from Plotley, it's standard to import it as go, and then, once you've imported graph objects as a module, you can go ahead and make a figure object by doing go dot figure. 15:53:47 Okay, so here's a blank figure object, and we'll learn what about how to add stuff and then how to control the axes that gets displayed, etc. 15:53:58 So let's go ahead and show you what a figure object looks like in Python. 15:54:04 So if we produce, print out the figure, object, and don't try and display it, or you'll see that we have what looks like a dictionary. 15:54:12 So, the first thing being noticing that since this is an empty figure, the data key has a value of an empty string or an empty list, because there is no data but the layout has the entry that is itself a dictionary. 15:54:27 So, for instance, we could, we could look at the layout just like we would. 15:54:36 Another entry of a dictionary, and then we'll notice that. 15:54:39 Well the layout has, for instance, something called a template as a key, so we could look at the template like so. 15:54:46 And now we see this template is itself sort of contained like a dictionary, and has all this various information about how things should be displayed by default. 15:54:57 Okay, so that's just showing us how we can access all the different elements of this branching. You know. 15:55:05 Figure as the layout or the data. So why don't we go ahead now and see how we can add data and add graph objects to a figure and plot a graph object like a scatter point? 15:55:21 Marker or a line, and the data associated with it is known as a trace. 15:55:25 So when we would make a scatter plot, we'll be saying that we'll be adding a scatter trace to our figure. 15:55:31 That's just standard plot. Leave verbiage. 15:55:35 So we're gonna use this data set from plot League called stocks. 15:55:39 And looking at the first 5 rows of stock stocks, has a date column, along with various stock closing prices that I think have been scaled in reference to this first day. 15:55:54 So this first day is a one, and then every day after that, for each of the columns is going to be like a multiple of its price relative closing price on that trading day relative to the first day. 15:56:07 So the stocks are Google Apple, Amazon, Facebook, or now Meta, Netflix and Microsoft. 15:56:16 Okay, so we're gonna go ahead and don't worry. 15:56:18 We're gonna guide. We're gonna dive more in detail with these, and later notebooks. 15:56:23 But we're gonna add a trace to our figure. 15:56:25 So we first create a fit. Okay? So we call go dot figure. 15:56:30 And if we'd like to add traces to it, we have to store it as a variable. 15:56:35 So I'm going to start in the variable fig. 15:56:37 Then, when you want to add a trace, you can call, add underscore trace, and then you import the trace that you'd like to add. 15:56:44 So for us that trace is going to be. I know we're making a line plot, but the line plots can actually be made. 15:56:51 Was the scatter, trace, and you just set the mode equal to lines. 15:56:55 Now this might be confusing, because it's a lot of like. 15:56:57 Well, wait a minute. You're introducing both. Scatter and add trace. 15:57:01 This is just to demonstrate, adding a trace. 15:57:03 We're gonna see more in depth. These commands as we go along in future notebooks. 15:57:08 Okay. So here I'm adding a trace that will plot the apple stock price. 15:57:13 And I'm adding a trace that will plot the Microsoft stock price, and then, when you're all done, adding things to your figure, you call fig dot show, and that will display the figure. 15:57:25 Okay. So here's my red line, which is the Microsoft. 15:57:28 Stock price. Here's my blue line, which is the apple stock price. 15:57:33 So let's maybe talk about 2 things real quickly. 15:57:38 You'll notice that plot lead charts by default have nice hover interactivity, meaning as I hover my mouse over the different points, information pops up like the dates, February the eleventh, 2,019 and then it's kind of hard to see but if you Zoom 15:57:54 in the value to the next of that is the vertical value which would be the closing stock price relative to the initial day. 15:58:02 Okay, so that's a nice hover feature. We're gonna learn more about hovering in a later notebook and how you can control what's displayed by default. 15:58:11 It's just sort of the data that you've provided. 15:58:14 And so how can I access the data? Well, let's look at that figure object. 15:58:18 We've made fig now that it's not an empty figure object. 15:58:21 You can see the data branch or the data key in the dictionary has values, hey? 15:58:29 So, for instance, we could access if we wanted to. We could do fig at data which we see is a dictionary. 15:58:41 And so we could do figured data at X, and we may have to do that. 0. 15:58:46 Because I think it's a technically a tuple. 15:58:48 So if we go back we can see it by this parentheses that it's a tuple. 15:58:52 So if we and access the 0 entry of the tuple, which will give us the scatter plot, we can look at, for instance, at the data that's plotted on the x-axis. 15:59:03 So this leads nicely into the next decide. You'll notice that these are are all strings, but when we look at the plot it was able to take the strings and then convert them into the appropriate date times. 15:59:17 So plotly interacts nicely with date times and strings of date times. 15:59:23 Assuming that those strings are formatted, well meaning in a standard format that people can recognize, I think it's I'm not even gonna try and guess what the proper format name is. But you can look it up. 15:59:33 So, for instance, these are year, month, day, and if you're in that format, Botley is able to read it in as a string, and then convert it to the date time that it is, but you'll still just like with Matt plot lib. 15:59:46 You need to enter it in the appropriate order that you want it plotted. 15:59:48 So if you were, for instance, put all the months, the the days in October first. 15:59:54 That would mess up the plotting. Okay. 15:59:58 So we're not gonna touch too much about how to change either the layout or the appearance of the lines themselves in this notebook. 16:00:07 That's for a later notebook. I just wanted to point out. 16:00:10 Don't worry, we're going to dive more into both. 16:00:12 How do I change the appearance of my figure as a layout? 16:00:15 And how do I change the appearance of my traces? 16:00:18 We're gonna touch on that in later notebooks. 16:00:20 But for now we've introduced figure objects we've introduced how to add a trace, and we've learned the the verbiage or the the language of plot. 16:00:30 We both with figure objects, and traces, so in the next notebook we'll start to dive in a little bit more into what plot we can do, and I hope to see you there.