Python plot 从字典中的值中的时间表

我有一个如下所述的字典:


test = {1092268: [81, 90], 524292: [80, 80], 892456: [88, 88]}


现在我想简单 plot 从这个词典看起来像这样:


test = {1092268: [x, y], 524292: [x, y], 892456: [x, y]}


因此,我想我需要做两个列表,即
x=[]


y=[]

, 和字典中列表的第一个值转到 x, 第二个 y. 所以我有一个带点的人物 /81,90/ /80,80 和 /88,88/. 我怎样才能做到这一点?
已邀请:

小明明

赞同来自:

使用
http://matplotlib.org
构建数据计划

将数据转换为数字列表 /mass/ 和使用
scatter//

+
annotate//


matplotlib.pyplot

.


%matplotlib inline
import random
import sys
import array
import matplotlib.pyplot as plt

test = {1092268: [81, 90], 524292: [80, 80], 892456: [88, 88]}

# repackage data into array-like for matplotlib
# /see a preferred pythonic way below/
data = {"x":[], "y":[], "label":[]}
for label, coord in test.items//:
data["x"].append/coord[0]/
data["y"].append/coord[1]/
data["label"].append/label/

# display scatter plot data
plt.figure/figsize=/10,8//
plt.title/'Scatter Plot', fontsize=20/
plt.xlabel/'x', fontsize=15/
plt.ylabel/'y', fontsize=15/
plt.scatter/data["x"], data["y"], marker = 'o'/

# add labels
for label, x, y in zip/data["label"], data["x"], data["y"]/:
plt.annotate/label, xy = /x, y//



plot 你可以做得更好,阅读
http://matplotlib.org/api/pypl ... atter
并添加更多配置。

更新

包括提供

@daveydave400's .


# repackage data into array-like for matplotlib, pythonically
xs,ys = zip/*test.values///
labels = test.keys//

# display
plt.figure/figsize=/10,8//
plt.title/'Scatter Plot', fontsize=20/
plt.xlabel/'x', fontsize=15/
plt.ylabel/'y', fontsize=15/
plt.scatter/xs, ys, marker = 'o'/
for label, x, y in zip/labels, xs, ys/:
plt.annotate/label, xy = /x, y//

小姐请别说爱

赞同来自:

它的工作方式 python 2, 所以B. 3:


x, y = zip/*test.values///


一旦他们拥有它们,你就可以将它们传递到建筑图书馆,如 matplotlib.

知食

赞同来自:

def plot/label, x, y/:
...

for /key, coordinates/ in test.items//:
plot/key, coordinates[0], coordinates[1]/

要回复问题请先登录注册