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

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

本文介绍了如何迭代散点图的列表列表并创建独特元素的图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

我有一个 list_of_x_and_y_list 包含 xy 值,它看起来像:

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

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

  • "data_a" = [(44800,14888),(132000,12500),(40554,12900)]

  • "data_b" = [(None, 193788), (101653, 78880), (3866, 160000)]

list_of_x_and_y_list len /或 data_name_list len 为>20.

问题:

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

我尝试过的:

  fig = plt.figure()ax = fig.add_subplot(1, 1, 1)斧= plt.axes(facecolor ='#FFFFFF')prop_cycle = plt.rcParams['axes.prop_cycle']颜色= prop_cycle.by_key()['颜色']打印(list_of_x_and_y_list)对于x_and_y_list,data_name,zip中的颜色(list_of_x_and_y_list,data_name_list,颜色):对于 x_and_y_list 中的 x_and_y,:打印(x_and_y)x,y = x_and_yax.scatter(x,y,label = data_name,color = color)#"label = data_name"创建#庞大的传说清单!# :(plt.title('Matplot散点图')plt.legend(loc = 2)file_name = "3kstc.png"fig.savefig(file_name,dpi = fig.dpi)print("Generated:{}".format(file_name))

问题:

这个图例好像很长,不知道怎么改:

相关研究:

  • Background:

    I have a list_of_x_and_y_list that contains x and y values which looks like:

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

    I have another data_name_list ["data_a","data_b"] so that

    • "data_a" = [(44800, 14888), (132000, 12500), (40554, 12900)]

    • "data_b" = [(None, 193788), (101653, 78880), (3866, 160000)]

    The len of list_of_x_and_y_list / or len of data_name_list is > 20.

    Question:

    How can I create a scatter plot for each item (being the same colour) in the data_name_list?

    What I have tried:

       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))
    

    The Problem:

    The legend appears to be a very long list, which I don't know how to rectify:

    Relevant Research:

    解决方案

    The reason you get a long repeated list as a legend is because you are providing each point as a separate series, as matplotlib does not automatically group your data based on the labels.

    A quick fix is to iterate over the list and zip together the x-values and the y-values of each series as two tuples, so that the x tuple contains all the x-values and the y tuple the y-values.

    Then you can feed these tuples to the plt.plot method together with the labels.

    I felt that the names list_of_x_and_y_list were uneccessary long and complicated, so in my code I've used shorter names.

    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)
    

    这篇关于如何迭代散点图的列表列表并创建独特元素的图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:28