Interactively changing alpha value of matplotlib plots

I have looked at the documentation, but I cannot figure out if this is possible -

I have a dataset with values x

and y

and and discrete values z

. Several pairs (x,y)

use the same value z

. What I want to do is when I hover one point with a specific value z

, alpha

all points with the same value z

go to 1 - that is, if all alpha values ​​are initially 0.5, I'd only like points with the same z value to go to 1.

Here's a minimal working example to illustrate what I'm talking about:

#! /usr/bin/env python

import numpy as np
import matplotlib.pyplot as plt

x  = np.random.randn(100)
y  = np.random.randn(100)

z   = np.arange(0, 10, 1)
z   = np.repeat(z, 10)

im = plt.scatter(x, y, c=z, alpha = 0.5)
plt.colorbar(im)
plt.show()

      

+3


source to share


1 answer


Perhaps you can fake what you want to achieve using the second graph:



import numpy as np
import matplotlib.pyplot as plt

Z = np.zeros(1000, dtype = [("Z", int), ("P", float, 2)])
Z["P"] = np.random.uniform(0.0,1.0,(len(Z),2))
Z["Z"] = np.random.randint(0,50,len(Z))

def on_pick(event):
    z = Z[event.ind[0]]['Z']
    P = Z[np.where(Z["Z"] == z)]["P"]
    selection_plot.set_data(P[:,0],P[:,1])
    plt.draw()


fig = plt.figure(figsize=(10,10), facecolor='white')
fig.canvas.mpl_connect('pick_event', on_pick)
ax = plt.subplot(111, aspect=1)
ax.plot(Z['P'][:,0], Z['P'][:,1], 'o', color='k', alpha=0.1, picker=5)

selection_plot, = ax.plot([],[], 'o', color='black', alpha=1.0, zorder=10)
plt.show()

      

0


source







All Articles