[英]Bokeh not displaying plot even after converting X and Y values into NumPy arrays and using a DateTime axis
Bokeh is not displaying my plot.散景没有显示我的 plot。 I will give the whole code but mark the area I am suspicious of with Python comments.
我将给出整个代码,但用 Python 注释标记我怀疑的区域。
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".运行代码时,我只看到两个空白的灰色图和我在“可疑区域”中指定的标题。
picture of the resulting plots that I am getting我得到的结果图的图片
I then booted up the Edge Console and saw that there were several errors, 5 to be exact.然后我启动了 Edge 控制台,发现有几个错误,准确地说是 5 个。 4 of them were warnings which stated [bokeh] could not set initial ranges and last one was an error which stated Uncaught (in promise) Error: invalid bbox {x0: NaN, y0: 27, x1: NaN, y1: 125}
其中 4 个是警告,指出 [bokeh] 无法设置初始范围,最后一个是错误,指出 Uncaught (in promise) Error: invalid bbox {x0: NaN, y0: 27, x1: NaN, y1: 125}
There is this StackOverflow answer that I found which provides two solutions for my error to work.我发现了这个StackOverflow 答案,它为我的错误提供了两种解决方案。 I had already implemented the second solution beforehand as you can see -- I have already converted the datetime values to datetime NumPy arrays and even though the values are strings, the axis for the plots is a datetime axis which means it should work as stated in the second solution but it doesn't work.
如您所见,我已经预先实现了第二个解决方案——我已经将日期时间值转换为日期时间 NumPy arrays 并且即使值是字符串,绘图的轴也是日期时间轴,这意味着它应该按照第二种解决方案,但它不起作用。
I will be extremely grateful if you could help me fix my mistakes.如果您能帮助我改正我的错误,我将不胜感激。 (its probably like an obvious mistake like a spelling mistake which I am unable to locate)
(这可能是一个明显的错误,比如我无法找到的拼写错误)
As I mentioned in my comment, something is wrong with your data.正如我在评论中提到的,您的数据有问题。 I copy'n'past your code and created some dummy data, which is working fine.
我复制了你的代码并创建了一些虚拟数据,它工作正常。
Minimal Example最小的例子
import pandas as pd
import numpy as np
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, RangeTool
from bokeh.plotting import figure, output_notebook, show
output_notebook()
#dummy data
df = pd.DataFrame({'close':np.random.randint(100,200,20)}, index=pd.date_range('2020-01-01', periods=20, freq='M'))
df.index.name = 'date'
source = ColumnDataSource(df)
# figures
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=(df.index[9], df.index[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")
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
show(column(p, select))
Output Output
Further readings进一步阅读
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.