将点添加到散点图并在python中绘制旁边的颜色条

问题描述 投票:0回答:1

我已经成功创建了一个散点图,其中每个点都有一个x坐标,y坐标和我用颜色条表示的第三个变量(例如时间)。时间值在[0,100]内的所有点都被正确表示。但是,有时候时间需要值float('inf')。颜色条忽略了这些点,我想将它们叠加到散点图上。我该怎么做这个添加?

import random
import pylab

x1 = [random.randint(1,11) for x1_20times in range(20)]
y1 = [random.randint(1,11) for y1_20times in range(20)]
time1 = [random.randint(1,12) for time1_20times in range(20)]

x2 = [random.randint(1,11) for x1_20times in range(20)]
y2 = [random.randint(1,11) for y1_20times in range(20)]
time2 = [random.randint(1,100) for time1_20times in range(20)]

time2[5:8] = [float('inf')]*3 # Change a few of the entries to infinity.

pylab.subplot(2,1,1)
pylab.scatter(x1, y1, c = time1, s = 75)
pylab.xlabel('x1')
pylab.ylabel('y1')
pylab.jet()
pylab.colorbar()

pylab.subplot(2,1,2)
pylab.scatter(x2, y2, c = time2, s = 75)
pylab.scatter(x2[5:8], y2[5:8], s = 75, marker = ur'$\mathcircled{s}$')
pylab.xlabel('x2')
pylab.ylabel('y2')
# m2 = pylab.cm.ScalarMappable(cmap = pylab.cm.jet)
# m2.set_array(time2)
# pylab.colorbar(m2)

# pylab.tight_layout()
pylab.show()

我可以得到正确绘图的点(并且我假设颜色表示也是准确的)但是我不能在散点图旁边显示第二个子图的颜色条。

python add colorbar scatter
1个回答
0
投票

提取要点:

In [25]: filter(lambda m: m[2] == float('inf'), zip(x2, y2, time2))
Out[25]: [(4, 6, inf), (9, 6, inf), (2, 2, inf)]

In [26]: zip(*filter(lambda m: m[2] == float('inf'), zip(x2, y2, time2)))
Out[26]: [(4, 9, 2), (6, 6, 2), (inf, inf, inf)]

In [27]: x,y,t = zip(*filter(lambda m: m[2] == float('inf'), zip(x2, y2, time2)))

然后根据你的喜好绘制它们:

pylab.plot(x, y, 's', mfc='black', mec='None', ms=7)
© www.soinside.com 2019 - 2024. All rights reserved.