plt.scatter无法识别由ListedColormap()生成的cmap [英] plt.scatter cannot recognize a cmap generated by ListedColormap()

查看:50
本文介绍了plt.scatter无法识别由ListedColormap()生成的cmap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ListedColormap()为散点图自定义颜色图.

这是散点图的数据集:

  labels = [0,1,1,100,100]X = np.array([[0,2],[0, 0],[1, 0],[5, 0],[5,2]])

这段代码

  color_list = [红色",黄色",蓝色"]cmap = mpl.colors.ListedColormap(color_list)plt.scatter(X [:,0],X [:,1],c =标签,cmap = cmap,s = 200);

输出

似乎plt.scatter无法识别ListedColormap()生成的cmap.

我构造了 cmap 以将左下角的点设为黄色,但事实并非如此.

我对颜色顺序的理解(0->红色,1->黄色,100->蓝色)可能是错误的.

任何提示将不胜感激.

解决方案

基于

有黄点

  labels = np.array([0,56,63,100,100])#< ---新标签值X = np.array([[0,2],[0,0],[1,0],[5,0],[5,2]])color_list = [红色",黄色",蓝色"]cmap = mpl.colors.ListedColormap(color_list)范围 = np.linspace(labels.min(), labels.max(), len(color_list)+1)范数 = mpl.colors.BoundaryNorm(范围,cmap.N)plt.scatter(X[:,0], X[:,1], c=labels, cmap=cmap, s=200, norm=norm)

I am trying to customize a Colormap with ListedColormap() for a scatter plot.

Here is a dataset for the scatter plot:

labels = [  0,   1,   1, 100, 100]
X = np.array([[0, 2],
       [0, 0],
       [1, 0],
       [5, 0],
       [5, 2]])

This piece of code

color_list = ["red", "yellow", 'blue']
cmap = mpl.colors.ListedColormap(color_list)
plt.scatter(X[:,0], X[:,1], c=labels, cmap=cmap, s=200);

outputs

It seems that plt.scatter cannot recognize the cmap generated by ListedColormap().

I constructed the cmap to have the points at the lower left hand corner to be yellow, which is not happening.

My understanding (0 -> red, 1 -> yellow, 100 -> blue) about the order of colors might be wrong.

Any hint would be appreciated.

解决方案

Building upon @ImportanceOfBeingEarnest's comment, for your current values of labels, you will get no yellow color if you consider the equally spaced range of 0-33.33, 33.34-66.66, 66.67-100. The following answer highlights this. The second figure below however shows the yellow color, provided you have the labels falling between the correct range. Check the official page for more examples on BoundaryNorm.

The key line here is ranges = np.linspace(labels.min(), labels.max(), len(color_list)+1) which divides your range of values (labels) into equally spaced intervals.

import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt

labels = np.array([  0,   1,   1, 100, 100]) # converted to array for ease
X = np.array([[0, 2], [0, 0], [1, 0], [5, 0], [5, 2]])

color_list = ["red", "yellow", 'blue']
cmap = mpl.colors.ListedColormap(color_list)

ranges = np.linspace(labels.min(), labels.max(), len(color_list)+1)
norm = mpl.colors.BoundaryNorm(ranges, cmap.N)

plt.scatter(X[:,0], X[:,1], c=labels, cmap=cmap, s=200, norm=norm)

With yellow points

labels = np.array([  0,   56,   63, 100, 100]) # <--- new label values
X = np.array([[0, 2], [0, 0], [1, 0], [5, 0], [5, 2]])

color_list = ["red", "yellow", 'blue']
cmap = mpl.colors.ListedColormap(color_list)

ranges = np.linspace(labels.min(), labels.max(), len(color_list)+1)
norm = mpl.colors.BoundaryNorm(ranges, cmap.N)

plt.scatter(X[:,0], X[:,1], c=labels, cmap=cmap, s=200, norm=norm)

这篇关于plt.scatter无法识别由ListedColormap()生成的cmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆