Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change dynamically the contents of a matplotlib plot

I while ago, I was comparing the output of two functions using python and matplotlib. The result was as good as simple, since plotting with matplotlib is quite easy: I just plotted two arrays with different markers. Piece of cake.

Now I find myself with the same problem, but now I have a lot of pair of curves to compare. I initially tried plotting everything with different colors and markers. This did not satisfy me since the ranges of each curve are not quite the same. In addition to this, I quickly ran out of colors and markers that were sufficiently different to identify (RGBCMYK, after that, custom colors resemble any of the previous ones).

I also tried subplotting each pair of curves, obtaining a window with many plots. Too crowded. I tried one window per plot, too many windows.

So I was just wondering if there is any existing widget or if you have any suggestion (or a different idea) to accomplish this:

I want to see a pair of curves and then select easily the next one, with a slidebar, button, mouse scroll, or any other widget or event. By changing curves, the previous one should disappear, the legend should change and its axis as well.

like image 694
YuppieNetworking Avatar asked Jan 21 '26 01:01

YuppieNetworking


1 Answers

Well I managed to do it with an event handler for mouse clicks. I will change it for something more useful, but I post my solution anyway.

import matplotlib.pyplot as plt

figure = plt.figure()
# plotting
plt.plot([1,2,3],[10,20,30],'bo-')
plt.grid()
plt.legend()

def on_press(event):
    print 'you pressed', event.button, event.xdata, event.ydata
    event.canvas.figure.clear()
    # select new curves to plot, in this example [1,2,3] [0,0,0]
    event.canvas.figure.gca().plot([1,2,3],[0,0,0], 'ro-')
    event.canvas.figure.gca().grid()
    event.canvas.figure.gca().legend()
    event.canvas.draw()


figure.canvas.mpl_connect('button_press_event', on_press)
like image 165
YuppieNetworking Avatar answered Jan 23 '26 21:01

YuppieNetworking