astromonerd
astromonerd

Reputation: 937

Conditional colors in scatter plot

In the below toy example, I would like to make conditional colors in my scatter plot such that for all values, say 3 < xy < 7, the opacity of the scatter points are alpha = 1, and for the rest they have an 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()

enter image description here enter image description here

Upvotes: 0

Views: 1769

Answers (1)

Serenity
Serenity

Reputation: 36695

Use masked array from numpy and plot scatter twice:

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

Upvotes: 1

Related Questions