Skip to content

Latest commit

 

History

History
89 lines (74 loc) · 2.83 KB

File metadata and controls

89 lines (74 loc) · 2.83 KB

Figura 3D de matplotlib en ventana de Tkinter

# -*- coding: utf-8 -*-
"""
Ejemplo de tkinter GUI con gráfica 3D de matplotlib

"""
import numpy as np
import tkinter as tk
from tkinter import ttk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg as FigureCanvas

class main(tk.Tk):
    """
    Clase principal
    
    """
    def __init__(self, parent):
        tk.Tk.__init__(self, parent)
        self.initUI()
        
    def initUI(self):
        """
        Define la ventana y los widgets. Conecta los botones y los métodos
        
        """
        self.grid()
        
        #Combo
        self.Combobox = ttk.Combobox(self, values=['Graph1','Graph2'],
                                state="readonly")
        self.Combobox.pack()
        self.Combobox.bind("<<ComboboxSelected>>", self.Draw)
        
        #Gráfica
        framegrafica = tk.Frame(self)
        framegrafica.pack()
        self.fig = plt.figure(figsize=(15,12))
        self.canvas = FigureCanvas(self.fig, master=framegrafica)
        self.canvas.get_tk_widget().pack(expand=True, fill=tk.BOTH)
        self.axes = self.fig.add_subplot(1,1,1, projection="3d")
        self.Draw(event=None)
        
    def Draw(self, event):
        """
        Grafica dependiendo de la selección del Combobox
        
        """
        if self.Combobox.get() == "Graph2":
            self.axes.clear()
            x = np.linspace(np.random.randint(-5,0), np.random.randint(5,10),
                            40) #X coordinates
            y = np.linspace(np.random.randint(-5,0), np.random.randint(5,10),
                            40) #Y coordinates
            X, Y = np.meshgrid(x, y) #Forming MeshGrid
            Z = np.sin(np.sqrt(X**2+Y**2))
            self.axes.plot_surface(X, Y, Z) #plots the 3D surface plot
            self.canvas.draw()
        else:
            self.axes.clear()
            #Seno
            x = np.linspace(-6,6,30) #X coordinates
            y = np.linspace(-6,6,30) #Y coordinates
            X,Y = np.meshgrid(x,y) #Forming MeshGrid
            Z = np.sin(np.sqrt(X**2+Y**2))
            
            # Esfera
            # u = np.linspace(0, 2 * np.pi, 100)
            # v = np.linspace(0, np.pi, 100)
            # X = 10 * np.outer(np.cos(u), np.sin(v))
            # Y = 10 * np.outer(np.sin(u), np.sin(v))
            # Z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))
            
            #Superficie
            # X = np.array([[1, 3], [2, 4]])
            # Y = np.array([[5, 6], [7, 8]])
            # Z = np.array([[9, 12], [10, 11]])
            
            
            self.axes.plot_surface(X, Y, Z) #plots the 3D surface plot
            self.axes.set(xlabel='x', ylabel='y', zlabel='z')
            self.canvas.draw()

if __name__ == '__main__':
    ventana = main(None)
    ventana.mainloop()