英文:
Bokeh ColumnDataSource Error when identifying as source - why?
问题
我遇到了一些错误消息,当我将ColumnDataSource标识为我的数据源时,希望能够正确操作。
让我展示一下这些错误。
首先,我在DataFrame中生成了一些随机数据并将其放入ColumnDataSource中:
col_list = ['ob1','ob2','ob3','ob4','ob5']
df = pd.DataFrame(np.random.uniform(73.965,74.03,size=(25, 5)).astype(float), columns=col_list)
df.reset_index(inplace=True)
df = df.rename(columns = {'index':'order'})
df['order'] = df['order'] + 1
cds = ColumnDataSource(data=df)
到目前为止都还好。
我尝试生成一个图形:
p = figure(title = 'ColumnDataSource测试', sizing_mode = 'stretch_both')
p.line(x=cds.data['order'], y = cds.data['ob1'], source = cds.data[['order', 'ob1']])
show(p)
但是出现了以下错误:
Traceback (most recent call last):
File "e:\Black_Belt_Six_Sigma\first_take.py", line 57, in <module>
p.line(x=cds.data['order'], y = cds.data['ob1'], source = cds.data[['order', 'ob1']])
TypeError: unhashable type: 'list'
好吧,我不会再将源参数设置为列表,再试一次:
p = figure(title = 'ColumnDataSource测试', sizing_mode = 'stretch_both')
p.line(x=cds.data['order'], y = cds.data['ob1'], source = cds.data)
show(p)
我没有得到图形,只得到以下错误:
RuntimeError:
预期x和y引用提供的数据源中的字段。
当将'源'参数传递给glyph方法时,序列值(如列表或数组)必须来自数据源中数据列的引用。
例如,作为一个示例:
source = ColumnDataSource(data=dict(x=a_list, y=an_array))
p.circle(x='x', y='y', source=source, ...) # 传递列名和源
或者,只要*没有*提供源,*所有*数据序列都可以作为字面值提供:
p.circle(x=a_list, y=an_array, ...) # 传递实际序列和无源
根据这个错误消息,我尝试了以下操作:
cds = ColumnDataSource(data=dict(order = df['order'].to_list(), ob1 = df['ob1'].to_list()))
p = figure(title = 'ColumnDataSource测试', sizing_mode = 'stretch_both')
p.line(x=cds.data['order'], y = cds.data['ob1'], source = cds)
show(p)
以及
cds = ColumnDataSource(data=dict(order = df['order'], ob1 = df['ob1']))
p = figure(title = 'ColumnDataSource测试', sizing_mode = 'stretch_both')
p.line(x=cds.data['order'], y = cds.data['ob1'], source = cds)
show(p)
两者都返回相同的错误消息。
如果我不指定source
参数,我可以得到一个图形/绘图,所以也许这是正确的操作?看起来有点奇怪,但我想如果开发人员将其作为一个参数,那可能很重要。
英文:
I'm getting error messages when identifying ColumnDataSource as my source, want to do it right.
Let me show the errors.
First, I generate some random data in a DataFrame and put it into the ColumnDataSource:
col_list = ['ob1','ob2','ob3','ob4','ob5']
df = pd.DataFrame(np.random.uniform(73.965,74.03,size=(25, 5)).astype(float), columns=col_list)
df.reset_index(inplace=True)
df = df.rename(columns = {'index':'order'})
df['order'] = df['order'] + 1
cds = ColumnDataSource(data=df)
So far so good.
I try to generate a graph:
p = figure(title = 'ColumnDataSource test', sizing_mode = 'stretch_both')
p.line(x=cds.data['order'], y = cds.data['ob1'], source = cds.data[['order', 'ob1']])
show(p)
and get the following error:
Traceback (most recent call last):
File "e:\Black_Belt_Six_Sigma\first_take.py", line 57, in <module>
p.line(x=cds.data['order'], y = cds.data['ob1'], source = cds.data[['order', 'ob1']])
TypeError: unhashable type: 'list'
Fair enough, I won't give the source parameter a list and try again:
p = figure(title = 'ColumnDataSource test', sizing_mode = 'stretch_both')
p.line(x=cds.data['order'], y = cds.data['ob1'], source = cds.data)
show(p)
I get no graph but only the following error:
RuntimeError:
Expected x and y to reference fields in the supplied data source.
When a 'source' argument is passed to a glyph method, values that are sequences
(like lists or arrays) must come from references to data columns in the source.
For instance, as an example:
source = ColumnDataSource(data=dict(x=a_list, y=an_array))
p.circle(x='x', y='y', source=source, ...) # pass column names and a source
Alternatively, *all* data sequences may be provided as literals as long as a
source is *not* provided:
p.circle(x=a_list, y=an_array, ...) # pass actual sequences and no source
Based on this error message I've tried the following:
cds = ColumnDataSource(data=dict(order = df['order'].to_list(), ob1 = df['ob1'].to_list()))
p = figure(title = 'ColumnDataSource test', sizing_mode = 'stretch_both')
p.line(x=cds.data['order'], y = cds.data['ob1'], source = cds)
show(p)
And
cds = ColumnDataSource(data=dict(order = df['order'], ob1 = df['ob1']))
p = figure(title = 'ColumnDataSource test', sizing_mode = 'stretch_both')
p.line(x=cds.data['order'], y = cds.data['ob1'], source = cds)
show(p)
Both keep returning the same error message.
I can get a graph/plot if I don't specify the source
parameter, so maybe that's the right course of action? Seems odd, I imagine it's important if the developers made it a parameter.
答案1
得分: 1
你应该直接将字典键 order
和 ob1
传递给参数 x
和 y
。将你的 ColumnDataSource
cds
传递给参数 source
(更多示例请参考这里):
import pandas as pd
import numpy as np
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource
col_list = ['ob1', 'ob2', 'ob3', 'ob4', 'ob5']
df = pd.DataFrame(np.random.uniform(73.965, 74.03, size=(25, 5)).astype(float), columns=col_list)
df.reset_index(inplace=True)
df = df.rename(columns={'index': 'order'})
df['order'] = df['order'] + 1
cds = ColumnDataSource(data=df)
p = figure(title='ColumnDataSource 测试', sizing_mode='stretch_both')
p.line(x='order', y='ob1', source=cds)
show(p)
英文:
You should pass your dictionary keys order
and ob1
directly to the arguments x
and y
. And your ColumDataSource
cds
to the argument source
(see more examples here):
import pandas as pd
import numpy as np
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource
col_list = ['ob1','ob2','ob3','ob4','ob5']
df = pd.DataFrame(np.random.uniform(73.965,74.03,size=(25, 5)).astype(float), columns=col_list)
df.reset_index(inplace=True)
df = df.rename(columns = {'index':'order'})
df['order'] = df['order'] + 1
cds = ColumnDataSource(data=df)
p = figure(title = 'ColumnDataSource test', sizing_mode = 'stretch_both')
p.line(x='order',y='ob1',source=cds)
show(p)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论