1 00:00:00,000 --> 00:00:03,118 Great. Now you know what Bokeh is, 2 00:00:03,119 --> 00:00:04,889 because I explained that in the previous 3 00:00:04,919 --> 00:00:07,259 lecture. And in this lecture, I'll show you 4 00:00:07,259 --> 00:00:09,539 how to build a very basic invoking 5 00:00:09,539 --> 00:00:12,299 graph. And yeah, so we'll start from 6 00:00:12,299 --> 00:00:15,429 scratch as the bus, I will improve that graph. 7 00:00:15,430 --> 00:00:17,552 [No audio] 8 00:00:17,554 --> 00:00:21,014 Yeah, let's say this is your first Bokeh graph, 9 00:00:21,015 --> 00:00:23,699 or maybe you have built Boken graphs 10 00:00:23,699 --> 00:00:26,579 before. So before, taking this course, 11 00:00:26,579 --> 00:00:28,139 and you know of few things already, 12 00:00:28,409 --> 00:00:30,629 still, I would suggest you start from 13 00:00:30,629 --> 00:00:33,089 here, because I'll show you the correct 14 00:00:33,119 --> 00:00:36,449 logic towards building graphs step by 15 00:00:36,449 --> 00:00:38,759 step. And now let's go ahead and do 16 00:00:38,759 --> 00:00:43,589 this, I'll be using some times I will be 17 00:00:43,589 --> 00:00:46,769 using Jupyter Notebook to write Python 18 00:00:46,769 --> 00:00:49,319 code and in other times, I'll be using 19 00:00:49,319 --> 00:00:52,259 the Atom editor. So the Jupyter Notebook 20 00:00:52,289 --> 00:00:54,149 is a good place to start actually, 21 00:00:54,689 --> 00:00:57,719 because it will allow us to write some 22 00:00:57,719 --> 00:01:00,119 interactive code, we can just write a 23 00:01:00,119 --> 00:01:01,859 single line of code and see what it 24 00:01:01,859 --> 00:01:05,279 does. So that's great for, starting of 25 00:01:05,279 --> 00:01:07,349 with a library. So you can explore 26 00:01:07,349 --> 00:01:10,019 the library by testing small pieces of 27 00:01:10,019 --> 00:01:15,569 code, and yeah. With Shift and Right Click 28 00:01:15,569 --> 00:01:19,499 on Windows over folder, you can go over 29 00:01:19,499 --> 00:01:22,349 here, Open a command window here. And 30 00:01:22,589 --> 00:01:24,869 this will open a command prompt in the 31 00:01:24,869 --> 00:01:28,289 current folder. So on a Mac or Linux, 32 00:01:28,769 --> 00:01:31,199 you can maybe do something like open 33 00:01:31,229 --> 00:01:34,409 terminal here, or you can open a 34 00:01:34,409 --> 00:01:36,749 terminal normally, and then you can cd 35 00:01:36,779 --> 00:01:40,529 to the path, to your working path. So 36 00:01:40,529 --> 00:01:42,359 I'll be writing the code in these 37 00:01:42,359 --> 00:01:46,979 directory. And to install Jupyter, 38 00:01:47,009 --> 00:01:48,671 you would say pip install 39 00:01:50,628 --> 00:01:55,304 jupyter notebook. And this is a requirement 40 00:01:55,319 --> 00:01:56,909 already satisfied, because I've already 41 00:01:56,909 --> 00:01:58,919 installed Jupyter Notebook. If you 42 00:01:58,919 --> 00:02:01,109 haven't, you can just go ahead and do 43 00:02:01,109 --> 00:02:03,119 this and, you know, it will be 44 00:02:03,119 --> 00:02:06,269 installed for you. And now to start 45 00:02:06,299 --> 00:02:07,903 Jupyter Notebooks you'd say 46 00:02:07,904 --> 00:02:10,075 [No audio] 47 00:02:10,077 --> 00:02:13,829 jupyter notebook, just like that. And that will 48 00:02:13,829 --> 00:02:16,949 open a Jupyter Notebook on localhost. So 49 00:02:16,979 --> 00:02:21,419 it's like a service based notebook. And 50 00:02:21,419 --> 00:02:23,969 yeah, if you create now you create a 51 00:02:23,969 --> 00:02:25,406 Python 3 notebook 52 00:02:25,407 --> 00:02:27,881 [No audio] 53 00:02:27,883 --> 00:02:33,180 and name it Basic graph, and just like that. 54 00:02:33,181 --> 00:02:35,492 [No audio] 55 00:02:35,494 --> 00:02:40,634 In the demo folder, you will have this Basic graph.ipynb 56 00:02:40,635 --> 00:02:43,711 which stands for IPython Notebook, but now the 57 00:02:43,712 --> 00:02:46,169 IPython Notebook is renamed to Jupyter 58 00:02:46,169 --> 00:02:48,029 Notebook, but the extension still 59 00:02:48,029 --> 00:02:50,519 stays the same. And you also have this 60 00:02:50,519 --> 00:02:52,019 checkpoints, which are false 61 00:02:52,049 --> 00:02:55,379 automatically produced when Jupyter 62 00:02:55,379 --> 00:02:58,349 saves your sessions. Now you should keep 63 00:02:58,349 --> 00:03:02,399 your command prompt open. And then this 64 00:03:02,399 --> 00:03:05,639 is where you write your code. And if you 65 00:03:05,639 --> 00:03:07,679 don't prefer to use Jupyter Notebooks, 66 00:03:07,679 --> 00:03:12,269 you can use your favorite editor. So 67 00:03:12,269 --> 00:03:16,619 that should be Atom, or Vim, or the 68 00:03:16,619 --> 00:03:20,459 default Python IDE, and so on, it 69 00:03:20,459 --> 00:03:23,579 doesn't matter much. And I'll be using 70 00:03:23,579 --> 00:03:26,609 commands to command my code, and that 71 00:03:26,609 --> 00:03:28,409 will help you read and understand my 72 00:03:28,409 --> 00:03:31,709 code easier. Because you know, I'll be 73 00:03:31,709 --> 00:03:33,659 writing some code here, and some code 74 00:03:33,659 --> 00:03:36,869 and later on like that. And I'll be 75 00:03:36,869 --> 00:03:39,449 explaining the code, so as I type it in, 76 00:03:40,199 --> 00:03:42,209 but sometimes you forget what I typed 77 00:03:42,209 --> 00:03:44,609 here and you're struggling to understand 78 00:03:44,609 --> 00:03:47,579 it. But if I have some commands here, 79 00:03:47,879 --> 00:03:53,189 let's say, plotting the x and y values. 80 00:03:53,849 --> 00:03:55,859 And if I write this command here, Python 81 00:03:56,189 --> 00:03:58,559 will not read it as code. So it will 82 00:03:58,559 --> 00:04:01,769 ignore it, but you can read it. And you 83 00:04:01,769 --> 00:04:04,289 will assume that I'm writing this 84 00:04:04,679 --> 00:04:09,149 command for the next block. So I write 85 00:04:09,149 --> 00:04:10,889 the command, I leave no space, and I 86 00:04:10,919 --> 00:04:15,089 type the code there. And then I leave a 87 00:04:15,089 --> 00:04:16,949 space there, and I typed some other code 88 00:04:16,949 --> 00:04:18,629 there, so it's another block that 89 00:04:18,629 --> 00:04:20,759 doesn't have to do with this command. So 90 00:04:20,759 --> 00:04:22,949 this command will be about this block of 91 00:04:22,949 --> 00:04:25,889 code there. Sometimes I may want to 92 00:04:26,489 --> 00:04:29,399 apply specific commands about specific 93 00:04:29,399 --> 00:04:30,985 lines, for example, this one here, 94 00:04:31,467 --> 00:04:33,616 in that case, I will write the command just there. 95 00:04:34,687 --> 00:04:38,007 So let's say for the x values 96 00:04:38,009 --> 00:04:42,929 here, or something like that. Great. And 97 00:04:43,349 --> 00:04:45,689 the first command will usually be above 98 00:04:45,689 --> 00:04:48,119 the script. So what we'll be doing is 99 00:04:48,149 --> 00:04:53,219 making a basic Bokeh graph. This will 100 00:04:53,219 --> 00:04:56,069 be a line graph, right. And I'll leave a 101 00:04:56,069 --> 00:04:58,289 space there and here I start the code. 102 00:04:59,999 --> 00:05:02,009 So, first of all, I'll be importing 103 00:05:02,010 --> 00:05:06,747 [Author typing] 104 00:05:06,749 --> 00:05:10,859 Bokeh. And I'll be explaining this 105 00:05:10,889 --> 00:05:14,699 later, but Bokeh has a few interfaces. 106 00:05:14,700 --> 00:05:16,827 [No audio] 107 00:05:16,829 --> 00:05:20,265 The first interface is bokeh.plotting. 108 00:05:20,620 --> 00:05:23,253 So it's called the bokeh.plotting interface. 109 00:05:23,495 --> 00:05:25,919 And I'll be going through the interfaces later. 110 00:05:25,920 --> 00:05:29,789 So for now let's focus on how a graph looks like 111 00:05:29,819 --> 00:05:33,089 and how the code looks like to build a 112 00:05:33,089 --> 00:05:36,599 Bokeh graph. So for, from bokeh.plotting 113 00:05:36,629 --> 00:05:39,509 imports, import the figure object, 114 00:05:39,539 --> 00:05:42,329 which is the most important object, and 115 00:05:42,359 --> 00:05:45,089 Bokeh because this will contain like 116 00:05:45,089 --> 00:05:48,209 the, like a container where you add 117 00:05:48,749 --> 00:05:50,699 points, you add lines, and you add the 118 00:05:50,699 --> 00:05:53,159 labels for the points, and you add grades 119 00:05:53,159 --> 00:05:56,819 for the plot, and legends, and 120 00:05:56,819 --> 00:05:58,889 everything. So you'll add everything 121 00:05:58,919 --> 00:06:03,809 into this figure object. That's it. And 122 00:06:04,019 --> 00:06:07,769 then you need to have some helpful tools 123 00:06:07,769 --> 00:06:11,729 from Bokeh that comes from bokeh.io 124 00:06:11,729 --> 00:06:15,179 interface. So from that you want to 125 00:06:15,179 --> 00:06:21,179 import output_file and show, and that's 126 00:06:21,179 --> 00:06:24,239 basically what we need, you'll see what 127 00:06:24,269 --> 00:06:27,809 all these are in just a bit. And then we 128 00:06:27,809 --> 00:06:29,669 need to create some data. So let's 129 00:06:29,669 --> 00:06:30,965 create some fake data there. 130 00:06:30,966 --> 00:06:35,278 [No audio] 131 00:06:35,280 --> 00:06:38,109 Prepare some data, I'll be using lists. 132 00:06:38,110 --> 00:06:40,509 [No audio] 133 00:06:40,511 --> 00:06:43,867 Let's say 1, 2, 3, 4, and 5. 134 00:06:44,078 --> 00:06:47,099 So that's for the x axis. So in 135 00:06:47,099 --> 00:06:48,929 the plot, these values will be mapped 136 00:06:48,959 --> 00:06:52,467 along the x axis. And then we have a y axis there. 137 00:06:54,100 --> 00:06:59,189 Let's say 6, 7, 8, 9, 10. A note that 138 00:06:59,549 --> 00:07:02,759 this list have to have the same length, 139 00:07:02,789 --> 00:07:04,679 so in order to pull them in Bokeh. 140 00:07:05,406 --> 00:07:08,076 Otherwise, Bokeh will throw an error, 141 00:07:08,279 --> 00:07:12,059 it will say that, you know it, Bokeh will start 142 00:07:12,059 --> 00:07:15,149 to take pairs of this list. It will 143 00:07:15,149 --> 00:07:17,459 take 1 and 6 and it will map these 144 00:07:17,489 --> 00:07:20,219 along the x and the y axis. So it will 145 00:07:20,219 --> 00:07:22,319 create a point with coordinates 1 and 146 00:07:22,319 --> 00:07:24,299 6, and then 2, and 7, and so on, 147 00:07:24,921 --> 00:07:28,581 and goes through 5 and 10. And then if 148 00:07:28,589 --> 00:07:33,629 you add a line number here, say 7, 149 00:07:34,979 --> 00:07:37,439 or whatever, Python will say, I 150 00:07:37,439 --> 00:07:40,329 didn't find a pair for 7 in the y axis. So 151 00:07:40,330 --> 00:07:44,110 you'll get an error. Great. So we have the data 152 00:07:44,111 --> 00:07:47,354 [No audio] 153 00:07:47,356 --> 00:07:52,118 prepare the output file. 154 00:07:53,238 --> 00:07:55,289 So here is where you use the 155 00:07:55,289 --> 00:07:59,561 output_file function and there you pass 156 00:07:59,563 --> 00:08:01,583 [No audio] 157 00:08:01,585 --> 00:08:05,159 other name of the HTML file that 158 00:08:05,159 --> 00:08:07,649 will be created. So where you can choose 159 00:08:07,649 --> 00:08:10,049 every name you want. And then the next 160 00:08:10,049 --> 00:08:12,539 thing to do is you want to create a figure 161 00:08:12,539 --> 00:08:15,449 object instance. So you have figure 162 00:08:15,449 --> 00:08:18,209 class there, you have imported it, and 163 00:08:18,209 --> 00:08:22,439 you can use it now. Just like that, so 164 00:08:22,439 --> 00:08:24,435 I'm storing that in the f variable. 165 00:08:24,436 --> 00:08:26,562 [No audio] 166 00:08:26,564 --> 00:08:32,249 Let me command this, create figure objects. 167 00:08:32,250 --> 00:08:35,866 [No audio] 168 00:08:35,868 --> 00:08:42,569 And finally, create line plot. The way 169 00:08:42,569 --> 00:08:45,269 to create the line plot is by referring 170 00:08:45,299 --> 00:08:47,699 to the figure object that you created. 171 00:08:48,119 --> 00:08:50,609 And when you apply the line method if 172 00:08:50,609 --> 00:08:53,189 you want to create a line. And the line 173 00:08:53,189 --> 00:08:56,489 express from you to pass the x array 174 00:08:56,489 --> 00:09:00,059 with data and the y array as well. So 175 00:09:00,059 --> 00:09:02,459 know thats everything I'm passing here 176 00:09:02,489 --> 00:09:05,686 are the minimum requirements to build a 177 00:09:05,688 --> 00:09:09,059 plot. There's a lot of features that you 178 00:09:09,059 --> 00:09:11,639 will be adding later here in the code so 179 00:09:11,639 --> 00:09:14,969 that we arrange our plots with 180 00:09:14,969 --> 00:09:18,149 features such as legends and labels, and 181 00:09:18,149 --> 00:09:21,779 colors and transparency etc. But this is 182 00:09:21,779 --> 00:09:26,279 the back bone, so to say. And what you 183 00:09:26,279 --> 00:09:29,383 have created a line, all you have left to do is 184 00:09:29,520 --> 00:09:32,710 use the Show methods to show your figure object. 185 00:09:32,711 --> 00:09:35,211 [No audio] 186 00:09:35,213 --> 00:09:40,099 And yet to execute that in Jupyter, I can just 187 00:09:40,101 --> 00:09:45,044 press Ctrl and Enter. Wait a second 188 00:09:45,045 --> 00:09:47,647 [No audio] 189 00:09:47,649 --> 00:09:51,174 and I got an error there and put here. 190 00:09:51,296 --> 00:09:53,165 It should be output_file. Sorry. 191 00:09:53,926 --> 00:09:56,856 Execute again and here is a line graph. 192 00:09:56,857 --> 00:09:59,357 [No audio] 193 00:09:59,359 --> 00:10:01,650 And yeah. If you look at this, you see 194 00:10:01,650 --> 00:10:03,870 that the first point of the line is at 195 00:10:03,870 --> 00:10:06,330 coordinates one. So this is the x axis, 196 00:10:06,450 --> 00:10:09,660 1 for the x, 6 for the y, if you 197 00:10:09,660 --> 00:10:12,720 look here is a 1 and 6. And then you 198 00:10:12,720 --> 00:10:15,930 have 2 and 7 as a one up to the 199 00:10:15,932 --> 00:10:18,230 last point, which is 5 to 10. 200 00:10:18,231 --> 00:10:20,564 [No audio] 201 00:10:20,566 --> 00:10:25,860 5 and 10. Yeah. So now there's a graph. And 202 00:10:25,860 --> 00:10:27,990 this is not an image graph, as you can 203 00:10:27,990 --> 00:10:30,120 see, I can move this around and you 204 00:10:30,120 --> 00:10:32,340 also have a couple of tools here. So 205 00:10:32,340 --> 00:10:35,730 these are called tools. And what you can 206 00:10:35,730 --> 00:10:38,520 do with these is, let's say, this is a 207 00:10:38,520 --> 00:10:41,490 Pan, and if you see this vertical blue 208 00:10:41,490 --> 00:10:44,670 line here, that means that the Pan tool 209 00:10:44,670 --> 00:10:47,820 is already activated. And if I move my 210 00:10:47,970 --> 00:10:50,430 cursor like that, I'll pan around the 211 00:10:50,430 --> 00:10:54,300 graph. If you click, let's say, the 212 00:10:54,300 --> 00:10:57,060 Wheel Zoom, you'll see that both Pan 213 00:10:57,150 --> 00:10:59,880 and the Wheel Zoom are activated now. And I 214 00:10:59,880 --> 00:11:03,630 can use the wheel of my mouse to zoom in 215 00:11:03,660 --> 00:11:06,330 and out. And you know, remember that I 216 00:11:06,330 --> 00:11:09,420 told you, Bokeh produces interactive 217 00:11:09,450 --> 00:11:13,290 graphs. So here it is. However, this is 218 00:11:13,290 --> 00:11:15,360 just a basic functionality of 219 00:11:15,360 --> 00:11:18,270 interactivity. Because later, as I told 220 00:11:18,270 --> 00:11:20,280 you earlier, I will be adding widgets 221 00:11:20,280 --> 00:11:22,671 here that interacts very powerfully 222 00:11:22,673 --> 00:11:26,460 with a graph. And here basically, you also 223 00:11:26,460 --> 00:11:29,730 have this box zoom that you can 224 00:11:29,730 --> 00:11:31,830 zoom in a certain area of your graph, 225 00:11:32,430 --> 00:11:35,430 you also have this reset to there, 226 00:11:36,420 --> 00:11:39,090 which actually resets the graph to each 227 00:11:39,090 --> 00:11:42,930 default view. And so what's left, we 228 00:11:42,930 --> 00:11:46,410 have this Help button, which brings you 229 00:11:46,410 --> 00:11:48,840 to the Bokeh documentation. And I'll go 230 00:11:48,840 --> 00:11:50,550 through the Bokeh documentation later, 231 00:11:51,510 --> 00:11:54,810 which is very helpful for you to get 232 00:11:54,810 --> 00:11:59,490 help from. And it's also a Save button 233 00:11:59,490 --> 00:12:02,100 in here, which you can use to save your 234 00:12:02,100 --> 00:12:07,410 plots in PNG graphs, or images. So if 235 00:12:07,410 --> 00:12:10,320 you ever want to convert your graphs to 236 00:12:10,320 --> 00:12:12,360 images, you can use that button. And 237 00:12:12,360 --> 00:12:14,813 yeah, that's about building a basic graph. And 238 00:12:14,814 --> 00:12:17,586 I'd like to give you some practice activity now. 239 00:12:17,587 --> 00:12:19,954 [No audio] 240 00:12:19,956 --> 00:12:24,117 You know, here I use a line graph. 241 00:12:24,581 --> 00:12:27,625 But you can use other types of glyphs. 242 00:12:27,847 --> 00:12:32,280 This is how these geometries are code in 243 00:12:32,490 --> 00:12:36,060 Bokeh. And you can get an idea of what 244 00:12:36,090 --> 00:12:38,340 a glyphs you can use, you can apply to 245 00:12:38,340 --> 00:12:41,280 your figure by doing dir f, which is 246 00:12:41,310 --> 00:12:43,068 object instance of your figure object. 247 00:12:43,070 --> 00:12:45,089 [No audio] 248 00:12:45,090 --> 00:12:48,330 You will see here, let's find a line. 249 00:12:48,331 --> 00:12:50,336 [No audio] 250 00:12:50,338 --> 00:12:52,980 Here is line. So that's the one you will 251 00:12:52,980 --> 00:12:56,370 used. But I'd like to ask you to make 252 00:12:56,430 --> 00:12:59,550 your other, your own graphs using 253 00:13:00,630 --> 00:13:04,285 triangle first, and then a circle. 254 00:13:04,286 --> 00:13:06,810 [No audio] 255 00:13:06,812 --> 00:13:09,240 So that'd be very easy. Instead of line 256 00:13:09,240 --> 00:13:11,700 just pass these things here and see what 257 00:13:11,700 --> 00:13:15,480 you get. But try to make the graph from 258 00:13:15,480 --> 00:13:18,960 scratch instead of copying mine. Let me 259 00:13:18,960 --> 00:13:20,640 command this I forgot to command this. 260 00:13:20,641 --> 00:13:27,118 [Author typing] 261 00:13:27,120 --> 00:13:29,070 You can also play around with some other 262 00:13:29,070 --> 00:13:32,490 data to see how that goes, and yeah, that will 263 00:13:32,490 --> 00:13:34,170 make sure that you understand how to 264 00:13:34,170 --> 00:13:37,440 create a very basic Bokeh plot. And in 265 00:13:37,440 --> 00:13:39,090 the next lecture, I'll show you how to 266 00:13:39,090 --> 00:13:42,480 use some more earlier on data. So we use 267 00:13:42,480 --> 00:13:44,010 lists here but in the next lecture we 268 00:13:44,010 --> 00:13:47,670 will be use CSV files, so plotting from a 269 00:13:47,670 --> 00:13:50,490 CSV file. I will be creating the same 270 00:13:50,490 --> 00:13:51,900 graph. See you there.