如何在python ggplot中创建条形图?
我正在使用yhat的 ggplot库.我有以下熊猫DataFrame:
I'm using yhat's ggplot library. I have the following pandas DataFrame:
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
此刻,我正在执行以下操作(其中,函数第一行中第一行中返回的df
是上面的DataFrame):
At the moment, I'm doing the following (where df
returned in the first line in the first line in the function is the DataFrame above):
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)
这就是我得到的:
但是,每当我尝试使用geom_bar()
而不是geom_point()
时,最终我只会得到100%的柱线.我只尝试了普通的geom_bar()
和geom_bar(stat="percent observed")
,但是似乎都没有用.这始终是我得到的:
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:
我想做的是模仿/复制以下内容:
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)?
使用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()
输出如下: