Python 当';字符串';类型转换为';int';在博克

Python 当';字符串';类型转换为';int';在博克,python,pandas,bokeh,Python,Pandas,Bokeh,我正在从xml中获取一些属性值,这些属性值本质上是数字的,但类型为“string” 我正在将那些'strings'类型转换为'int',并尝试在Bokeh中绘制条形图。 图形填充不正确(已附加)。有什么建议吗 下面是代码 import pandas as pd from bokeh.charts import Bar, output_file, show #String values fetched from xml var='5' var1='6' #Converting string to

我正在从xml中获取一些属性值,这些属性值本质上是数字的,但类型为“string”

我正在将那些'strings'类型转换为'int',并尝试在Bokeh中绘制条形图。 图形填充不正确(已附加)。有什么建议吗

下面是代码

import pandas as pd
from bokeh.charts import Bar, output_file, show
#String values fetched from xml
var='5'
var1='6'
#Converting string to int
var=int(var)
var1=int(var1)

#Creating a dataframe
d = {'col1': [var], 'col2': [var1]}
df=pd.DataFrame(data=d)
print df

#Output
#   col1  col2
#0     0     4

#Displaying with Bokeh

p=Bar(df)
output_file("bar.html")
show(p)

首先:
Bar
是旧的、不推荐的
bokeh.charts
API的一部分,该API已从核心bokeh中完全删除。它仍然作为
bkcharts
软件包提供,但完全未维护且不受支持。此时不应将其用于任何新工作


然而,最近的工作使用稳定的、受支持的
bokeh.plotting
API极大地改进了对条形图和其他分类图的支持。这里纯粹致力于解释和演示各种简单和复杂的条形图。此外,现在使用标准的
bokeh.plotting
调用可以很容易地进行条形图绘制,现在也适用了

从您的示例代码中,我不太清楚您试图实现什么。下面是一个非常精简的版本,可能与此类似:

from bokeh.io import output_file, show
from bokeh.plotting import figure

p = figure(x_range=['col1', 'col2'])
p.vbar(x=['col1', 'col2'], top=[5, 6], width=0.8)

output_file("bar.html")
show(p)
该代码生成以下输出:

下面是一个更完整的简单条形图示例,它使用pandas统计数据(类似于
条形图
所做的操作),并使用鼠标悬停工具,使用“cars”样本数据和
bokeh。绘图
API:

from bokeh.io import show, output_file
from bokeh.models import HoverTool
from bokeh.plotting import figure
from bokeh.sampledata.autompg import autompg as df

output_file("groupby.html")

df.cyl = df.cyl.astype(str)
group = df.groupby('cyl')

p = figure(plot_height=350, x_range=group, toolbar_location=None, tools="")
p.vbar(x='cyl', top='mpg_mean', width=0.9, source=group)

p.add_tools(HoverTool(tooltips=[("Avg MPG", "@mpg_mean")]))

show(p)
这将产生以下结果


这正是我一直在寻找的……谢谢bigreddot