0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

python tkinterGUIへの埋め込みmatplotlibで3Dフィギュアをプロット

Last updated at Posted at 2021-04-24

python で tkinterウィンドウ内にmatplotlibを埋め込み
3Dフィギュアをプロットする。

自分のメモまでです。

#実行サンプル
左側のdrawボタンを押すと、
3Dフィギュア(固定)がプロット表示される

image.png

#実行環境
windows10
Anaconda3

#実行手順
下記ソースコードをファイル保存して、
Anacondata prompt から
python ソースコードのパス 
で実行。

#ソースコード

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('TkAgg')
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import matplotlib.animation as animation
import tkinter.messagebox
from tkinter import *
import tkinter as tk



#lorenz
def button_add():
   def lorenz(x, y, z, s=10, r=5, b=28):
       '''
       Given:
          x, y, z: a point of interest in three dimensional space
          s, r, b: parameters defining the lorenz attractor
       Returns:
          x_dot, y_dot, z_dot: values of the lorenz attractor's partial
              derivatives at the point x, y, z
       '''
       x_dot = s*(y - x)
       y_dot = r*x - y - x*z
       z_dot = x*y - b*z
       return x_dot, y_dot, z_dot

   dt = 0.01
   num_steps = 10000 

   # Need one more for the initial values
   xs = np.empty(num_steps + 1)
   ys = np.empty(num_steps + 1)
   zs = np.empty(num_steps + 1)

   # Set initial values
   xs[0], ys[0], zs[0] = (0., 1., 1.05)

   # Step through "time", calculating the partial derivatives at the current point
   # and using them to estimate the next point
   for i in range(num_steps):
       x_dot, y_dot, z_dot = lorenz(xs[i], ys[i], zs[i])
       xs[i + 1] = xs[i] + (x_dot * dt)
       ys[i + 1] = ys[i] + (y_dot * dt)
       zs[i + 1] = zs[i] + (z_dot * dt)

   # Plot
   fig = Figure()

   canvas = FigureCanvasTkAgg(fig, w)
   canvas.draw()

   ax = fig.add_subplot(111, projection="3d")
   ax.plot(xs, ys, zs, lw=0.5)
   ax.set_xlabel("X Axis")
   ax.set_ylabel("Y Axis")
   ax.set_zlabel("Z Axis")
   ax.set_title("Lorenz Attractor")

   toolbar = NavigationToolbar2Tk(canvas, w)
   toolbar.update()

   canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
##   plt.show()

#---------------interface---------------
w = Tk()
w.title("Lorenz")
w.geometry('800x600')
frame = tk.Frame(w)
frame.pack()

button1 = tk.Button(w, text='draw', fg='red', command= lambda : button_add())
button1.pack(side=tk.LEFT)

w.mainloop()

#関連資料
tkinterウィンドウ内に3Dフィギュアをプロット
参考 https://www.javaer101.com/ja/article/27212977.html

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?