【问题标题】:How to change scatter plot color according to certain rule如何根据一定的规则改变散点图颜色
【发布时间】:2016-01-17 22:29:27
【问题描述】:

我必须做一个散点图,它的颜色取决于第三个变量。 如果变量介于 0 和 1 之间,则给出“蓝色”、1-2、红色、2-3、紫色、3-4、绿色、4-5 灰色。 我该怎么做?

x = [1,2,3,4,5]
y = [3,4,2,3,4]
c = [1,2,4,0.5,5]

【问题讨论】:

  • 编辑:我通过迭代 c 列表进行修复,并根据它的值赋予它某种颜色

标签: python matplotlib scatter-plot scatter


【解决方案1】:

如果您想要颜色图的特定边界,您可以使用mpl.colors.BoundaryNormmpl.colors.ListedColormap

import matplotlib.pyplot as plt
import matplotlib as mpl

x = [1,2,3,4,5]
y = [3,4,2,3,4]
c = [1,2,4,0.5,5]

cmap = mpl.colors.ListedColormap(['blue','red','magenta', 'green', 'gray'])
c_norm = mpl.colors.BoundaryNorm(boundaries=[0,1,2,3,4,5], ncolors=5)
plt.scatter(x, y, c=c, s=200, cmap=cmap, norm=c_norm)
plt.colorbar()
plt.show()

这给出了这个情节:

【讨论】:

    【解决方案2】:

    您可以创建和使用列出的颜色图:

    import matplotlib as mpl
    import matplotlib.pyplot as plt
    
    x = [1,2,3,4,5]
    y = [3,4,2,3,4]
    c = [1,2,4,0.5,5]    
    cmap = mpl.colors.ListedColormap( [[1., 0., 0.],
                                       [0., 1., 0.],
                                       [0., 0., 1.]])
    plt.scatter(x, y, c=c, s=100, cmap=cmap)
    plt.show()
    

    【讨论】:

    • 您实际上并没有使用这种方法设置每个颜色边界,只是颜色图。对于与此示例不同的c 列表,您将得到错误的颜色。
    【解决方案3】:

    这是另一个示例,根据年龄为散点图着色。

    BoundaryNorm 为每个年龄段设置了界限,并为每个年龄段关联了一种颜色。

    例如,如果有年龄范围< 18, 18-40, 40-65, 65-80, > 80,您可以将这些界限列为[18,40,65,80]。 BoundaryNorm 需要比颜色数量多一个边界,因此您可以在前面添加0,在末尾添加100

    您可以从现有颜色图创建颜色图,提供所需的颜色数量:plt.cm.get_cmap('plasma_r', len(boundaries)+1) 或作为 ListedColormap,为其提供明确的颜色列表:matplotlib.colors.ListedColormap([...])

    示例代码:

    import matplotlib
    from matplotlib import pyplot as plt
    import pandas as pd
    import numpy as np
    
    N = 30
    df = pd.DataFrame({'x': np.random.randint(4,12,N),
                       'y': np.random.randint(4,10,N),
                       'birthdt': np.random.randint(1,95, N)})
    boundaries = [18, 40, 65, 80]
    cmap = matplotlib.colors.ListedColormap(['limegreen', 'dodgerblue', 'crimson', 'orange', 'fuchsia'])
    # cmap = plt.cm.get_cmap('plasma_r', len(boundaries) + 1)
    norm = matplotlib.colors.BoundaryNorm([0]+boundaries+[100], len(boundaries)+1)
    
    plt.scatter(df.x, df.y, s=60, c=df.birthdt, cmap=cmap, norm=norm)
    cbar = plt.colorbar(extend='max')
    cbar.ax.set_ylabel('Age')
    plt.show()
    

    如果您希望颜色条分隔与年龄范围成比例,您可以尝试:

    import matplotlib
    from matplotlib import pyplot as plt
    import pandas as pd
    import numpy as np
    
    N = 30
    df = pd.DataFrame({'x': np.random.randint(4, 12, N),
                       'y': np.random.randint(4, 10, N),
                       'birthdt': np.random.randint(1, 95, N)})
    boundaries = [18, 30, 65, 80]
    max_age = 100
    base_colors = ['limegreen', 'dodgerblue', 'crimson', 'orange', 'fuchsia']
    full_colors = [c for c, b0, b1 in zip(base_colors, [0] + boundaries, boundaries + [max_age]) for i in range(b1 - b0)]
    cmap_full = matplotlib.colors.ListedColormap(full_colors)
    norm_full = matplotlib.colors.Normalize(vmin=0, vmax=max_age)
    plt.scatter(df.x, df.y, s=60, c=df.birthdt, cmap=cmap_full, norm=norm_full)
    cbar = plt.colorbar(extend='max', ticks=boundaries)
    cbar.ax.set_ylabel('Age')
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2013-10-08
      • 1970-01-01
      • 2018-08-23
      • 1970-01-01
      • 2022-06-10
      • 2019-01-03
      • 1970-01-01
      • 1970-01-01
      • 2021-10-05
      相关资源
      最近更新 更多