Bokeh ColumnDataSource标识为源时出现错误 – 为什么?

huangapple go评论72阅读模式
英文:

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 = [&#39;ob1&#39;,&#39;ob2&#39;,&#39;ob3&#39;,&#39;ob4&#39;,&#39;ob5&#39;]
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 = {&#39;index&#39;:&#39;order&#39;})
df[&#39;order&#39;] = df[&#39;order&#39;] + 1

cds = ColumnDataSource(data=df)

So far so good.

I try to generate a graph:

p = figure(title = &#39;ColumnDataSource test&#39;, sizing_mode = &#39;stretch_both&#39;)
p.line(x=cds.data[&#39;order&#39;], y = cds.data[&#39;ob1&#39;], source = cds.data[[&#39;order&#39;, &#39;ob1&#39;]])
show(p)

and get the following error:

Traceback (most recent call last):
  File &quot;e:\Black_Belt_Six_Sigma\first_take.py&quot;, line 57, in &lt;module&gt;
    p.line(x=cds.data[&#39;order&#39;], y = cds.data[&#39;ob1&#39;], source = cds.data[[&#39;order&#39;, &#39;ob1&#39;]])
TypeError: unhashable type: &#39;list&#39;

Fair enough, I won't give the source parameter a list and try again:

p = figure(title = &#39;ColumnDataSource test&#39;, sizing_mode = &#39;stretch_both&#39;)
p.line(x=cds.data[&#39;order&#39;], y = cds.data[&#39;ob1&#39;], 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 &#39;source&#39; 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=&#39;x&#39;, y=&#39;y&#39;, 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[&#39;order&#39;].to_list(), ob1 = df[&#39;ob1&#39;].to_list()))

p = figure(title = &#39;ColumnDataSource test&#39;, sizing_mode = &#39;stretch_both&#39;)
p.line(x=cds.data[&#39;order&#39;], y = cds.data[&#39;ob1&#39;], source = cds)
show(p)

And

cds = ColumnDataSource(data=dict(order = df[&#39;order&#39;], ob1 = df[&#39;ob1&#39;]))

p = figure(title = &#39;ColumnDataSource test&#39;, sizing_mode = &#39;stretch_both&#39;)
p.line(x=cds.data[&#39;order&#39;], y = cds.data[&#39;ob1&#39;], 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

你应该直接将字典键 orderob1 传递给参数 xy。将你的 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)

Bokeh ColumnDataSource标识为源时出现错误 – 为什么?

英文:

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 = [&#39;ob1&#39;,&#39;ob2&#39;,&#39;ob3&#39;,&#39;ob4&#39;,&#39;ob5&#39;]
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 = {&#39;index&#39;:&#39;order&#39;})
df[&#39;order&#39;] = df[&#39;order&#39;] + 1

cds = ColumnDataSource(data=df)

p = figure(title = &#39;ColumnDataSource test&#39;, sizing_mode = &#39;stretch_both&#39;)
p.line(x=&#39;order&#39;,y=&#39;ob1&#39;,source=cds)
show(p)

Bokeh ColumnDataSource标识为源时出现错误 – 为什么?

huangapple
  • 本文由 发表于 2023年2月14日 19:45:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定