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

2024-05-29 05:08:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我在用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是上面的数据帧):

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

你知道怎么让酒吧部分运作吗?


Tags: 数据dflibrarybargraphpointexpectedpercent

热门问题