Dynamically change matplotlib chart contents

I used to, I compared the output of two functions using python and matplotlib. The result was just as simple since building with matplotlib is pretty simple: I just built two arrays with different markers. Piece of cake.

Now I am having the same problem, but now I have many pairs of curves for comparison. At first I tried to sketch everything with different colors and markers. This did not satisfy me, since the ranges of each curve are not exactly the same. In addition to this, I quickly ran out of colors and markers that were different enough for identification (RGBCMYK, after that the custom colors resemble any previous ones).

I also tried to fit each pair of curves, getting a window with many graphs. Too many people. I tried one window per plot, too many windows.

So, I'm just wondering if there is any existing widget, or if you have any suggestion (or other idea) to accomplish this:

I want to see a couple of curves and then easily select the following: with a slider, button, mouse scroll, or any other widgets or events. By changing the curves, the previous one should disappear, the legend should change and its axis.

+3
source share
3 answers

Well, I managed to do this with an event handler for mouse clicks. I will change it to something more useful, but I will 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)
+8

, matplotlib . :

+2

I really like to use features. If you complete the tutorial by writing a graphical scientific programming application , you can do what you want. The tutorial shows how to interact with the matplotlib graph using the graphical user interface.

+2
source

Source: https://habr.com/ru/post/1728193/


All Articles