Home > Software design >  Bokeh not displaying plot even after converting X and Y values into NumPy arrays and using a DateTim
Bokeh not displaying plot even after converting X and Y values into NumPy arrays and using a DateTim

Time:08-11

Bokeh is not displaying my plot. I will give the whole code but mark the area I am suspicious of with Python comments.

import pandas
import numpy

from bokeh.layouts import column
from bokeh.models import ColumnDataSource, RangeTool
from bokeh.plotting import figure, output_file, show

from selenium import webdriver
from selenium.webdriver.edge.service import Service
from webdriver_manager.microsoft import EdgeChromiumDriverManager
from selenium.webdriver.common.by import By

driver = webdriver.Edge(service=Service(EdgeChromiumDriverManager().install()))
driver.get("https://www.statmuse.com/money/ask/bitcoin value graph 2020-2021 monthly")
driver.implicitly_wait(10)
html = driver.find_element(By.CLASS_NAME, "kaojVddNm5wDHXzg63Rp").get_attribute("outerHTML")
driver.close()

df = pandas.DataFrame(pandas.read_html(html)[0][::-1])

df["DATE"] = pandas.to_datetime(df["DATE"])
dates = df["DATE"].to_numpy(dtype="datetime64[M]")

source = ColumnDataSource(data=dict(date=dates, close=list(df["CLOSE"])))

# -------------------------------------------------------------------------------------
# ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ Suspicious area where the error might be ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

p = figure(plot_height=300, plot_width=1200, tools="", toolbar_location=None,
           x_axis_type="datetime", x_axis_location="above",
           background_fill_color="#efefef", x_range=(dates[9], dates[18]))

p.line(x="date", y="close", source=source)

select = figure(title="Drag the middle and edges of the selection box to change the range above",
                plot_height=130, plot_width=1200, y_range=p.y_range,
                x_axis_type="datetime", y_axis_type=None,
                tools="", toolbar_location=None, background_fill_color="#efefef")

# ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ Suspicious area where the error might be ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
# -------------------------------------------------------------------------------------

range_tool = RangeTool(x_range=p.x_range)

range_tool.overlay.fill_color = "navy"
range_tool.overlay.fill_alpha = 0.2

select.line(x="date", y="close", source=source)
select.ygrid.line_color = None
select.add_tools(range_tool)
select.toolbar.active_multi = range_tool

output_file("btc_price_interactive.html", title="Bitcoin Price Chart")
show(column(p, select))

When running the code, I see nothing but two blank grey plots and a title that I specified in the "Suspicious Area".

range tool

Further readings

  1. The bokeh documentation has an example for the use of the range tool with some sampledata.
  2. It looks like you are trying to plot a candlestick plot. Bokeh docs has an example for this, too.
  3. On SO exists a modification of the candlestick example here, which can be modified with a range-tool easily.
  • Related