python - matplotlib 散点图颜色作为第三个和第四个变量的函数

标签 python numpy matplotlib gradient

有什么方法可以使用两个变量来设置颜色来为散点图着色吗? IE。 a 设置蓝色级别,b 设置红色级别?类似这样的事情:

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

最佳答案

扩展 falsetru 的答案,您可以使用 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

关于python - matplotlib 散点图颜色作为第三个和第四个变量的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31714421/

相关文章:

python - 如何从 python beautiful soup 的表中获取 tbody?

python - 有谁知道 TikZ 的非 TeX 等价物?

python - 为 python 目录中的每个模块调用特定函数

python - 值错误 : operands could not be broadcast together with shapes (1, 2) (20,100)

python - 用于谐波分析的快速傅里叶变换

python - Matplotlib:外部图例,分布在多个子图中

python - 确保 python 脚本仍在运行

python - 使用SciPy接口(interface)和Cython直接调用BLAS/LAPACK

python - 如何向绘图添加悬停注释

python - 用箭头标记 matplotlib 直方图 bin