【问题标题】:how to plot an histogram with python ggplot?如何使用 python ggplot 绘制直方图?
【发布时间】:2014-07-11 00:57:55
【问题描述】:

我想绘制一个直方图,表示 y 轴上的值 TP 和 x 轴上的方法。特别是我想根据“数据”列的值获得不同的数字。

在这种情况下,我想要一个值为 2,1,6,9,8,1,0 的第一个直方图和一个值为 10,10,16,...的第二个直方图...

ggplot 的 python 版本似乎与 R 版本略有不同。

            FN FP  TN  TP                   data  method
method                                              
SS0208  18  0  80   2  A p=100 n=100 SNR=0.5  SS0208
SS0408  19  0  80   1  A p=100 n=100 SNR=0.5  SS0408
SS0206  14  9  71   6  A p=100 n=100 SNR=0.5  SS0206
SS0406  11  6  74   9  A p=100 n=100 SNR=0.5  SS0406
SS0506  12  6  74   8  A p=100 n=100 SNR=0.5  SS0506
SS0508  19  0  80   1  A p=100 n=100 SNR=0.5  SS0508
LKSC    20  0  80   0  A p=100 n=100 SNR=0.5    LKSC
SS0208  10  1  79  10   A p=100 n=100 SNR=10  SS0208
SS0408  10  0  80  10   A p=100 n=100 SNR=10  SS0408
SS0206   4  5  75  16   A p=100 n=100 SNR=10  SS0206

作为第一步,我尝试仅绘制一个直方图,但收到错误消息。

 df = df[df.data == df.data.unique()[0]]

In [65]: ggplot() + geom_bar(df, aes(x='method', y='TP'), stat='identity')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-65-dd47b8d85375> in <module>()
----> 1 ggplot() + geom_bar(df, aes(x='method', y='TP'), stat='identity')

TypeError: __init__() missing 2 required positional arguments: 'aesthetics' and 'data'w

In [66]: 

我尝试了不同的命令组合,但没有解决。

一旦解决了第一个问题,我希望根据“数据”的值对直方图进行分组。这可能由'facet_wrap' 完成

【问题讨论】:

标签: python ggplot2 histogram python-ggplot


【解决方案1】:

这可能是因为您在没有参数的情况下调用了ggplot()(不确定这是否可行。如果您认为可以,请在http://github.com/yhat/ggplot 上添加问题)。

无论如何,这应该工作:

ggplot(df, aes(x='method', y='TP')) + geom_bar(stat='identity')

不幸的是,使用 geom_bar 进行刻面还不能正常工作(仅当所有刻面都具有所有级别/x 值时!)-> Bugreport

【讨论】:

    猜你喜欢
    • 2023-01-07
    • 2014-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多