散点图中的条件颜色

Conditional colors in scatter plot

在下面的玩具示例中,我想在我的散点图中制作条件颜色,这样对于所有值,比如 3 < xy < 7,散点的不透明度为 alpha = 1,其余的他们的 alpha < 1.

xy = np.random.rand(10,10)*100//10
print xy

# Determine the shape of the the array

x_size = np.shape(xy)[0]
y_size = np.shape(xy)[1]

# Scatter plot the array

fig = plt.figure()
ax = fig.add_subplot(111)
xi, yi = np.meshgrid(range(x_size), range(y_size))
ax.scatter(xi, yi, s=100, c=xy, cmap='RdPu')
plt.show()

使用 numpy 中的掩码数组并绘制散点图两次:

import matplotlib.pyplot as plt
import numpy as np

xy = np.random.rand(10,10)*100//10
x_size = np.shape(xy)[0]
y_size = np.shape(xy)[1]

# get interesting in data
xy2 = np.ma.masked_where((xy > 3) & (xy < 7), xy)
print xy2

# Scatter plot the array
fig = plt.figure()
ax = fig.add_subplot(111)
xi, yi = np.meshgrid(xrange(x_size), xrange(y_size))
ax.scatter(xi, yi, s=100, c=xy, cmap='RdPu', alpha = .5, edgecolors='none')  # plot all data
ax.scatter(xi, yi, s=100, c=xy2, cmap='bone',alpha = 1., edgecolors='none')  # plot masked data
plt.show()