使用matplotlib绘制[x,y]

时间:2017-04-06 22:50:11

标签: python matplotlib

我正在尝试使用下面的代码绘制knn的散点图,它给出了以下错误:

  

TypeError:zip参数#1必须支持迭代

l = [[[0.676, 1.064], [0.828, 1.038], [0.913, 0.82], [0.857, 0.948], [0.723, 1.064], [0.792, 0.888], [0.881, 1.036], [0.872, 0.893], [0.748, 0.965], [0.837, 0.883], [0.841, 0.925], [0.805, 0.875], [0.87, 1.068], [0.831, 0.859]], [[0.764, 0.899], [0.774, 0.875], [0.573, 0.896], [0.728, 0.916], [0.795, 0.724], [0.736, 0.845], [0.753, 0.837], [0.842, 0.778], [0.54, 0.841]], [[0.245, 0.243], [0.322, 0.219], [0.334, 0.159], [0.211, 0.136], [0.161, 0.305], [0.174, 0.079], [0.057, 0.073], [0.321, 0.139], [0.225, 0.208], [0.253, 0.18], [0.37, 0.068], [0.435, 0.222], [0.258, 0.091], [0.421, -0.095], [0.354, 0.056], [0.253, 0.137], [0.214, 0.194], [0.289, 0.012], [0.229, 0.142], [0.469, 0.319], [0.248, 0.332], [0.372, 0.381], [0.328, 0.243], [0.318, 0.257], [0.389, 0.1], [0.146, 0.152], [0.351, 0.165], [0.453, 0.124], [0.375, 0.078], [0.156, 0.027], [0.156, 0.19], [0.285, -0.107], [0.298, 0.069], [0.269, 0.124], [0.239, 0.082], [0.388, 0.132], [0.324, 0.015], [0.32, 0.013], [0.22, 0.201]], [[0.366, 0.772], [0.225, 0.757], [0.246, 0.498], [0.266, 0.828], [-0.028, 0.868], [0.162, 0.536], [0.224, 0.552], [0.206, 0.876], [0.125, 0.679], [0.105, 0.593], [0.219, 0.639], [0.106, 0.675], [0.216, 0.666], [0.227, 0.651], [0.297, 0.447], [0.18, 0.695], [0.051, 0.577], [0.227, 0.682], [0.265, 0.699], [0.121, 0.643], [0.062, 0.652], [0.278, 0.696], [0.173, 0.698], [0.27, 0.745], [0.207, 0.658], [0.324, 0.635], [0.076, 0.756], [0.142, 0.541], [0.073, 0.601], [0.104, 0.682], [0.086, 0.747], [0.091, 0.687], [0.091, 0.765], [0.187, 0.564], [0.233, 0.958], [0.217, 0.52], [0.274, 0.804], [0.357, 0.828]]]


plt.scatter(*zip(*l), c='r', s=100)

1 个答案:

答案 0 :(得分:0)

您的数据位于list lists lists中,并尝试从中获取散点图可能需要将主list展平为{{} 1 {} list;从而将尺寸减小到2D。虽然以下不是从数据中获取散点图的唯一方法,但它仍然是一个不错的起点:

lists

还有其他方法可以减少import matplotlib.pyplot as plt from functools import reduce plt.scatter(*zip(*reduce(lambda x,y: x+y, l)), c='r', s=100) plt.show() 的尺寸。这个恰好对我来说很好。

我希望这会有所帮助。