简体   繁体   English

如何使用python ggplot绘制直方图?

[英]how to plot an histogram with python ggplot?

I would like to plot an histogram representing the value TP on the y axis and the method on the x axis. 我想在y轴上绘制表示TP值的直方图,在x轴上绘制表示方法的直方图。 In particular I would like to obtain different figures according to the value of the column 'data'. 特别是,我想根据“数据”列的值获得不同的数字。

In this case I want a first histogram with values 2,1,6,9,8,1,0 and a second histogram with values 10,10,16,... 在这种情况下,我想要第一个直方图的值为2,1、6、9、8、1,0和第二个直方图的值为10、10、16,...

The python version of ggplot seems to be slightly different by the R ones. 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

As a first step I have tried to plot only one histogram and I received an error. 第一步,我尝试仅绘制一个直方图,但收到一个错误。

 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]: 

I have tried different combinations of commands but I did not solve. 我尝试了不同的命令组合,但没有解决。

Once that this first problem has been solved I would like the histograms grouped according to the value of 'data'. 一旦解决了第一个问题,我希望根据“数据”的值对直方图进行分组。 This could probably be done by 'facet_wrap' 这可能可以通过'facet_wrap'

This is probably because you called ggplot() without an argument (Not sure if that should be possible. If you think so, please add a issue on http://github.com/yhat/ggplot ). 这可能是因为您在不带参数的情况下调用了ggplot() (不确定是否应该这样做。如果您这样认为,请在http://github.com/yhat/ggplot上添加一个问题)。

Anyway, this should work: 无论如何,这应该起作用:

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

Unfortunately, faceting with geom_bar doesn't work yet properly (only when all facets have all levels/ x values!) -> Bugreport 不幸的是,使用geom_bar进行构面尚不能正常工作(仅当所有构面都具有所有level / x值时才可以 !)-> 错误报告

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM