多国間超音波距離計の簡単なデモ GUI を開発しています。デバイスは、複数の異なるセンサーから超音波信号を取得し、これらの信号に対して飛行時間処理を実行し、検出されたターゲットの距離情報を使用して、2D デカルト座標でオブジェクトの位置を特定します。
matplotlib を使用して各センサー信号を表示し、ターゲットの位置をプロットする wxPython GUI を作成したいと思います。私が望んでいたのとほぼ同じレイアウトのサンプル コードをいくつか見つけ、コードの不要なコンポーネントの一部を削除しました。Python コードは以下のとおりです。
import sys,os,csv
import numpy as N
import wx
import matplotlib
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import \
FigureCanvasWxAgg as FigCanvas, \
NavigationToolbar2WxAgg as NavigationToolbar
class FinalProject(wx.Frame):
title = ' Ultrasound Demo '
def __init__(self):
wx.Frame.__init__(self, None, -1, self.title)
self.create_menu()
self.create_status_bar()
self.create_main_panel()
def create_menu(self):
self.menubar = wx.MenuBar()
menu_file = wx.Menu()
m_expt = menu_file.Append(-1, "&Save plot\tCtrl-S", "Save plot to file")
self.Bind(wx.EVT_MENU, self.on_save_plot, m_expt)
menu_file.AppendSeparator()
m_exit = menu_file.Append(-1, "E&xit\tCtrl-X", "Exit")
self.Bind(wx.EVT_MENU, self.on_exit, m_exit)
self.menubar.Append(menu_file, "&File")
self.SetMenuBar(self.menubar)
def create_main_panel(self):
self.panel = wx.Panel(self)
self.dpi = 100
self.fig = Figure((9.5, 5.0), dpi=self.dpi)
self.canvas = FigCanvas(self.panel, -1, self.fig)
# self.axes1 = self.fig.add_subplot2grid((2,2), (0,0))
self.axes1 = self.fig.add_subplot(2,1,1)
# self.axes2 = self.fig.add_subplot2grid((2,2), (1,0))
self.axes2 = self.fig.add_subplot(2,1,2)
self.toolbar = NavigationToolbar(self.canvas)
self.vbox = wx.BoxSizer(wx.VERTICAL)
self.vbox.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
self.vbox.AddSpacer(10)
self.hbox = wx.BoxSizer(wx.HORIZONTAL)
flags = wx.ALIGN_LEFT | wx.ALL | wx.ALIGN_CENTER_VERTICAL
self.panel.SetSizer(self.vbox)
self.vbox.Fit(self)
def create_status_bar(self):
self.statusbar = self.CreateStatusBar()
def on_draw_button1(self, event):
self.axes1.clear()
self.axes2.clear()
i = N.arange(0,4,1)
q = i
w = N.arange(-4,0,1)
self.axes1.plot(q,i,'red')
self.axes2.plot(w,i,'yellow')
self.canvas.draw()
def on_draw_button2(self, event):
self.axes1.clear()
self.axes2.clear()
a = [0,1,2,3,4,]
b = [5.5,4.5,3.5,2.5,1.5]
c = [7.5,2.5,4,6.8,10.6]
self.axes1.plot(b,a,'purple')
self.axes2.plot(c,a,'black')
self.canvas.draw()
def on_save_plot(self, event):
file_choices = "PNG (*.png)|*.png"
dlg = wx.FileDialog(
self,
message="Save plot as...",
defaultDir=os.getcwd(),
defaultFile="plot.png",
wildcard=file_choices,
style=wx.SAVE)
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
self.canvas.print_figure(path, dpi=self.dpi)
self.flash_status_message("Saved to %s" % path)
def on_exit(self, event):
self.Destroy()
if __name__ == '__main__':
app = wx.PySimpleApp()
app.frame = FinalProject()
app.frame.Show()
app.MainLoop()
del app
したがって、このコードは、キャプチャされた 2 つの信号を表示するのに役立ちます。追加のサブプロットを追加するのは、対称グリッド上に存在する場合は比較的簡単ですが、これら 2 つのプロットの右側に 3 番目のサブプロットを追加したいと考えています。両方の高さに及びます。
私がやりたいことを明確にするために、現在のキャンバスの高さと同じ高さの既存のプロットの右側に 3 番目のサブプロットを追加します。
matplotlib のドキュメントを読むと、これを行うための適切な関数は plt.subplot2grid() のようです。残念ながら、この関数を使用して現在のサブプロットを定義しようとするとエラーが発生します (使用しようとしたコードは以下でコメントアウトされています。
self.axes1 = self.fig.add_subplot2grid((2,2), (0,0)))
他の誰かがこれを試みましたか?私が間違っている可能性がある場所についてのアイデアはありますか?
ありがとう!