【问题标题】:How do I create a bar chart in python ggplot?如何在 python ggplot 中创建条形图?
【发布时间】:2014-05-01 05:26:05
【问题描述】:

我正在使用 yhat 的 ggplot library。我有以下熊猫数据框:

   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)

这是我得到的:

但是,每当我尝试使用 geom_bar() 而不是 geom_point() 时,我最终只能得到 100% 的条形图。我试过简单的geom_bar()geom_bar(stat="percent observed"),但似乎都不起作用。这总是我得到的:

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

知道如何让酒吧部分工作(或整个事情,就此而言)?

【问题讨论】:

  • 看起来像一个尚未移植的错误/东西。请参阅 Github here 上的问题。如果它正在工作,它会类似于 ggplot(aes(x='degree'), df) + geom_bar(aes(y='percent_observed'), stat='identity'),但看起来像 stats 而不是 'bin' 还不能工作。
  • @Marius 好的,谢谢。目前没什么大不了的,但我希望他们尽快实施!

标签: python pandas python-ggplot


【解决方案1】:

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

输出如下:

【讨论】:

  • 谢谢!如果您尝试使用 xlim 设置 x 轴的范围,这看起来有点时髦,但它似乎工作得很好。我也无法更改条的宽度。
  • 花了很多时间尝试使用stat='identity' 执行此操作,但没有成功。确实如此。
猜你喜欢
  • 1970-01-01
  • 2011-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多