【问题标题】:Bokeh Bar plot |散景条图 |
【发布时间】:2018-08-09 06:51:40
【问题描述】:

我正在尝试使用 Python 创建散景条形图。 data2 是值

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)

错误

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

我在这里做错了什么?

【问题讨论】:

    标签: python bar-chart bokeh


    【解决方案1】:

    bokeh.charts API(包括Bar)已于 2017 年弃用并从 Bokeh 中删除。它不受维护且不受支持,此时不应出于任何原因使用。使用得到良好支持的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)
    

    有关bokeh.plotting 中对条形图和分类图的大幅改进支持的更多信息,请参阅详尽的用户指南部分Handling Categorical Data

    【讨论】:

      【解决方案2】:

      来自 Bokeh 项目维护者的注意事项:此答案指的是一个过时和弃用的 API。有关使用现代且完全受支持的 Bokeh API 创建条形图的信息,请参阅其他回复。


      您可能希望将所有数据放在一个数据框中。

      要直接绘制你想要的东西而不这样做,你需要删除“values”关键字。

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

      不过,这不会添加您的 x-labels。为此,您可以执行以下操作:

      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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-11
        • 2021-01-01
        • 1970-01-01
        相关资源
        最近更新 更多