Kishan Jangam
Kishan Jangam

Reputation: 39

Bokeh Bar plot |

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?

Upvotes: 3

Views: 6705

Answers (2)

kwu
kwu

Reputation: 9

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)

Upvotes: 0

bigreddot
bigreddot

Reputation: 34568

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)

enter image description here

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

Upvotes: 3

Related Questions