【问题标题】:Ploting nested lists in dictionary - python在字典中绘制嵌套列表 - python
【发布时间】:2018-11-25 22:35:08
【问题描述】:

我的数据集看起来像这样:

{'Berlin': [[1, 333]],
 'London': [[1, 111], [2, 555]],
 'Paris': [[1, 444], [2, 222], [3, 999]]}

现在我需要为每个城市制作散点图,例如,巴黎的图应该在 x 轴数字上:1 2 3,在 y 轴上:444 222 999。

我有一个非常大的数据集,所以我希望每个地块有 5 个城市。

我的代码看起来像这样,但在输出中我得到空图。

for town in dictionary_town:
    x=[]
    y=[]
    i+=1
    save_plot = 'path'+str(i)+'.png'

    for list in town:
        x.append(list[0])
        y.append(list[1])

    plt.style.use('dark_background')
    plt.figure()
    plt.plot( kind='scatter',x = x, y=y,label=town, s=30,figsize=(50, 20), fontsize=25, alpha=0.5)
    plt.ylabel("Population", fontsize=50)
    plt.title(str(i), fontsize=50)
    legend = plt.legend(fontsize=30, markerscale=0.5, ncol=1)
    plt.savefig(save_plot)
    plt.close()

【问题讨论】:

    标签: python python-3.x pandas scatter-plot


    【解决方案1】:

    这里有一段最少的工作代码,只是为了专注于保存图表。为了简单起见,我故意省略了代码中的图形样式部分。

    import matplotlib.pyplot as plt
    
    dictionary_town = {'Berlin': [[1, 333]],
                       'London': [[1, 111], [2, 555]],
                       'Paris': [[1, 444], [2, 222], [3, 999]]}
    
    path = '/your_path_to_image_saving/' #pls change here
    i = 0
    for town, values in dictionary_town.items():
        x = []
        y = []
        i += 1
        save_plot = path + str(i) + '.png'
        for value in values:
            x.append(value[0])
            y.append(value[1])
        plt.figure()
        plt.scatter(x=x, y=y, label=town)
        plt.ylabel("Population")
        plt.title(str(i))
        plt.legend(loc=4)
        plt.savefig(save_plot)
        plt.close()
    

    我建议你从这里开始,一一添加一些样式,以找出问题所在。

    希望这会有所帮助。


    编辑: 一次绘制 5 个城市。如果要更改城市数量,请更改 chunk。此外,如果城市数需要大于 5,则需要相应添加 colors

    import matplotlib.pyplot as plt
    import numpy as np
    
    dictionary_town = {'Berlin': [[1, 333]],
                       'London': [[1, 111], [2, 555]],
                       'Paris': [[1, 444], [2, 222], [3, 999]]}
    
    colors = {1: 'r', 2: 'b', 3: 'g', 4: 'k', 5: 'grey'}
    
    path = '/your_path_to_image_saving/' #pls change here
    i = 0
    chunk = 5
    for town, values in dictionary_town.items():
        i += 1
        values = np.array(values)
        x = values[:,0]
        y = values[:,1]
        plt.scatter(x=x, y=y, label=town, color=colors[(i+1)%chunk+1])
        if i % chunk == 0:
            save_plot = path + str(i) + '.png'
            plt.ylabel("Population")
            plt.title(str(i))
            plt.legend(loc=4)
            plt.savefig(save_plot)
            plt.close()
        elif (i == len(dictionary_town)) & (i % chunk != 0):
            save_plot = path + str(i) + '.png'
            plt.ylabel("Population")
            plt.title(str(i))
            plt.legend(loc=4)
            plt.savefig(save_plot)
            plt.close()
    

    【讨论】:

    • 是的,它正在工作。 :) 谢谢各位.. 是否可以在每个地块中放置 5 个城镇?因为,现在我得到了 185 个地块,而且我想少于那个。 :D
    • @Neven 我更新了我的代码以一次绘制 5 个城市。
    【解决方案2】:

    您可以简单地使用zip 转置您的列表 为了将x, y 对转换为列表:

    import matplotlib.pyplot as plt
    
    dictionary_town = {'Berlin': [[1, 333]],
     'London': [[1, 111], [2, 555]],
     'Paris': [[1, 444], [2, 222], [3, 999]]}
    
    
    for i, town in enumerate(dictionary_town):
        x, y = zip(*dictionary_town[town])
        save_plot = 'path'+str(i)+'.png'
    
        plt.style.use('dark_background')
        fig = plt.figure(figsize=(50, 20))
        plt.scatter(x, y, s=30,label=town, alpha=0.5)
        plt.ylabel("Population", fontsize=50)
        plt.title(str(i), fontsize=50)
        plt.legend(fontsize=30, markerscale=0.5, ncol=1)
        plt.savefig(save_plot)
        plt.close(fig)
    

    【讨论】:

    • 我收到一个关键错误。 :/ KeyError异常:的[[[1.0,0.07841227279285981],[2.0,0.08206794681681895],[3.0,0.08595926719786834],[4.0,0.06782354709176948],[9995.0,0.011077215571876565],[9996.0,0.003400169925570253]]]”没有一个是在[索引]'
    • 您是否将数据集存储在 pandas 数据框中?
    猜你喜欢
    • 1970-01-01
    • 2021-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多