(BAD_COLUMN_NAME):字形引用不存在的列名称,散景

Jan*_*ski 4 python plot bokeh

我正在尝试修改 Bokeh 库图中的代码。它的链接在这里

我的目标是为绘图提供不同的 y 尺度。我编辑了该make_plot函数,而是复制该函数的内容以尝试制作第二个(右上)图,使用 x 和 y2 列而不是第一个(左上)图所使用的 x 和 y 列。

我到目前为止:

from __future__ import print_function

import numpy as np
import pandas as pd

from bokeh.util.browser import view
from bokeh.document import Document
from bokeh.embed import file_html
from bokeh.layouts import column, gridplot
from bokeh.models import Circle, ColumnDataSource, Div, Grid, Line, LinearAxis, Plot, Range1d
from bokeh.resources import INLINE

raw_columns=[
[176.232,   2.212975,   176.232,   14088.2,   10.0,   7.46,   8.0,    6.58],
[177.487,   2.212190,   177.487,   14080.6,   8.0,    6.77,   8.0,    5.76],
[178.743,   2.211719,   178.743,   14057.6,   13.0,   12.74,  8.0,    7.71],
[180,       2.211562,   180,       14019.3,    7.11,   8.0,    8.84],
[181.257,   2.211719,   181.257,    14080.6,   11.0,   7.81,   8.0,    8.47],
[182.513,   2.212190,   182.513,    14057.6,   14.0,   8.84,   8.0,    7.04],
[183.768,   2.212975,   183.768,    14019.3,   5.0,    5.73,   8.0,    6.89]]

df = pd.DataFrame(data=raw_columns, columns=
                       ['Ix','Iy','IIx','IIy','IIIx','IIIy','IVx','IVy'])


circles_source = ColumnDataSource(
    data = dict(
        xi   = df['Ix'],
        yi   = df['Iy'],
        xii  = df['IIx'],
        yii  = df['IIy'],
        xiii = df['IIIx'],
        yiii = df['IIIy'],
        xiv  = df['IVx'],
        yiv  = df['IVy'],
    )
   )

x = np.linspace(176, 184, 100)
x1column = df['Ix'].values
y1column = df['Iy'].values
#y = 0.0000995532*x**2 - 0.0358391636*x + 5.4370863900
z = np.polyfit(x1column,y1column,2)
y = z[0]*x**2 + z[1]*x +z[2]
lines_source = ColumnDataSource(data=dict(x=x, y=y))

xdr = Range1d(start=176, end=184)
ydr = Range1d(start=2.2115, end=2.2135)
x2dr = Range1d(start=-0.12, end=0.12)
y2dr = Range1d(start=14000, end=14100)
x2column = df['IIx'].values
y2column = df['IIy'].values
z2 = np.polyfit(x1column,y2column,2)
y2 = z2[0]*x**2 + z2[1]*x +z2[2]
def make_plot(title, xname, yname):
    plot = Plot(x_range=xdr, y_range=ydr, plot_width=400, plot_height=400,
                background_fill_color='#efefef')
    plot.title.text = title

    xaxis = LinearAxis(axis_line_color=None)
    plot.add_layout(xaxis, 'below')

    yaxis = LinearAxis(axis_line_color=None)
    plot.add_layout(yaxis, 'left')

    plot.add_layout(Grid(dimension=0, ticker=xaxis.ticker))
    plot.add_layout(Grid(dimension=1, ticker=yaxis.ticker))

    line = Line(x='x', y='y', line_color="#666699", line_width=2)
    plot.add_glyph(lines_source, line)

    circle = Circle(
        x=xname, y=yname, size=12,
        fill_color="#cc6633", line_color="#cc6633", fill_alpha=0.5
    )
    plot.add_glyph(circles_source, circle)

    return plot

#where will this comment show up
I   = make_plot('I',   'xi',   'yi')
#playing around starts here 
plot = Plot(x_range=x2dr, y_range=y2dr, plot_width=400, plot_height=400,
            background_fill_color='#efefef')
plot.title.text = 'd-d transition'

xaxis = LinearAxis(axis_line_color=None)
plot.add_layout(xaxis, 'below')

yaxis = LinearAxis(axis_line_color=None)
plot.add_layout(yaxis, 'left')

plot.add_layout(Grid(dimension=0, ticker=xaxis.ticker))
plot.add_layout(Grid(dimension=1, ticker=yaxis.ticker))

lines_source = ColumnDataSource(data=dict(x=x, y=y2))
line = Line(x='x', y='y2', line_color="#666699", line_width=2)
plot.add_glyph(lines_source, line)

circle = Circle(
    x='xii', y='yii', size=12,
    fill_color="#cc6633", line_color="#cc6633", fill_alpha=0.5
)
plot.add_glyph(circles_source, circle)
II = plot
#playing around ends here
#II  = make_plot('II',  'xii',  'yii')
III = make_plot('III', 'xiii', 'yiii')
IV  = make_plot('IV',  'xiv',  'yiv')

grid = gridplot([[I, II], [III, IV]], toolbar_location=None)

div = Div(text="""
<h1>Anscombe's Quartet</h1>
<p>Anscombe's quartet is a collection of four small datasets that have nearly
identical simple descriptive statistics (mean, variance, correlation, and linear
regression lines), yet appear very different when graphed.
</p>
""")

doc = Document()
doc.add_root(column(div, grid, sizing_mode="scale_width"))

if __name__ == "__main__":
    doc.validate()
    filename = "anscombe.html"
    with open(filename, "w") as f:
        f.write(file_html(doc, INLINE, "Anscombe's Quartet"))
    print("Wrote %s" % filename)
    view(filename)
Run Code Online (Sandbox Code Playgroud)
ERROR:bokeh.core.validation.check:E-1001 (BAD_COLUMN_NAME): Glyph refers to nonexistent column name. This could either be due to a misspelling or typo, or due to an expected column being missing. : key "y" value "y2" (closest match: "y") [renderer: GlyphRenderer(id='14758', ...)]
ERROR:bokeh.core.validation.check:E-1001 (BAD_COLUMN_NAME): Glyph refers to nonexistent column name. This could either be due to a misspelling or typo, or due to an expected column being missing. : key "y" value "y2" (closest match: "y") [renderer: GlyphRenderer(id='14758', ...)]
Run Code Online (Sandbox Code Playgroud)

如果知道有帮助的话:我尝试用“y”替换“y2”,并且脚本运行没有错误,但右上角的图是空白的。

这个错误似乎很容易解决,但我无法解决它。

big*_*dot 5

设置为\'y\'是正确的做法。传递到字形的名称映射到数据源中的列名称,并且数据源中没有“y2”列。绘图为空白的原因是您的 x 坐标超出了您设置的显式视口范围:

\n\n
(base) \xe2\x9d\xaf python test.py\nmin x = 176.0, max x = 184.0\nWrote anscombe.html\n
Run Code Online (Sandbox Code Playgroud)\n\n

相比之下,您设置的绘图的 x 范围是从x2dr.start = -0.12x2dr.end = 0.12。因此,所有数据都离屏幕很远。您可以调整绘图边界或调整 x 坐标。我不知道什么适合您的用例,因此我无法就此事提供建议。

\n\n

作为一个温和有用的提示,出现问题时快速查看数据几乎总是一个好主意。添加一条打印语句来显示 x 最大/最小值立即发现了该问题。

\n