pandas 如何在 python ggplot 中创建条形图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/22599521/
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
How do I create a bar chart in python ggplot?
提问by Clay
I'm using yhat's ggplot library. I have the following pandas DataFrame:
我正在使用 yhat 的ggplot 库。我有以下Pandas数据帧:
   degree  observed  percent observed  expected  percent expected
0       0         0               0.0         0          0.044551
1       1         1               0.1         1          0.138604
2       2         3               0.3         2          0.215607
3       3         4               0.4         2          0.223592
4       4         1               0.1         2          0.173905
5       5         1               0.1         1          0.108208
At the moment, I'm doing the following (where dfreturned in the first line in the first line in the function is the DataFrame above):
目前,我正在执行以下操作(在df函数第一行的第一行中返回的是上面的 DataFrame):
def chartObservedExpected(graph):
    df = observedExpected(graph)
    return ggplot(aes(x='degree', y='percent observed'), data=df) + \
           geom_point() + \
           ylim(-0.015,1.015) + \
           xlim(-0.05,max(df['degree']) + 0.25) + \
           labs("Degree","Proportion of Total") + \
           ggtitle("Observed Node Degree Distribution")
chartObservedExpected(G)
This is what I get:
这就是我得到的:


However, whenever I try geom_bar()instead of geom_point(), I end up with just 100% bars. I've tried just plain geom_bar()and also geom_bar(stat="percent observed"), but neither seem to work. This is always what I get:
但是,每当我尝试geom_bar()代替 时geom_point(),我最终都会得到 100% 的条形。我已经尝试过简单geom_bar()和也geom_bar(stat="percent observed"),但似乎都不起作用。这总是我得到的:


What I'm trying to do is to mimic/reproduce the following:
我想要做的是模仿/重现以下内容:


Any idea how to get the bar part working (or the whole thing, for that matter)?
知道如何让酒吧部分工作(或整个事情,就此而言)?
回答by HYRY
Use weight, here is an example:
使用weight,这里是一个例子:
from ggplot import *
import pandas as pd
df = pd.DataFrame({"x":[1,2,3,4], "y":[1,3,4,2]})
ggplot(aes(x="x", weight="y"), df) + geom_bar()
the output looks like:
输出看起来像:



