バックナンバーはこちら。
https://www.simulationroom999.com/blog/model-based-of-minimum-2-backnumber/
はじめに
前回から一切にtkinterによるGUI作成を始めいてる。
メインウィンドウの生成と大きさ、位置調整までを実施。
今回はmatplotlibをメインウィンドウへ埋め込むところから開始。
登場人物
博識フクロウのフクさん

イラストACにて公開の「kino_k」さんのイラストを使用しています。
https://www.ac-illust.com/main/profile.php?id=iKciwKA9&area=1
エンジニア歴8年の太郎くん

イラストACにて公開の「しのみ」さんのイラストを使用しています。
https://www.ac-illust.com/main/profile.php?id=uCKphAW2&area=1
matplotlibをtkinterに埋め込む際に必要なモジュール

今回はmatplotlibの埋め込みからだねー。

その前にimportしておくモジュールがある。

matplotlib以外に必要なものがあるの?

「matplotlib以外」というかmatplotlibに含まれているモジュールだね。
まぁ以下を実施すればOKだ。
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk

matplotlib.pyplotは以前も使った波形表示のモジュールで、
それ以外にFigureCanvasTkAggとNavigationToolbar2Tkが必要なんだ。
それぞれ何をしてくれるものなの?

FigureCanvasTkAggはmatplotlib.pyplotによる波形表示用のウィジットの配置用。
NavigationToolbar2Tkはmatplotlibで使ってるメニューバーだ。

そういえばここでもそんな説明してたね。
忘れてた。
事前にmatplotlibで空の図(figure)を作っておく

次は埋め込み用の図(figure)を作っておく。
やり方は通常のmatplotlibと一緒だ。
違いは表示用のshow()を呼ばないところくらいだね。
fig = plt.figure()
fig.set_size_inches(8.4, 4.5)
ax = plt.subplot(1,1,1)
ax.set_xlabel('Time')
ax.set_ylabel('Value')
# define plots
ax.plot([], [], label="target[rad/s]", color='Magenta',linewidth=3)
ax.plot([], [], label="voltage[V]", color='Red')
ax.plot([], [], label="speed[rad/s]", color='Blue',linewidth=0.9)
ax.plot([], [], label="loadTorqueStep.tau[N m]", color='Cyan')
ax.plot([], [], label="current[A]", color='Green',linestyle='--',linewidth=0.8)
ax.plot([], [], label="cpu_load[ms]", color='Black',linestyle='--',linewidth=1)
ax.legend(bbox_to_anchor=(1, 1), borderaxespad=0, fontsize=10)
ax.grid(which='both')

plotは呼び出すけど、plotするデータはセットしないのか。

データは後でセットする感じだね。

まぁリアルタイムに更新するわけだからそうなるのか。
まとめ

まとめだよ。
- matplotlibをtkinterに埋め込む際には以下のモジュールが必要。
- matplotlib.pyplot。
- プロット用。
- FigureCanvasTkAgg。
- プロット画面埋め込み用。
- NavigationToolbar2Tk。
- メニューバー。
- matplotlib.pyplot。
- 埋め込む前に図(figure)を用意しておく。
バックナンバーはこちら。
コメント