首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何根据matplotlib中的颜色映射对散点进行着色?

如何根据matplotlib中的颜色映射对散点进行着色?
EN

Stack Overflow用户
提问于 2012-08-09 21:55:06
回答 3查看 22.4K关注 0票数 19

我正在尝试根据从已经定义的颜色贴图中挑选的一组值(从0到1)对散点图中的点进行着色,比如Blues或Red。我试过这个:

代码语言:javascript
复制
import matplotlib
import matplotlib.pyplot as plt
from numpy import *
from scipy import *
fig = plt.figure()
mymap = plt.get_cmap("Reds")
x = [8.4808517662594909, 11.749082788323497, 5.9075039082855652, 3.6156231827873615, 12.536817102137768, 11.749082788323497, 5.9075039082855652, 3.6156231827873615, 12.536817102137768]
spaced_colors = linspace(0, 1, 10)
print spaced_colors
plt.scatter(x, x,
            color=spaced_colors,
            cmap=mymap)
# this does not work either
plt.scatter(x, x,
            color=spaced_colors,
            cmap=plt.get_cmap("gray"))

但是,无论是使用红色还是灰色映射表,它都不起作用。如何做到这一点?

编辑:如果我想单独绘制每个点,这样它就可以有一个单独的图例,我该怎么做呢?我试过了:

代码语言:javascript
复制
fig = plt.figure()
mymap = plt.get_cmap("Reds")
data = np.random.random([10, 2])
colors = list(linspace(0.1, 1, 5)) + list(linspace(0.1, 1, 5))
print "colors: ", colors
plt.subplot(1, 2, 1)
plt.scatter(data[:, 0], data[:, 1],
           c=colors,
           cmap=mymap)
plt.subplot(1, 2, 2)
# attempt to plot first five points in five shades of red,
# with a separate legend for each point
for n in range(5):
    plt.scatter([data[n, 0]], [data[n, 1]],
               c=[colors[n]],
               cmap=mymap,
               label="point %d" %(n))
plt.legend()

但它失败了。我需要为每个点调用scatter,以便它可以有一个单独的label=,但仍然希望每个点具有不同的颜色映射图阴影作为其颜色。谢谢。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-08-12 01:54:54

如果你真的想要这样做(你在编辑中描述的那样),你必须从你的色彩映射表中“拉”出颜色(我已经注释了我对你的代码所做的所有更改):

代码语言:javascript
复制
import numpy as np
import matplotlib.pyplot as plt 

# plt.subplots instead of plt.subplot
# create a figure and two subplots side by side, they share the
# x and the y-axis
fig, axes = plt.subplots(ncols=2, sharey=True, sharex=True)
data = np.random.random([10, 2]) 
# np.r_ instead of lists
colors = np.r_[np.linspace(0.1, 1, 5), np.linspace(0.1, 1, 5)] 
mymap = plt.get_cmap("Reds")
# get the colors from the color map
my_colors = mymap(colors)
# here you give floats as color to scatter and a color map
# scatter "translates" this
axes[0].scatter(data[:, 0], data[:, 1], s=40,
                c=colors, edgecolors='None',
                cmap=mymap)
for n in range(5):
    # here you give a color to scatter
    axes[1].scatter(data[n, 0], data[n, 1], s=40,
                    color=my_colors[n], edgecolors='None',
                    label="point %d" %(n))
# by default legend would show multiple scatterpoints (as you would normally
# plot multiple points with scatter)
# I reduce the number to one here
plt.legend(scatterpoints=1)
plt.tight_layout()
plt.show()

但是,如果您只想绘制10个值,并且想要为每个值命名,则应该考虑使用不同的名称,例如,此example中的条形图。另一个机会是使用带有自定义颜色周期的plt.plot,就像在这个example中一样。

票数 20
EN

Stack Overflow用户

发布于 2012-08-09 22:58:05

As per the documentation,则需要c关键字参数而不是color。(我同意这有点令人困惑,但在这种情况下,"c“和"s”术语是从matlab继承的。)

例如。

代码语言:javascript
复制
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

x, y, colors = np.random.random((3,10))

fig, ax = plt.subplots()
ax.scatter(x, y, c=colors, s=50, cmap=mpl.cm.Reds)

plt.show()

票数 13
EN

Stack Overflow用户

发布于 2012-08-16 06:26:52

这样如何:

代码语言:javascript
复制
import matplotlib.pyplot as plt
import numpy as np


reds = plt.get_cmap("Reds")
x = np.linspace(0, 10, 10)
y = np.log(x)

# color by value given a cmap
plt.subplot(121)
plt.scatter(x, y, c=x, s=100, cmap=reds)


# color by value, and add a legend for each
plt.subplot(122)
norm = plt.normalize()
norm.autoscale(x)
for i, (x_val, y_val) in enumerate(zip(x, y)):
    plt.plot(x_val, y_val, 'o', markersize=10, 
             color=reds(norm(x_val)),
             label='Point %s' % i
             )
plt.legend(numpoints=1, loc='lower right')

plt.show()

代码应该是完全不言自明的,但如果你想让我复习任何东西,请大声说出来。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11885060

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档