2

Is there some way I can color my scatter plot using two variables to set the color? ie. a sets the blue level and b sets the red level? Something along the lines of this:

import pylab
import numpy
x = numpy.random.random(50)
y = numpy.random.random(50)
a = numpy.random.random(50)
b = numpy.random.random(50)
s = pylab.scatter(x,y,c='red' * a + 'blue'*b)
pylab.colorbar(s)
pylab.show()
3
  • 5
    pylab.scatter(x,y,c=zip(a, b, numpy.zeros(50)))? Commented Jul 30, 2015 at 2:53
  • Awesome! That does work for me. How would you make two colorbars for this? Commented Jul 30, 2015 at 2:58
  • What do you want the two colorbars to signify? In falsetru's example, the colors of the points vary jointly in two dimensions ('redness' and 'greenness'). You could fake separate 'redness' and 'greenness' colorbars, but most of the points would be various shades of brown that would not show up on either colorbar. You could perhaps plot the 2D color space, with red on the x-axis and green on the y-axis. Commented Jul 30, 2015 at 15:04

1 Answer 1

2

Expanding on falsetru's answer, you could make a two-dimensional colorbar using imshow:

import numpy as np
from matplotlib import pyplot as plt
x = np.random.random(50)
y = np.random.random(50)
a = np.random.random(50)
b = np.random.random(50)
s = c=zip(a, b, np.zeros(50))
ax = plt.gca()
print ax.get_position()
plt.scatter(x,y,c = s)
#adjust limits to make room for inset axes
plt.xlim(xmax = 1.5)
plt.ylim(ymax = 1.5)
#create inset axes
ax = plt.axes([.7, .675, .2, .2], axisbg='y')
n = 20
red = np.linspace(min(a), max(a), n)
green = np.linspace(min(b), max(b), n)
floats = np.linspace(0, 1, n)
#make arrays of all possible values between 0 and 1
X, Y = np.meshgrid(floats, floats)
#stack the arrays with a third array of zeros
Z = np.dstack((X, Y, np.zeros(X.shape)))
Z = np.rot90(Z)
red = np.round(red[::-1], 2)
green = np.round(green, 2)
plt.imshow(Z)
ticks = np.arange(0, n, 6)
plt.yticks(ticks, [red[i] for i in ticks])
plt.xticks(ticks, [green[i] for i in ticks], rotation = 'vertical')
plt.xlabel('Green')
plt.ylabel('Red')
plt.show()

two-dimensional colorbar

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.