[英]Add points to scatterplot and plot a colorbar beside it in python
我已經成功創建了一個散點圖,其中每個點都有一個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()
我可以得到正確繪制的點(並且我假設顏色表示也准確),但是我無法顯示散點圖旁邊的第二個子圖的顏色條。
提取要點:
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.