Matplotlib, turn-based animation

This is a pretty simple question about matplotlib, but I can't figure out how to do this:

I want to build several shapes and use the arrow in the chart window to move from one to another.

while I just know how to create mutilny charts and build them in different windows, for example:

import matplotlib.pyplot as plt fig = plt.figure() plt.figure(1) n= plt.bar([1,2,3,4],[1,2,3,4]) plt.figure(2) n= plt.bar([1,2,3,4],[-1,-2,-3,-4]) plt.show() 

or with multiple shapes in one window using a subtitle.

How can I use mutliple plot in one window and move from one to another with arrows?

Thanks in advance.

+6
source share
1 answer

To create a plot that updates when you press the left and right keys, you will need to handle keyboard events (docs: http://matplotlib.sourceforge.net/users/event_handling.html ).

I put together an example of a plot update using the pyplot interface when you click the left and right arrows:

 import matplotlib.pyplot as plt import numpy as np data = np.linspace(1, 100) power = 0 plt.plot(data**power) def on_keyboard(event): global power if event.key == 'right': power += 1 elif event.key == 'left': power -= 1 plt.clf() plt.plot(data**power) plt.draw() plt.gcf().canvas.mpl_connect('key_press_event', on_keyboard) plt.show() 
+10
source

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


All Articles