简体   繁体   English

在Bokeh中将“字符串”类型转换为“ int”时,无法正确绘制图形

[英]Not able to plot graph correctly when a 'string' type is converted to 'int' in Bokeh

i am fetching some attribute values from xml , which are numeric in nature but coming as type 'string' 我正在从xml中获取一些属性值,这些属性值本质上是数字的,但类型为'string'

i am converting those 'strings' type to 'int' , and trying to plot a Bar graph in Bokeh. 我正在将这些“字符串”类型转换为“ int” ,并试图在散景中绘制条形图。 The graph is not populating correctly (attached) 图形未正确填充(已附加) 散景图 . Any suggestion ? 有什么建议吗?

Below is the code 下面是代码

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)

First off: Bar is part of the old, deprecated bokeh.charts API that has since been completely removed from core Bokeh. 首先: Bar是已弃用的bokeh.charts API的一部分,此API已从核心Bokeh中完全删除。 It is still available as the bkcharts package, but it is completely unmaintained and unsupported . 它仍然可以作为bkcharts软件包使用,但完全不受维护和支持 It should not be used for any new work at this point. 这时不应将其用于任何新工作。


However, recent work greatly improved the support for bar and other categorical plots using the stable, supported bokeh.plotting API. 但是,最近的工作使用稳定的受支持的bokeh.plotting API大大改善了对条形图和其他分类图的支持。 There is large new User's Guide Section purely dedicated to explaining and demonstrating many kind of bar charts, both simple and sophisticated. 新的大型用户指南部分专门用于解释和演示多种条形图,包括简单和复杂的条形图。 Moreover, now that bar plots are easy to make using standard bokeh.plotting calls, the general guidance and documentation for hover tools now applies as well. 而且,由于现在可以使用标准的bokeh.plotting调用轻松制作bokeh.plotting ,因此现在也适用于悬停工具一般指导和文档

It's not quite clear to me from your example code what you are trying to accomplish. 从您的示例代码中,我不太清楚您要完成的工作。 Here is a very stripped down version of something that might be similar: 这是可能很相似的简化版本:

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)

That code produces this output: 该代码产生以下输出:

在此处输入图片说明

Here is a more complete example of a simple bar chart using pandas statistics (similar to what Bar would do) with hover tool using the "cars" sample data and the bokeh.plotting API: 以下是使用“汽车”样本数据和bokeh.plotting API的悬停工具,使用熊猫统计信息(类似于Bar所做的事情)的简单条形图的更完整示例:

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)

Which produces the following result 产生以下结果

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM