使用 Bokeh 绘制整个 Pandas DataFrame

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28655096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:58:17  来源:igfitidea点击:

Plot the whole pandas DataFrame with Bokeh

pythonpandasbokeh

提问by Anarcho-Chossid

I would like to plot a whole pandas DataFrame with Bokeh. I.e., I am looking for a Bokeh equivalent of the third line:

我想用 Bokeh 绘制整个 Pandas DataFrame。即,我正在寻找相当于第三行的 Bokeh:

import pandas as pd
income_df = pd.read_csv("income_2013_dollars.csv", sep='\t', thousands=',')
income_df.plot(x="year")

Is there currently a way to do that, or do I have to pass each y-value separately?

目前有没有办法做到这一点,还是我必须分别传递每个 y 值?

回答by birdsarah

Note from Bokeh project maintainers:This answer refers to an obsolete and deprecated API was long since removed from Bokeh. For information about creating bar charts with modern and fully supported Bokeh APIs, see other Questions/Answers.

Bokeh 项目维护者的注意事项:此答案指的是一个过时且已弃用的 API 早已从 Bokeh 中删除。有关使用现代且完全受支持的 Bokeh API 创建条形图的信息,请参阅其他问题/答案。



You may find the charts examples useful:

您可能会发现图表示例很有用:

https://github.com/bokeh/bokeh/tree/master/examples/charts

https://github.com/bokeh/bokeh/tree/master/examples/charts

If you wanted a bar chart it would be:

如果你想要一个条形图,它会是:

from bokeh.charts import Bar
Bar(income_df, notebook=True).show()  # assuming the index is corretly set on your df

You may want a Lineor TimeSerieswhich work similarly - just checkout the examples for more details and more configuration - like adding titles, labels etc.

您可能需要一个LineTimeSeries类似的工作方式 - 只需查看示例以获取更多详细信息和更多配置 - 例如添加标题、标签等。

Note that you can use other output methods - notebook, file, or server. See the documentation here: http://docs.bokeh.org/en/latest/docs/user_guide/charts.html#generic-arguments

请注意,您可以使用其他输出方法 - 笔记本、文件或服务器。请参阅此处的文档:http: //docs.bokeh.org/en/latest/docs/user_guide/charts.html#generic-arguments

Update: (sorry for the confusion on how to display the output). An alternative way of specifying the display type of the chart is to use the methods output_notebook(), output_file("file.html"), output_server()and then use the show method. For example

更新:(对于如何显示输出的混淆感到抱歉)。指定图表显示类型的另一种方法是使用方法output_notebook()output_file("file.html")output_server()然后使用 show 方法。例如

from bokeh.charts import Bar
from bokeh.plotting import output_notebook, show
output_notebook()
bar = Bar(income_df)
show(bar)

However, you cannotdo the following

但是,您不能执行以下操作

from bokeh.charts import Bar
from bokeh.plotting import output_notebook
output_notebook()
Bar(income_df).show()  # WILL GIVE YOU AN ERROR

The two show methods are different.

两种展示方式不同。

回答by bigreddot

See this User's Guide Section for modern information on creating Bar charts with Pandas:

有关使用 Pandas 创建条形图的现代信息,请参阅此用户指南部分:

https://docs.bokeh.org/en/latest/docs/user_guide/categorical.html#pandas

https://docs.bokeh.org/en/latest/docs/user_guide/categorical.html#pandas

For example:

例如:

from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource
from bokeh.palettes import Spectral5
from bokeh.plotting import figure
from bokeh.sampledata.autompg import autompg as df
from bokeh.transform import factor_cmap

output_file("groupby.html")

df.cyl = df.cyl.astype(str)
group = df.groupby('cyl')

source = ColumnDataSource(group)

cyl_cmap = factor_cmap('cyl', palette=Spectral5, factors=sorted(df.cyl.unique()))

p = figure(plot_height=350, x_range=group, title="MPG by # Cylinders",
           toolbar_location=None, tools="")

p.vbar(x='cyl', top='mpg_mean', width=1, source=source,
       line_color=cyl_cmap, fill_color=cyl_cmap)

p.y_range.start = 0
p.xgrid.grid_line_color = None
p.xaxis.axis_label = "some stuff"
p.xaxis.major_label_orientation = 1.2
p.outline_line_color = None

show(p)

enter image description here

在此处输入图片说明