如何迭代散点图的列表列表并创建唯一元素的图例

How to iterate a list of list for a scatter plot and create a legend of unique elements

背景:

我有一个 list_of_x_and_y_list,其中包含 xy 值,如下所示:

[[(44800, 14888), (132000, 12500), (40554, 12900)], [(None, 193788), (101653, 78880), (3866, 160000)]]

我还有一个data_name_list["data_a","data_b"]所以

list_of_x_and_y_listlen / 或 data_name_listlen > 20。

问题:

如何为 data_name_list 中的每个项目(相同颜色)创建散点图?

我试过的:

   fig = plt.figure()
   ax = fig.add_subplot(1, 1, 1)
   ax = plt.axes(facecolor='#FFFFFF')
   prop_cycle = plt.rcParams['axes.prop_cycle']
   colors = prop_cycle.by_key()['color']

   print(list_of_x_and_y_list)
   for x_and_y_list, data_name, color in zip(list_of_x_and_y_list, data_name_list, colors):
       for x_and_y in x_and_y_list,:
          print(x_and_y)
          x, y = x_and_y
          ax.scatter(x, y, label=data_name, color=color) # "label=data_name" creates 
                                                         # a huge list as a legend! 
                                                         # :(


       plt.title('Matplot scatter plot')
       plt.legend(loc=2)
       file_name = "3kstc.png"
       fig.savefig(file_name, dpi=fig.dpi)
       print("Generated: {}".format(file_name))

问题:

图例好像是一个很长的列表,不知道怎么改:

相关研究:

你得到一个长重复列表作为图例的原因是因为你将每个点作为一个单独的系列提供,因为 matplotlib 不会根据标签自动对你的数据进行分组。

一个快速修复方法是迭代列表并将每个系列的 x 值和 y 值压缩为两个元组,这样 x 元组包含所有 x 值和y 元组 y 值。

然后您可以将这些元组与标签一起提供给 plt.plot 方法。

我觉得名字 list_of_x_and_y_list 不必要地又长又复杂,所以在我的代码中我使用了较短的名字。

import matplotlib.pyplot as plt

data_series = [[(44800, 14888), (132000, 12500), (40554, 12900)],
               [(None, 193788), (101653, 78880), (3866, 160000)]]
data_names = ["data_a","data_b"]

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax = plt.axes(facecolor='#FFFFFF')
prop_cycle = plt.rcParams['axes.prop_cycle']
colors = prop_cycle.by_key()['color']

for data, data_name, color in zip(data_series, data_names, colors):
    x,y = zip(*data)
    ax.scatter(x, y, label=data_name, color=color)
    plt.title('Matplot scatter plot')
    plt.legend(loc=1)

要每个 data_name 只获得一个条目,您应该只添加一次 data_name 作为标签。其余调用应与 label=None 一起进行。 使用当前代码可以实现的最简单方法是在循环结束时将 data_name 设置为 None

from matplotlib import pyplot as plt
from random import randint

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.set_facecolor('#FFFFFF')
# create some random data, suppose the sublists have different lengths
list_of_x_and_y_list = [[(randint(1000, 4000), randint(2000, 5000)) for col in range(randint(2, 10))]
                        for row in range(10)]
data_name_list = list('abcdefghij')
colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
for x_and_y_list, data_name, color in zip(list_of_x_and_y_list, data_name_list, colors):
    for x_and_y in x_and_y_list :
        x, y = x_and_y
        ax.scatter(x, y, label=data_name, color=color)
        data_name = None
plt.legend(loc=2)
plt.show()

有些东西可以简化,使代码'more pythonic',例如:

for x_and_y in x_and_y_list :
    x, y = x_and_y

可以写成:

for x, y in x_and_y_list:

另一个问题是,如果有大量数据,对每个点调用 scatter 可能会很慢。属于同一列表的所有 x 和 y 都可以绘制在一起。例如使用 list comprehension:

for x_and_y_list, data_name, color in zip(list_of_x_and_y_list, data_name_list, colors):
    xs = [x for x, y in x_and_y_list]
    ys = [y for x, y in x_and_y_list]
    ax.scatter(xs, ys, label=data_name, color=color)

scatter 甚至可以获得每个点的颜色列表,但是一次性绘制所有点,不允许每个 data_name.

的标签

很多时候,numpy用于存储数值数据。这有一些优点,例如用于快速计算的矢量化。使用 numpy 代码看起来像:

import numpy as np

for x_and_y_list, data_name, color in zip(list_of_x_and_y_list, data_name_list, colors):
    xys = np.array(x_and_y_list)
    ax.scatter(xys[:,0], xys[:,1], label=data_name, color=color)