I have 3 columns of data representing 3 pixels (x1, x2, x3), that update live.
I want to:
I am trying to modify from: Matplotlib Plot Points Over Time Where Old Points Fade
However I am unable to assign a different color to each value of x (x=1, x=2, x=3):
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation
from matplotlib.colors import LinearSegmentedColormap
from matplotlib.animation import PillowWriter
fig, ax = plt.subplots()
ax.set_xlabel('X Axis', size = 12)
ax.set_ylabel('Y Axis', size = 12)
ax.axis([0,4,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], [1,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():
x = np.arange(1,4) # TODO: ASSOCIATE COLOUR WITH EACH X VALUE
y = np.random.rand(3)
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('Different colors for each x value')
ani = matplotlib.animation.FuncAnimation(fig, update, frames=t_vals,interval=50)
plt.show()
It looks like you took the right approach, the only change I would suggest is creating 3 different scatter plots (one for each x
values) instead of one.
See code below:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation
from matplotlib.colors import LinearSegmentedColormap
from matplotlib.animation import PillowWriter
import matplotlib.cm as cm
fig, ax = plt.subplots()
ax.set_xlabel('X Axis', size = 12)
ax.set_ylabel('Y Axis', size = 12)
ax.axis([0,4,0,1])
x_vals = []
y_vals = []
iterations = 100
t_vals = np.linspace(0,1, iterations)
cmaps=[cm.get_cmap('Reds'),cm.get_cmap('Blues'),cm.get_cmap('Greens')] #declaring colormaps
scatters=[ax.scatter(x_vals,y_vals,c=[],cmap=cmaps[i],vmin=0,vmax=1) for i in range(len(cmaps))] #initializing the 3 scatter plots
intensities=[[] for i in range(len(cmaps))] #initializing intensities array
def get_new_vals():
x = np.arange(1,4)
y = np.random.rand(3)
return x,y
def update(t):
global x_vals, y_vals, intensities
# Get intermediate points
new_xvals, new_yvals = get_new_vals()
x_vals=np.hstack((x_vals,new_xvals))
y_vals=np.hstack((y_vals,new_yvals))
# Put new values in your plot
for i in range(3):
scatters[i].set_offsets(np.c_[x_vals[x_vals==i+1],y_vals[x_vals==i+1]])
intensities[i]=np.concatenate((np.array(intensities[i])*0.96, np.ones(len(new_xvals[new_xvals==i+1]))))
scatters[i].set_array(intensities[i])
ax.set_title('Different colours for each x value')
ani = matplotlib.animation.FuncAnimation(fig, update, frames=t_vals,interval=50)
plt.show()