使用matplotlib按钮在我创建的图之间进行切换/切换 [英] Using a matplotlib button to alternate/switch between plots I created

查看:30
本文介绍了使用matplotlib按钮在我创建的图之间进行切换/切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我使用 python 3.5 中的 matplotlib 库创建了多个图表,但我希望能够灵活地利用按钮在我在单个窗口中创建的视图之间进行切换.我一直在尝试在此处进行示例,但没有成功所以.我很好奇如何灵活地点击创建的不同视图.

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()

目前它将显示在三个不同的窗口中.现在,当我尝试通过按钮使所有视图进入一个视图时,我无法做到这一点,因为坦率地说,我不熟悉如何在方法中传递变量以使用回调函数创建所需的子图.有没有一种方法可以整理我的代码,使其全部在一个matplotlib窗口下运行?

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?

推荐答案

以下将是一个使用您创建的函数的类.这些实际上不会绘制任何内容,但会提供所需的数据.它们应该放在一个名为 funcs 的列表中,当您单击 next 或 prev 时,会弹出相应的图表.这应该可以帮助您入门.

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()

这篇关于使用matplotlib按钮在我创建的图之间进行切换/切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆