1

I want to create an animated plot of a timeserie but I want to be able to color every data point differently. While I am running various analysis tasks on the timeserie data I want to color each data point according to the region that it belongs too.

I followed this example to get an understanding of how animated plotting works and I also found that answer that showcases how color can be incorporated. The problem is that in that approach the whole graph is re-plotted in every iteration, thus changing the color of the whole graph and not the newly plotted data point only.

Can someone show me how the decay example can be altered to assign different color to each data point?

Community
  • 1
  • 1
LetsPlayYahtzee
  • 7,161
  • 12
  • 41
  • 65

1 Answers1

0

You can colour points using scatter and provided you're not planning on plotting too many points, simply adding new points each time with different colours may be the way to go. A minimal example based on decay,

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation


def data_gen(t=0):
    cnt = 0
    while cnt < 1000:
        cnt += 1
        t += 0.01
        yield t, np.sin(2*np.pi*t) * np.exp(-t/10.)

def get_colour(t):
    cmap = matplotlib.cm.get_cmap('Spectral')
    return cmap(t%1.)

def init():
    ax.set_ylim(-1.1, 1.1)
    ax.set_xlim(0, 10)


fig, ax = plt.subplots()
ax.grid()

def run(data):

    # Get some data and plot
    t, y = data
    ax.scatter(t, y, c=get_colour(t))

    #Update axis
    xmin, xmax = ax.get_xlim()
    if t >= xmax:
        ax.set_xlim(xmin, 2*xmax)
        ax.figure.canvas.draw()

ani = animation.FuncAnimation(fig, run, data_gen, blit=False, interval=10,
                              repeat=False, init_func=init)
plt.show()
Ed Smith
  • 12,716
  • 2
  • 43
  • 55
  • good approach indeed, that way you can indeed paint every dtp the way you want, in my case too many data points might be the case though, each timeserie contains aprox 2000000 dtps – LetsPlayYahtzee Aug 04 '16 at 15:23
  • The problem is that with `line.set_xaxis`, you update data and can use `blit` to only redraw new points (see http://stackoverflow.com/questions/8955869/why-is-plotting-with-matplotlib-so-slow). Each call to scatter adds points as a new collection (don't think you can append to collection). For efficiency you could plot the 2,000,000 points as a few collections, e.g. by changing `data_gen` to return/plot blocks at a time (or only plotting once you have enough points, not `c` argument can be an array of values). – Ed Smith Aug 04 '16 at 15:31
  • another problem with scatter is that as the animation progresses and more and more points get added the density increases you cannot differentiate the color of the dtps unless you zoom in, but while it's still on animation it's kind of hard to zoom in – LetsPlayYahtzee Aug 04 '16 at 16:20
  • You can set marker styles, etc for scatter so it looks like part of a line. Any solution (even a line) will be split into discrete data points of different colour. – Ed Smith Aug 04 '16 at 16:31
  • I solved it by setting the `edgecolors` parameter to 'none' which makes the margins of the markers transparent and that way its much easier to see the color of the markers even without zooming in – LetsPlayYahtzee Aug 04 '16 at 16:55