【问题标题】:Different colors for scatter plots based on origin of data基于数据来源的散点图的不同颜色
【发布时间】:2018-01-28 12:06:24
【问题描述】:

我有一个名为“samples”的列表,我从 2 个不同的文件夹(例如 Folder1 和 Folder2)将几张图像加载到此列表中。然后我将此列表转换为 DataFrame 并将它们绘制在 2D 散点图中。我希望散点图将 Folder1 中的所有内容显示为红色,Folder2 中的所有内容显示为蓝色。我怎样才能做到这一点。我的代码如下:

    samples = []
    Folder1 = glob.iglob('/home/..../Folder1/*.png')
    Folder2 = glob.iglob('/home/..../Folder2/*.png')

    for fname in Folder1:
        img = misc.imread(fname)
        samples.append((img[::2, ::2] / 255.0).reshape(-1))

    for fname in Folder2:
        img = misc.imread(fname)
        samples.append((img[::2, ::2] / 255.0).reshape(-1))

    samples = pd.DataFrame(samples)

    def do_ISO(df):
        from sklearn import manifold
        iso = manifold.Isomap(n_neighbors=6, n_components=3)
        iso.fit(df)
        A = iso.transform(df)
        return A

    def Plot2D(T, title, x, y):
        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.set_title(title)
        ax.set_xlabel('Component: {0}'.format(x))
        ax.set_ylabel('Component: {0}'.format(y))
        x_size = (max(T[:,x]) - min(T[:,x])) * 0.08
        y_size = (max(T[:,y]) - min(T[:,y])) * 0.08
        ax.scatter(T[:,x],T[:,y], marker='.',alpha=0.7)

    Plot2D(do_ISO(samples), 'ISO_Chart', 0, 1)

    plt.show()

【问题讨论】:

    标签: python-2.7 matplotlib scatter-plot data-science


    【解决方案1】:

    如果没有看到您正在使用的数组,这很难说。您实际上是在绘制 do_ISO() 函数的结果,该函数使用 sklearn.manifold.Isomap.transform() 创建一个数组。

    此函数是否保留了数组中元素的顺序? 如果是这样,事情可能会相当容易。当您首先从 Folder1 填充所有图像,然后从 Folder2 填充所有图像时,您可以简单地计算 Folder1 中的项目数,并根据该数字将数组拆分为 2(例如nbFilesFolder1)。然后你给scatter打了2个电话:

    ax.scatter(T[:nbFilesFolder1,x],T[:nbFilesFolder1,y], marker='.',alpha=0.7, c='red')
    ax.scatter(T[nbFilesFolder1:,x],T[nbFilesFolder1:,y], marker='.',alpha=0.7, c='blue')
    

    【讨论】:

    • Diziet Asahi,你刚刚搞定了。这正是我想要的。是的,Folder1 包含 72 个项目,Folder2 包含 12 个项目。我使用你的技术通过拆分数组来绘制它们,它完全按照我想要的方式工作。非常感谢,我真的很感激。是你们激励像我这样的年轻数据分析师继续前进。
    猜你喜欢
    • 2021-02-26
    • 2019-11-27
    • 1970-01-01
    • 2013-06-26
    • 2018-10-02
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 2017-09-14
    相关资源
    最近更新 更多