python - 如何在 python ggplot 中创建条形图?

标签 python pandas python-ggplot

我正在使用 yhat 的 ggplot library .我有以下 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

目前,我正在执行以下操作(函数第一行第一行返回的 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)

这是我得到的:

scatter

但是,每当我尝试使用 geom_bar() 而不是 geom_point() 时,我最终只会得到 100% 的条。我已经尝试了普通的 geom_bar()geom_bar(stat="percent observed"),但似乎都不起作用。这总是我得到的:

bar

我想做的是模仿/复制以下内容:

attempt

知道如何让条形部分(或整个部分)正常工作吗?

最佳答案

使用权重,这里是一个例子:

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()

输出如下:

enter image description here

关于python - 如何在 python ggplot 中创建条形图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22599521/

相关文章:

Python ggplot 问题绘制 >8 只股票和图例被截断

python - 类型错误 : unhashable type: 'slice' pandas DataFrame column

python - 如何使用numpy的einsum来获取子数组的点积?

python - Pandas 方法在整个 DataFrame 上应用函数

python - 如何拆分具有多个选项的 Pandas 系列?

python - 在 Python ggplot 中更改轴标签大小

python - 如何在 XIST python 模块中设置元素类属性

更高效的Python输入/输出

python - utf-8 编码在 pd.read_csv() 中给出错误

python - 创建 Pandas DataFrame 以与 ggPlot 线图一起使用