使用ggplot和pandas在Python中绘制事件密度

时间:2016-10-18 06:56:28

标签: python pandas data-analysis python-ggplot

我正在尝试可视化此表单的数据:

  timestamp               senderId
0     735217  106758968942084595234
1     735217  114647222927547413607
2     735217  106758968942084595234
3     735217  106758968942084595234
4     735217  114647222927547413607
5     etc...
如果我不将geom_density分开,则

senderId有效:

df = pd.read_pickle('data.pkl')
df.columns = ['timestamp', 'senderId']
plot = ggplot(aes(x='timestamp'), data=df) + geom_density()
print plot

结果看起来像预期的那样:

density plot

但是,如果我想单独显示senderId as is done in the doc,则会失败:

> plot = ggplot(aes(x='timestamp', color='senderId'), data=df) + geom_density()
ValueError: `dataset` input should have multiple elements.

尝试使用更大的数据集(~40K事件):

> plot = ggplot(aes(x='timestamp', color='senderId'), data=df) + geom_density()
numpy.linalg.linalg.LinAlgError: singular matrix

有什么想法吗?这些错误在SO上有一些答案,但似乎都没有。

这是我想要的那种图表(来自ggplot' doc):

density plot

1 个答案:

答案 0 :(得分:0)

使用较小的数据集:

> plot = ggplot(aes(x='timestamp', color='senderId'), data=df) + geom_density()
ValueError: `dataset` input should have multiple elements.

这是因为有些senderId只有一行。

使用更大的数据集:

> plot = ggplot(aes(x='timestamp', color='senderId'), data=df) + geom_density()
numpy.linalg.linalg.LinAlgError: singular matrix

这是因为对于某些senderId,我在完全相同的timestamp处有多行。 ggplot不支持此功能。我可以通过使用更精细的时间戳来解决它。