sciapp / gr

GR framework: a graphics library for visualisation applications
Other
329 stars 54 forks source link

pygr.mlab plot limits not respected #66

Closed nilsbecker closed 6 years ago

nilsbecker commented 6 years ago

i am trying to produce a live-updating plot of some running molecule number statistics via mlab. i want to include a time axis on the x-axis, which is also updated live. for this, i have to reset the xlim limits in each update step to the exact extent of the time data. however, mlab snaps the limits to some 'round' values, which makes the updates choppy. is there a way to prevent that?

code:

def mlab_fig(time_range):
    mlab.figure(
            ylabel='mol number',
            xlabel="t",
            ylim=(0,100))
    mlab.colormap(colors)
    times = matimes.copy()
    numbers = manumbers.copy()
    while True:
        t0 = matimes[0]
        first_i = np.searchsorted(matimes, t0 + time_range)
        mlab.xlim(t0, t0 + time_range)
        times[:first_i] = matimes[:first_i]
        numbers[:, :first_i] = manumbers[:, :first_i]
        mlab.plot(*(a[:first_i] for n in manumbers for a in [matimes, n]))
        sleep(0.1)

here, matimes and manumbers are live-updated buffers.

FlorianRhiem commented 6 years ago

Yes, mlab uses gr.adjustlimits to produce round limits and I agree, this does not work for side-scrolling animations as they should be smooth. I will add a flag to disable it.

For now, you can manually replace gr.adjustlimits itself:

gr.adjustlimits = lambda x_min, x_max: (x_min, x_max)
nilsbecker commented 6 years ago

ah, that seems easy enough! will try. btw, is pygr.mlab actually redundant now that gr supports acting as a matplotlib backend? should i use that instead ?

nilsbecker commented 6 years ago

update: the scrolling is now working better! unfortunately, still a bit irregular but that may be to the actual data updates. is time.sleep the preferred way to pause in such an animation?

FlorianRhiem commented 6 years ago

For me it went from being incredibly choppy to very smooth, but the example I used was very simplified:

import gr
from gr.pygr import mlab

gr.adjustlimits = lambda x_min, x_max: (x_min, x_max)

for t in range(1000):
    x = [0.1+t*0.001, 0.172+t*0.001]
    y = [0, 1]
    mlab.plot(x, y, xrange=(x[0], x[1]))

Instead of updating the x limits whenever new data is available try to change the limits at a fixed rate, if that's possible with your data, that way a little empty space might be shown at the right side of the plot now and then, but it won't "jump" to the right once the data becomes available.

How you pause is completely up to you, time.sleep should work fine.

The matplotlib backend existed before the mlab module was created as a matlab-ish interface to gr. Personally I use mlab for simple/"normal" plots and switch to pure gr for less generic images.

nilsbecker commented 6 years ago

ok, thanks! my data are definitely inherently choppy. the smooth limit update might be feasible but it would require some fancy smooth adaptive speed control -- too much work for the moment.