散点图中的条件颜色

时间:2016-07-13 18:14:01

标签: python python-2.7 matplotlib jupyter-notebook

在下面的玩具示例中,我想在散点图中制作条件颜色,以便对于所有值,例如3&lt; xy&lt;在图7中,散点的不透明度是α= 1,并且对于其余的,它们具有α<1。 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()

enter image description here enter image description here

1 个答案:

答案 0 :(得分:1)

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

enter image description here