Sandeep Silwal
Sandeep Silwal

Reputation: 331

Matplotlib Plot Points Over Time Where Old Points Fade

I would like to achieve two objectives with matplotlib:

Currently, I am able to achieve the opposite goal using colormaps. That is, I can plot points over time but the recent points look more transparent.

Is it possible to get a 'fading' effect on matplotlib using cmap or other tools? Thanks! My code is below:

def plotter_fader(iterations = 100, stay_open = True):
# Set up plot
fig, ax = plt.subplots()
x_data = []
y_data = []
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111)
t_vals = np.linspace(0,1, iterations)
cmap = (0.09803921568627451, 0.09803921568627451, 0.09803921568627451, .05)
for t in t_vals:
    # Get intermediate points
    intermediate = (1-t)*A + t*B
    new_xvals, new_yvals = ... #Get these through some process
    x_vals.extend(new_xvals)
    y_vals.extend(new_yvals)

    # Put new values in your plot
    plt.plot(x_vals, y_vals, '.', color = cmap)

    # Recompute plot limits
    ax.relim()

    # Set title and wait a little bit for 'smoothness'
    ax.set_xlabel('X Axis', size = 12)
    ax.set_ylabel('Y Axis', size = 12)
    ax.set_title('Time: %0.3f' %t)
    ax.autoscale_view()
    fig.canvas.draw()
    time.sleep(0.005)

# Stay open after plotting ends
while stay_open:
    pass

Upvotes: 5

Views: 5835

Answers (2)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339380

Just as usual with a scatter plot you may define an array of values and a colormap that maps those values to colors. You can change this array in each iteration to make older points have a different value.

In the following we take a value of 0 as transparent and a value of 1 as dark blue and create a colormap with those colors.
In each iteration old values are multiplied by a number smaller than one, new values are set to have a value of 1.

Running the animation will hence produce fading points.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation
from matplotlib.colors import LinearSegmentedColormap

fig, ax = plt.subplots()
ax.set_xlabel('X Axis', size = 12)
ax.set_ylabel('Y Axis', size = 12)
ax.axis([0,1,0,1])
x_vals = []
y_vals = []
intensity = []
iterations = 100

t_vals = np.linspace(0,1, iterations)

colors = [[0,0,1,0],[0,0,1,0.5],[0,0.2,0.4,1]]
cmap = LinearSegmentedColormap.from_list("", colors)
scatter = ax.scatter(x_vals,y_vals, c=[], cmap=cmap, vmin=0,vmax=1)

def get_new_vals():
    n = np.random.randint(1,5)
    x = np.random.rand(n)
    y = np.random.rand(n)
    return list(x), list(y)

def update(t):
    global x_vals, y_vals, intensity
    # Get intermediate points
    new_xvals, new_yvals = get_new_vals()
    x_vals.extend(new_xvals)
    y_vals.extend(new_yvals)

    # Put new values in your plot
    scatter.set_offsets(np.c_[x_vals,y_vals])

    #calculate new color values
    intensity = np.concatenate((np.array(intensity)*0.96, np.ones(len(new_xvals))))
    scatter.set_array(intensity)

    # Set title
    ax.set_title('Time: %0.3f' %t)

ani = matplotlib.animation.FuncAnimation(fig, update, frames=t_vals,interval=50)

plt.show()

enter image description here

Upvotes: 15

IMCoins
IMCoins

Reputation: 3306

Is this what you are looking for ?

from matplotlib import pyplot as plt 

# Creates new axis.
plt.axis([0, 10, 0, 1])

# Allows interactive plotting
plt.ion()

# Transparency
alpha = 1

# Plotting first point outside of loop because more convenient for example
point = plt.scatter(0.5, 0.5, alpha=alpha)
for i in range(10):
    # As the loop goes on, increase transparency, remove the current point, 
    # and plots another one, more transparent.
    alpha -= 0.1
    point.remove()
    point = plt.scatter(5, .5, alpha=alpha, color='r')
    plt.pause(0.05)

while True:
    plt.pause(0.05)

Upvotes: -1

Related Questions