Plotting Bitcoin Prices as an Interactive Plot with a Range Tool

Python 3: Automating Your Job Tasks Superhero Level: Data Visualization with Bokeh and Python 3
12 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€91.90
List Price:  €128.67
You save:  €36.76
£78.70
List Price:  £110.19
You save:  £31.48
CA$136.14
List Price:  CA$190.60
You save:  CA$54.46
A$149.21
List Price:  A$208.90
You save:  A$59.69
S$134.58
List Price:  S$188.42
You save:  S$53.84
HK$780.18
List Price:  HK$1,092.29
You save:  HK$312.10
CHF 90.89
List Price:  CHF 127.25
You save:  CHF 36.36
NOK kr1,073.95
List Price:  NOK kr1,503.58
You save:  NOK kr429.62
DKK kr686.39
List Price:  DKK kr960.97
You save:  DKK kr274.58
NZ$162.99
List Price:  NZ$228.20
You save:  NZ$65.20
د.إ367.26
List Price:  د.إ514.18
You save:  د.إ146.92
৳11,707.96
List Price:  ৳16,391.62
You save:  ৳4,683.65
₹8,329.22
List Price:  ₹11,661.24
You save:  ₹3,332.02
RM468.70
List Price:  RM656.20
You save:  RM187.50
₦146,985.30
List Price:  ₦205,785.30
You save:  ₦58,800
₨27,706.17
List Price:  ₨38,789.74
You save:  ₨11,083.57
฿3,624.43
List Price:  ฿5,074.35
You save:  ฿1,449.91
₺3,224.01
List Price:  ₺4,513.75
You save:  ₺1,289.73
B$510.40
List Price:  B$714.59
You save:  B$204.18
R1,807.25
List Price:  R2,530.23
You save:  R722.97
Лв179.86
List Price:  Лв251.81
You save:  Лв71.95
₩135,425.45
List Price:  ₩189,601.05
You save:  ₩54,175.60
₪370.41
List Price:  ₪518.60
You save:  ₪148.18
₱5,770.97
List Price:  ₱8,079.59
You save:  ₱2,308.62
¥15,565.94
List Price:  ¥21,792.94
You save:  ¥6,227
MX$1,660.69
List Price:  MX$2,325.03
You save:  MX$664.34
QR362.75
List Price:  QR507.87
You save:  QR145.11
P1,354.73
List Price:  P1,896.68
You save:  P541.94
KSh13,098.69
List Price:  KSh18,338.69
You save:  KSh5,240
E£4,690.53
List Price:  E£6,566.93
You save:  E£1,876.40
ብር5,727.42
List Price:  ብር8,018.62
You save:  ብር2,291.20
Kz84,733.22
List Price:  Kz118,629.90
You save:  Kz33,896.68
CLP$89,440.82
List Price:  CLP$125,220.73
You save:  CLP$35,779.90
CN¥722.24
List Price:  CN¥1,011.17
You save:  CN¥288.92
RD$5,826.59
List Price:  RD$8,157.46
You save:  RD$2,330.87
DA13,436.03
List Price:  DA18,810.98
You save:  DA5,374.95
FJ$222.79
List Price:  FJ$311.92
You save:  FJ$89.12
Q776.81
List Price:  Q1,087.56
You save:  Q310.75
GY$20,917.88
List Price:  GY$29,285.87
You save:  GY$8,367.99
ISK kr13,827.61
List Price:  ISK kr19,359.21
You save:  ISK kr5,531.60
DH990.33
List Price:  DH1,386.51
You save:  DH396.17
L1,760
List Price:  L2,464.07
You save:  L704.07
ден5,654.52
List Price:  ден7,916.55
You save:  ден2,262.03
MOP$803.51
List Price:  MOP$1,124.95
You save:  MOP$321.43
N$1,819.78
List Price:  N$2,547.77
You save:  N$727.98
C$3,674.63
List Price:  C$5,144.63
You save:  C$1,470
रु13,329.38
List Price:  रु18,661.67
You save:  रु5,332.28
S/371.45
List Price:  S/520.05
You save:  S/148.59
K388.01
List Price:  K543.23
You save:  K155.22
SAR374.96
List Price:  SAR524.96
You save:  SAR150
ZK2,538.86
List Price:  ZK3,554.51
You save:  ZK1,015.64
L457.57
List Price:  L640.62
You save:  L183.04
Kč2,271.57
List Price:  Kč3,180.29
You save:  Kč908.72
Ft35,609.88
List Price:  Ft49,855.26
You save:  Ft14,245.38
SEK kr1,071.59
List Price:  SEK kr1,500.27
You save:  SEK kr428.67
ARS$88,666.13
List Price:  ARS$124,136.13
You save:  ARS$35,470
Bs690.83
List Price:  Bs967.19
You save:  Bs276.36
COP$380,678.73
List Price:  COP$532,965.46
You save:  COP$152,286.72
₡51,169.76
List Price:  ₡71,639.71
You save:  ₡20,469.95
L2,462.78
List Price:  L3,448
You save:  L985.21
₲749,130.70
List Price:  ₲1,048,812.96
You save:  ₲299,682.25
$U3,853.49
List Price:  $U5,395.05
You save:  $U1,541.55
zł392.01
List Price:  zł548.83
You save:  zł156.82
Already have an account? Log In

Transcript

Hi, and welcome to this lecture, it's time to build yet another type of plot, this time adding a new feature to our application arrange tool. Let me show you what I mean. So on this chart right here, we have the price evolution of Bitcoin over a period of one month, and we're going to discuss the details about this plot in a moment. Right underneath this figure, notice we have a separate figure, which is the actual range tool I was talking about. Now if you look carefully, you can notice that the line illustrated in the first larger picture is the same as the portion highlighted in the gray on the second smaller figure. This gray segment right here is called a range.

Moreover, you can increase or decrease the size of this range by simply dragging one or both of its edges. So like this or like this, as you drag one out Notice that the figure above changes accordingly. Critical, right? This range functionality enables you to basically zoom in and out of your plot and analyze the chart in greater detail for a customized experience. Another thing you can do with this range apart from moving its edges is to simply click on the highlighted area, notice this little hand cursor and just move the entire range according to your needs. This way, you can use this zoomed in view to go through the entire plot.

Great. Now it's time to see the code and I'm going to paste it into a new Jupiter notebook. So let me create this notebook. And I'm going to call it range. And now let's paste in the code. You can find this code in the lecture following this video as well.

Now, unlike in the previous applications in this section, where we use lists, Excel files, CSV files, Even bouquets on sample data. In this lecture in this application, we are going to read and load the necessary data right from a website using, of course, the read HTML method from within the pandas module. The website is called coin market cap.com. And since we are interested in plotting the price of bitcoin, I'm going to click on Bitcoin right here, and then I'm going to select historical data. Scroll down a bit. Further more, I will select a certain time interval by clicking on custom range right here.

And then I'm going to select let's say, February 20 of 2019. Up to march 20 of the same year, hit Apply. At this point, the site generates this table right here showing the date open, highest, lowest and closing prices for that day, as well as the daily volume. The market capitalization. Now let's open up the Python interpreter and load this table into a panda's data frame using the read HTML method, and the link to this webpage, so I'm going to simply copy this and paste it into the Python interpreter import import pandas. Now let's see our data frame df.

Notice that the result is actually a list of two elements, two tables that have been identified on the webpage we provided. So looking at the page, once again, notice that we have another table down below on the page. That's why we got two elements inside the list. Now of course, we are interested only in the first table. So we should specify this when creating the data frame by simply adding index zero to reference the first element of the list. Now before doing this, let's look at this table once again, and notice that the table starts by showing the data for the most recent date March 20.

However, in order to create our bitcoin price evolution plot, we will need our data set to start with the price recorded on February 20, and end with the price Bitcoin had on March 20. So we need to consider the normal timeline, not a reversed one. That's why we need to reverse the current order of elements in this table. And what better way to do that than to simply use a step of minus one. So let's see this in action. I'm going to load this data once again.

Sorry. So I said that we need the first table that would be the one at index zero in that list. And also we want the data to be reversed. So we have colon minus one. Okay, enter. Now let's see the F once again.

This time we got only the data frame that we need. And we've also reversed its rows to reflect on normal timeline from February 20, up to march 20. Great. Now let's get back to the code inside the Jupiter notebook. While importing the necessary modules up here, notice that the range tool has been imported as well from the bulky library. We are loading the table in a panda's data frame using the syntax we've just discussed.

And then we are converting the data column in our table right here to the proper date time format to use further into a NumPy array. So let's try this in the Python interpreter as well and see what does the date column look like and how to convert it to another format. So for this, I'm going to use df of date, the name of the column. Okay, now we can convert this format right here. To a NumPy friendly format using the to date time method from within the pandas module. So to do that, df dot date equals pandas.to date time of, and in between parentheses we have df dot date, because we are converting the dates in that column, enter.

Now let's see df. Once again, looking at our data frame. Now, notice that the date format has been changed, and also that the change has been made in place using this line of code right here. This means that the changes have been permanently saved to our data frame. On the next line of code right here, we are converting the date column from our data frame to a NumPy array by simply using the to NumPy method applied on the data frame column itself. We're also specifying the D type the type of array Which is the time 64 as you've already seen in the previous lecture, with this additional D in between square brackets, which stands for days, the smallest unit that we need to express the date in, actually, let's run this line in the Python interpreter as well, so you can see what I mean.

So I'm going to copy and paste this right here. Now let's see dates. So this is the NumPy array we are looking for. As a bonus, let me show you how to change the daytime unit when using the D type argument in order to also include smaller units of time for each date. So let's say that we have, instead of capital D, we have s for seconds if we want to have seconds displayed as well, dates, we have lowercase M for minutes. Let's say this again.

So notice that the seconds we had up here have disappeared. You can also specify hours, lowercase h dates. And as we've seen already, we have capital D for days. We also have capital M for showing only the month. So dates. And of course, as you might have guessed already, we have capital Y for only displaying the year.

Okay, back to the code now. Next we are creating the column data source object as we did earlier in this section to build a mapping between the date inside the NumPy array and the closing prices in the close column of the data frame. This column data source object is going to be later passed to the source argument when drawing the line as you can see, down here, next we are building the figure object for the first figure. The one on the top we are passing Multiple arguments that we've already discussed and tested in previous video in this section of the course. So I won't do it again here. However, the only thing I should mention is the use of the x range argument.

You can see this argument here, its role is to define the default range displayed when generating the figure. So in this case, I chose a random time range from this array. Let's see this in the interpreter. So I have dates of 12, up to date of 20. Sorry, my mistake, I should first set this back to these. And now let's see these are dates of 12 against and dates of 20.

Okay, so our default plot should display the evolution of the closing price of bitcoin between March 4 and march 12. And if you look carefully at the x axis of our plot, sorry, let's go generated once again. So if you look very carefully at the x axis, you can see that these are indeed the default limits of this range. So we have March 4, up here in the top left corner, up to march 12 in the top right corner along the x axis. Okay, back to the code. Next, we are simply drawing the line based on the data we've already generated above.

And we're also setting a label for the y axis, the one showing the closing prices of Bitcoin. Moving further, we have to also create the second figure the range to itself. For this, we need to use the figure function once again, and provide various arguments to customize the look and feel of this figure. We've already discussed these arguments in previous examples, so I won't waste your time providing the same explanations. Once again, you are free to test and play around with these arguments and their values. To change the way your figure is displayed.

Next we are creating the range functionality itself using the range tool class from within the bulky library. We are also passing a single argument here, which is x range. And this argument is basically referencing the same x range value that we used for the first figure up here. Actually, you should notice that we are setting the x range argument to be that x range so that the range highlighted in gray in the bottom figure will be the same as the one shown in the top figure. Okay, on the next several lines, we are customizing various styling aspects like the field color or the grid line color, and we are also using the line method as you can see down here to draw the same line once again, this time on the bottom figure as you can see on the chart as well. Next, we are adding the arranged All that we have created to the second figure in this line over here, referencing the Select variable.

And then we are also specifying that this is a so called active multi gesture tool, meaning you have multiple actions available. In this case, we can both drag the edges of this range and also Select and Move the entire range as shown earlier. Finally, as always, we are configuring the output HTML file and then showing the final result the two figures using the column function which tells our application to display the two figures as a column, one on top of the other. The shell function takes the result of the column function as an argument and generates the final result. So this is our final result. This is our bitcoin price chart and our range tool.

I hope you enjoyed watching this video and I'll see you in the next lecture.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.