Using a matplotlib button to alternate/switch between plots I created

这一生的挚爱 提交于 2021-02-19 04:36:24

问题


So I've created several charts using the matplotlib library in python 3.5, but I want to be able to have the flexibility to utilize a button to alternate between the views I created within a single window. I've been trying to experiment with an example here, but have not succeeded in doing so. I was curious in how to have the flexibility to click through different views that I created.

My code is sort of organized like this:

def plot1(data1, 'name1'):
    ...
    ax.plot(x,y)
    plt.draw()

def plot2(data2, 'name2'):
    ...
    ax2.plot(x,y)
    plt.draw()

def plot3(data3, 'name3'):
    ...
    ax3.plot(x,y)
    plt.draw()


plot1(data1,'name1')
plot2(data2,'name2')
plot3(data3,'name3')

plt.show()

Currently it will show up in three different windows. Now when I try to make this all into one view accessible via buttons, I'm unable to do so because quite frankly I'm unfamiliar with how to pass on the variables in my methods to create my desired subplots with the callback function. Is there a way to sort of structure my code to have them all run under one matplotlib window?


回答1:


The following would be a class that uses the functions that you create. Those would not actually plot anything, but provide the required data. They should be put in a list called funcs, and when you click next or prev the corresponding graph would pop up. This should get you started.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button


fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.2)
x = range(-50,50)
y = range(-50,50)

l, = plt.plot(x, y, lw=2)
ax.title.set_text('y = x')

class Index(object):
    ind = 0
    global funcs # used so yu can access local list, funcs, here
    def next(self, event):
        self.ind += 1 
        i = self.ind %(len(funcs))
        x,y,name = funcs[i]() # unpack tuple data
        l.set_xdata(x) #set x value data
        l.set_ydata(y) #set y value data
        ax.title.set_text(name) # set title of graph
        plt.draw()

    def prev(self, event):
        self.ind -= 1 
        i  = self.ind %(len(funcs))
        x,y, name = funcs[i]() #unpack tuple data
        l.set_xdata(x) #set x value data
        l.set_ydata(y) #set y value data
        ax.title.set_text(name) #set title of graph
        plt.draw()

def plot1():
    x = range(-20,20)
    y = x
    name = "y = x"
    return (x,y, name)

def plot2():
    x = range(-20,20)
    y = np.power(x, 2)
    name = "y = x^2"
    return (x,y,name)

def plot3():
    x = range(-20,20) # sample data
    y = np.power(x, 3)
    name = "y = x^3"
    return (x,y, name) 


funcs = [plot1, plot2, plot3] # functions in a list so you can interate over
callback = Index()
axprev = plt.axes([0.7, 0.05, 0.1, 0.075])
axnext = plt.axes([0.81, 0.05, 0.1, 0.075])
bnext = Button(axnext, 'Next')
bnext.on_clicked(callback.next)
bprev = Button(axprev, 'Previous')
bprev.on_clicked(callback.prev)

plt.show()


来源:https://stackoverflow.com/questions/47383588/using-a-matplotlib-button-to-alternate-switch-between-plots-i-created

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!