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

Plotting event density in Python with ggplot and pandas

我正在尝试可视化这种形式的数据:

  timestamp               senderId
0     735217  106758968942084595234
1     735217  114647222927547413607
2     735217  106758968942084595234
3     735217  106758968942084595234
4     735217  114647222927547413607
5     etc...

geom_density 如果我不分开 senderIds:

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

结果符合预期:

但是,如果我想单独显示 senderIdas 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 上有一些针对这些错误的答案,但 none 似乎相关。

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

使用较小的数据集:

> 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,我有多行完全相同 timestampggplot 不支持此功能。我可以通过使用更精细的时间戳来解决它。