下面的代码从两个不同的网站提取数据,Bokeh绘制数据。问题是,当我在x轴上相对于datatime.now()绘制“ Volumes”数据时,它会被绘制,但是当我绘制“ Open”或“ Lasts”数据时,我的散景图是空白的。我以为所抓取的数据包含垃圾字符,但如果是这样,则也不应绘制卷。在“ ugaz”和“ dgaz”以及输出导出到tag1.text和tags2.text的地方也会发生此问题。我试图了解了一段时间。
但是,当我打印(source.data)输出为
{'x':[datetime.datetime(2020,1,5,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
函数将字符串转换为数字。