如何在python ggplot中创建条形图? [英] How do I create a bar chart in python ggplot?

查看:90
本文介绍了如何在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()

输出如下:

这篇关于如何在python ggplot中创建条形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆