简体   繁体   中英

Add points to scatterplot and plot a colorbar beside it in python

I have successfully created a scatter plot where each point has a x coordinate, y coordinate and a third variable (eg, time) that I represent using a color bar. All points that have time value within [0,100] are correctly represented. However, sometimes time takes the value float('inf') . The color bar ignores such points, I want to superimpose these onto the scatter plot. How can I make this addition?

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()

I can get the points to correctly plot (and I am assuming the color representation is also accurate) but the I can't display the colorbar for the second subplot beside the scatterplot.

Extract out the points:

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)))

and plot them however you like:

pylab.plot(x, y, 's', mfc='black', mec='None', ms=7)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM