0

I need to make 100 views with matplotlib in python. The Start point is always random. I know how to generate 100 plots but I want to use the "Forward to next view" arrows.

Here is my code:

import matplotlib.pyplot as plt


for i in range (1,3):

    Startx = [randrange(1, 19)]  # x axis values
    Starty = [randrange(1, 8)]  # y axis values

    plt.plot(Startx, Starty, color='black', linestyle='solid', linewidth=3,  # plotting the points
             marker='o', markerfacecolor='green', markersize=8)

    ##########################################################################################################
    Endx = [16.5]
    Endy = [7.5]

    plt.plot(Endx, Endy, color='black', linestyle='solid', linewidth=3,
             marker='o', markerfacecolor='red', markersize=8)

    ##########################################################################################################

    Ax = [5, 2.5, 3, 5, 6, 5]
    Ay = [7.5, 6, 3.5, 3, 5.5, 7.5]

    plt.plot(Ax, Ay, color='black', linestyle='solid', linewidth=3,
             marker='o', markerfacecolor='blue', markersize=8)

    ##########################################################################################################

    Bx = [8, 6.5, 7.25, 8]
    By = [3, 3, 5.5, 3]

    plt.plot(Bx, By, color='black', linestyle='solid', linewidth=3,
             marker='o', markerfacecolor='blue', markersize=8)

    plt.xlim(0, 19)
    plt.ylim(0, 8)  # setting x and y axis range

    plt.xlabel('x - axis')  # naming the x axis
    plt.ylabel('y - axis')  # naming the y axis

    plt.show()  # function to show the plot

1 Answers1

2

You could try this approach, mentioned in this post matplotlib hooking in to home/back/forward button events Instead of overwritting the 'home' button, use the 'forward' button.

Also see this little example. With every push of the 'forward to next view' button, the plot will update with some random data.

from matplotlib.backends.backend_tkagg import NavigationToolbar2Tk
import matplotlib.pyplot as plt
from random import randint

#new forward button callback => gets triggered, when forward button gets pushed
def customForward(*args):
    ax = plt.gca()
    fig = plt.gcf()
    
    #get line object...
    line = ax.get_lines()[0]
    
    #...create some new random data...
    newData = [randint(1, 10), randint(1, 10), randint(1, 10)]
    
    #...and update displayed data
    line.set_ydata(newData)
    ax.set_ylim(min(newData), max(newData))
    #redraw canvas or new data won't be displayed
    fig.canvas.draw()

#monkey patch forward button callback
NavigationToolbar2Tk.forward = customForward

#plot first data
plt.plot([1, 2, 3], [1, 2, 3])
plt.show()
Dharman
  • 30,962
  • 25
  • 85
  • 135
stranger0612
  • 301
  • 1
  • 2
  • 12