在 Bokeh 中将 'string' 类型转换为 'int' 时无法正确绘制图形
Not able to plot graph correctly when a 'string' type is converted to 'int' in Bokeh
我正在从 xml 获取一些属性值,这些属性值本质上是数字,但类型为 'string'
我正在将那些 'strings' 类型转换为 'int' ,并尝试在中绘制条形图散景。
该图未正确填充(附加)。有什么建议吗?
下面是代码
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 的一部分,此后已从核心 Bokeh 中完全删除。它仍然可以作为 bkcharts
包使用,但它 完全没有维护和支持 。此时不应将其用于任何新工作。
但是,最近的工作大大改进了对使用稳定的条形图和其他分类图的支持,支持 bokeh.plotting
API。 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 现在也适用。
从你的示例代码中我不太清楚你想要完成什么。这是一个非常精简的版本,可能与此类似:
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 统计数据(类似于 Bar
所做的),悬停工具使用 "cars" 样本数据和 bokeh.plotting
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)
产生以下结果
我正在从 xml 获取一些属性值,这些属性值本质上是数字,但类型为 'string'
我正在将那些 'strings' 类型转换为 'int' ,并尝试在中绘制条形图散景。
该图未正确填充(附加)
下面是代码
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 的一部分,此后已从核心 Bokeh 中完全删除。它仍然可以作为 bkcharts
包使用,但它 完全没有维护和支持 。此时不应将其用于任何新工作。
但是,最近的工作大大改进了对使用稳定的条形图和其他分类图的支持,支持 bokeh.plotting
API。 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 现在也适用。
从你的示例代码中我不太清楚你想要完成什么。这是一个非常精简的版本,可能与此类似:
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 统计数据(类似于 Bar
所做的),悬停工具使用 "cars" 样本数据和 bokeh.plotting
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)
产生以下结果