gpt4 book ai didi

python - Bokeh 仅绘制来自网络抓取数据的特定列

转载 作者:太空宇宙 更新时间:2023-11-03 19:54:39 25 4
gpt4 key购买 nike

下面的代码从两个不同的网站提取数据,并用 Bokeh 绘制数据。问题是,当我在 x 轴上针对 datatime.now() 绘制“Volume”数据时,它会被绘制出来,但是当我绘制“Open”或“Lasts”数据时,我的 Bokeh 图为空白。我认为抓取的数据包含垃圾字符,但如果是这样,那么卷也不应该被绘制。 “ugaz”和“dgaz”以及输出导出到tags1.text 和tags2.text 时也会出现此问题。我已经有一段时间试图理解了。

但是,当我打印(source.data)时,输出为

{'x': [datetime.datetime(2020, 1, 5, 21, 15, 38, 712611)], 'y': ['1094'], 'y1': ['2.095']}

代码是:

 import requests
from bs4 import BeautifulSoup
from bokeh.models import Range1d, LinearAxis
import time
from datetime import datetime
from bokeh.models import ColumnDataSource, DatetimeTickFormatter
from bokeh.plotting import figure, show
from math import radians
import pandas as pd

p = figure()

Volumes = []
Opens = []
Lasts=[]
Contracts =[]
Lows = []
Highs = []

res3 = requests.get('https://shared.websol.barchart.com/quotes/quote.php?
page=quote&sym=ng&x=13&y=8&domain=if&display_ice=1&enabled_ice_exchanges=&tz=0&ed=0')
res1 = requests.get('https://finance.yahoo.com/quote/ugaz?ltr=1')
res2 = requests.get('https://finance.yahoo.com/quote/dgaz?ltr=1')
soup1 = BeautifulSoup(res1.text,'html.parser')
soup2 = BeautifulSoup(res2.text,'html.parser')
tags1 = soup1.find_all('span')[11]
tags2 = soup2.find_all('span')[11]
soup3 = BeautifulSoup(res3.text, 'lxml')
soup3.prettify()
data_rows = soup3.findAll('tr')[2:]
i = range(len(data_rows))
for td in data_rows:
Volume = td.findAll('td')[6].text
Volumes.append(Volume)
Open = td.findAll('td')[3].text
Opens.append(Open)
Last = td.findAll('td')[1].text
Lasts.append(Last)
Contract = td.findAll('td')[0].text
Contracts.append(Contract)
Low = td.findAll('td')[5].text
Lows.append(Low)
High = td.findAll('td')[4].text
Highs.append(High)

source = ColumnDataSource(dict(x=[datetime.now()],y=[Volumes[2]], y1=[Opens[2]]))

p.circle(x ='x', y ='y',source=source,color='blue')
p.circle(x ='x', y ='y1',source=source,color='red')

show(p)

最佳答案

您没有将所有值转换为数字:

{
'x': [datetime.datetime(2020, 1, 5, 21, 15, 38, 712611)],
'y': ['1094'], # value in list is a string -- BAD
'y1': ['2.095'] # value in list is a string -- BAD
}

除非您专门绘制分类值(此处不是这种情况),否则 CDS 列中的值通常应始终为数字。

您可以通过调用内置的 float 函数将字符串转换为数字。

关于python - Bokeh 仅绘制来自网络抓取数据的特定列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59605958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com