Bokeh Bar plot |

别说谁变了你拦得住时间么 提交于 2021-01-28 20:13:18

问题


I am trying to Create a bokeh bar chart using Python. The data2 is the values

from bokeh.plotting import figure, output_file, show,hplot
from bokeh.charts import Bar

data2=[65.75, 48.400000000000006, 58.183333333333337, 14.516666666666666]


bar = Bar(values=data2,xlabel="beef,pork,fowl,fish",ylabel="Avg consumtion", width=400)

show(bar)

Error

TypeError: Bar() takes at least 1 argument (1 given)

What am I doing wrong Here?


回答1:


The bokeh.charts API (including Bar) was deprecated and removed from Bokeh in 2017. It is unmaintained and unsupported and should not be used for any reason at this point. A simple bar chart can be accomplished using the well-supported bokeh.plotting API:

from bokeh.plotting import figure, show

categories = ["beef", "pork", "fowl", "fish"]
data = [65.75, 48.4, 58.183, 14.517]

p = figure(x_range=categories)
p.vbar(x=categories, top=data, width=0.9)

show(p)

For more information on the vastly improved support for bar and categorical plots in bokeh.plotting, see the extensive user guide section Handling Categorical Data




回答2:


Note from Bokeh project maintainers: This answer refers to an obsolete and deprecated API. For information about creating bar charts with modern and fully supported Bokeh APIs, see the other response.


You might want to put all of your data in a data frame.

To directly plot what you want without doing that, you need to remove the "values" keyword.

bar = Bar(data2,xlabel="beef,pork,fowl,fish",ylabel="Avg consumtion", width=400)

This won't add your x-labels though. To do that you can do the following:

from bokeh.plotting import figure, output_file, show,hplot
from bokeh.charts import Bar
from pandas import DataFrame as df
data2=[65.75, 48.400000000000006, 58.183333333333337, 14.516666666666666]

myDF = df(
  dict(
    data=data2,
    label=["beef", "pork", "fowl", "fish"],
    ))

bar = Bar(myDF, values="data",label="label",ylabel="Avg consumption", width=400)

show(bar)


来源:https://stackoverflow.com/questions/36959406/bokeh-bar-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!