【问题标题】:How to plot paired data with different colors considering their labels如何根据标签绘制具有不同颜色的配对数据
【发布时间】:2018-11-28 13:32:22
【问题描述】:

我有两个列表,第一个是配对数据的列表(数据包含 76 对),第二个是对应的标签(标签包含 0 到 11 之间的 76 个数字)。我需要 11 种不同的颜色。 考虑到它的标签,我需要用特定颜色绘制这些配对数据中的每一个。 这两个列表是这样的:

data=[[2,6],[1,7],[3,8],[4,6],[7,9]] 
labels=[1,0,0,2,2]

我是 python 的初学者。任何帮助将不胜感激

【问题讨论】:

    标签: python matplotlib plot colors scatter


    【解决方案1】:
    import matplotlib.pyplot as plt
    
    data=[[2,6],[1,7],[3,8],[4,6],[7,9]] 
    labels=[1,0,0,2,2] #labels should have the same length as data
    x = [i[0] for i in data] #separates the pairs into just the x component
    y = [i[1] for i in data] #separates the pairs into just the y component
    
    plt.scatter(x, y, c = labels, s = 50, cmap = 'rainbow')
    plt.show()
    

    “c = 标签”的意思是“使用标签值来选择颜色” “s = 50”决定了点的大小,根据需要进行调整,以便您可以看到颜色 "cmap = 'rainbow'" 决定了您用于颜色的颜色图。

    这是 matplotlib 的颜色图及其名称:matplotlib colormaps

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 2016-11-18
      • 2020-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多