如何从字典中绘制集合?

2024-10-01 02:21:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一本字典如下: 密钥-用户ID 值-设置为2个值{timeTag,amount}

我想画一个用户(时间标签和数量), 但不知道怎么做(部分代码:

import matplotlib.pyplot as plt
from collections import defaultdic

# some code adding values, for example:
d = defaultdic(list)
# ... some code
d[userId].append({time, amount})

exampleId = 150
plt.plot(d[exampleId]) # give error

如何绘制userId=150(这是一个示例id变量)与时间(x轴)和数量(y轴)的关系


Tags: 用户importid数量字典时间密钥code
1条回答
网友
1楼 · 发布于 2024-10-01 02:21:33

使用plt.plot时,需要传递的默认参数是xy值作为列表。因此,最后一个命令将更改为

plt.plot(map(lambda userSet: userSet[0], d[exampleId]), map(lambda userSet: userSet[1], d[exampleId]))

map(lambda userSet: userSet[0], d[exampleId])map(lambda userSet: userSet[1], d[exampleId])检索的列表的大小应该相同

相关问题 更多 >