python - How to plot several graphs and make use of the navigation button in [matplotlib] -
the latest version of matplotlib automatically creates navigation buttons under graph. however, examples see in internet show how create 1 graph, making button [next] , [previous] useless. how plot several graphs , make use of buttons?
for example want make graph sin() , cos() 0 degree 360 degree.
right this:
import scipy matplotlib import pyplot datarange = range(0, 360) datarange = map(scipy.deg2rad, datarange) data1 = map(scipy.sin, datarange) data2 = map(scipy.cos, datarange) pyplot.plot(data1) pyplot.show() # <--- if exclude pyplot.plot(data2) pyplot.show() the sin() graph shown. when close window cos graph shown. if exclude first pyplot.show(), both shown in same figure.
how make second graph shown when press next button?
according documentation, forward , back buttons used allow go previous view of single figure. example if used zoom-to-rectangle feature, back button return previous display. depending on backend, possible hook when these buttons pressed.
you can plot several graphs @ same time using subplot follows:
import scipy matplotlib import pyplot datarange = range(0, 360) datarange = map(scipy.deg2rad, datarange) data1 = map(scipy.sin, datarange) data2 = map(scipy.cos, datarange) pyplot.subplot(211) pyplot.plot(data1) pyplot.subplot(212) pyplot.plot(data2) pyplot.show() giving you:
alternatively, alternate between 2 figures when clicked on using following:
import scipy matplotlib import pyplot datarange = range(0, 360) datarange = map(scipy.deg2rad, datarange) data1 = map(scipy.sin, datarange) data2 = map(scipy.cos, datarange) toggle = true def onclick(event): global toggle toggle = not toggle event.canvas.figure.clear() if toggle: event.canvas.figure.gca().plot(data1) else: event.canvas.figure.gca().plot(data2) event.canvas.draw() fig = pyplot.figure() fig.canvas.mpl_connect('button_press_event', onclick) pyplot.plot(data1) pyplot.show() 
Comments
Post a Comment