将列数据从 Pandas df 传递给散景绘图函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38467455/
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-14 01:37:15  来源:igfitidea点击:

Passing column data from pandas df to bokeh plotting function

pythonpandasplotboxplotbokeh

提问by JeremyD

I'm working on automating plotting functions for metabolomics data with bokeh. Currently, I'm trying to read in my dataframe from CSV and iterate through the columns generating box plots for each metabolite (column).

我正在研究使用散景自动绘制代谢组学数据的绘图功能。目前,我正在尝试从 CSV 读取我的数据帧并遍历列,为每个代谢物(列)生成箱线图。

I have an example df that looks like this:

我有一个 df 示例,如下所示:

Sample  Group   AMP     ADP     ATP
1A      A       239847  239084  987374
1B      A       245098  241210  988950
2A      B       238759  200554  921032
2B      B       230029  215408  89980

Here is what my code looks like:

这是我的代码的样子:

import pandas
from bokeh.plotting import figure, output_file, show, save
from bokeh.charts import BoxPlot

df = pandas.read_csv("testdata_2.csv")

for colname, col in df.iteritems():
p = BoxPlot(df, values=df[colname], label='Group', xlabel='Group', ylabel='Peak Area',
             title=colname)
    output_file("boxplot.html")
    show(p)

This generates an error:

这会产生一个错误:

raise ValueError("expected an element of either %s, got %r" % (nice_join(self.type_params), value))

ValueError: expected an element of either Column Name or Column String or List(Column Name or Column String

It seems that setting values=df[colname]is the issue. If I replace it with values=df['colname']it gives me a key error for colname. I can plot just fine if I specify a given column such as values='ATP'but I need to be able loop through all columns.

看来是设置values=df[colname]的问题。如果我用它替换它values=df['colname']会给我一个 colname 的关键错误。如果我指定一个给定的列,我可以很好地绘制,例如,values='ATP'但我需要能够遍历所有列。

Any guidance? Is this even the best approach?

任何指导?这甚至是最好的方法吗?

Thanks in advance.

提前致谢。

回答by Pablo Reyes

If you want to organize them horizontally, you can create different graphs, and then you could use for instance hplot from bokeh.io as follows:

如果你想水平组织它们,你可以创建不同的图形,然后你可以使用 bokeh.io 中的 hplot,如下所示:

import pandas
from bokeh.plotting import figure, output_file, show, save
from bokeh.charts import BoxPlot
from bokeh.io import hplot

df = pandas.read_csv("testdata_2.csv")

p = []
for colname in ['AMP','ADP','ATP']:
    p += [BoxPlot(df, values=colname, label='Group', xlabel='Group',
            ylabel='Peak Area',title=colname, width=250,height=250)]

output_file("boxplot.html")
show(hplot(*p))

For your particular example I get: enter image description here

对于您的特定示例,我得到: 在此处输入图片说明