Python 如何在matplotlib中基于颜色贴图对散射中的点进行着色?
我试图根据从已经定义的颜色贴图(如蓝色或红色)中选择的一组值(从0到1)对散点图中的点进行着色。我试过这个:Python 如何在matplotlib中基于颜色贴图对散射中的点进行着色?,python,numpy,matplotlib,scipy,Python,Numpy,Matplotlib,Scipy,我试图根据从已经定义的颜色贴图(如蓝色或红色)中选择的一组值(从0到1)对散点图中的点进行着色。我试过这个: 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.9075039082
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"))
但无论是使用红色还是灰色地图,它都不起作用。如何做到这一点
编辑:如果我想单独绘制每个点,以便它可以有一个单独的图例,我该如何做?我试过:
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=,但仍然希望每个点都有一个颜色贴图的不同阴影作为其颜色。
谢谢。,您想要的是c
关键字参数,而不是color
。(我同意这有点混乱,但在本例中,“c”和“s”术语是从matlab继承的。)
例如
如果你真的想这样做(你在编辑中描述的),你必须从颜色映射中“提取”颜色(我已经对我对代码所做的所有更改进行了注释): 但是,如果只想绘制10个值,并想命名每个值, 你应该考虑使用不同的东西,例如条形图。 . 另一个机会是使用带有自定义颜色循环的
plt.plot
,如下所示 那么:
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()
代码应该都是不言自明的,但是如果你想让我检查任何东西,就大声说。值得一提的是一个怪癖:如果你从OP的代码开始,将
颜色更改为c
,它仍然不起作用,因为x
只有9个元素,而间隔的\u颜色有10个元素,这将触发分散中的不同分支。将其更改为spaced_colors=linspace(0,1,len(x))
(或其他任何内容)可以解决此问题。如果通过多次调用scatter
,分别指定每个点的颜色,为什么不起作用?因为scatter中有一个步骤定义了规范化实例,而且它没有所有数据的上下文,规范化无法正确缩放数据集(因此会得到错误的颜色)。我的回答可能会提供更多关于如何使用CMAP和规范的线索。
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()
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()