如何用Python ggplot绘制直方图?
我想画一个柱状图,y轴表示值TP,x轴表示方法。特别是,我想根据'数据'这一列的值来得到不同的图。
在这种情况下,我想要第一个柱状图的值是2,1,6,9,8,1,0,第二个柱状图的值是10,10,16,等等。
Python版本的ggplot似乎和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]:
我尝试了不同的命令组合,但没有解决问题。
一旦这个第一个问题解决后,我想要根据'data'的值将柱状图分组。这个可能可以通过'facet_wrap'
来实现。
1 个回答
0
这可能是因为你在调用 ggplot()
时没有传入任何参数(不确定这样做是否应该可以。如果你认为可以,请在 http://github.com/yhat/ggplot 上提个问题)。
不过,这样做应该可以正常运行:
ggplot(df, aes(x='method', y='TP')) + geom_bar(stat='identity')
不幸的是,使用 geom_bar 进行分面还不能正常工作(只有当所有分面都有所有的级别/x 值时才行!) -> 错误报告