2016-03-19 1 views
0

私は現在、いくつかの2次元のodeを解決する問題に取り組んでいます。私の問題は、バネで引かれたテストマスの位置を2次元ポテンシャルのテスト位置に計算することです。Pythonのマウスの動きから統合パスを取得します

私はサポートツールの位置をカスタマイズしたいと思っています。可能な輪郭マップの上に描画することをおすすめします。

私はPythonには新しく、これを行うには最良の方法が何であるかについての手がかりがありません。私はこのプロジェクトでnumpyとmatplotlibを使用しています。これは、これらのパッケージで実行できる場合です。

ありがとうございました。

答えて

1

等高線プロットをプロットする、マウスで長方形をやり取りして最終的に描かれた矩形のデータを得るといういくつかの観点から考える必要があります。以下のレシピをチェックします(最後に私はこれを適応した場所から場所を参照してください):

import matplotlib.pyplot as plt 
    from matplotlib.patches import Rectangle 
    import matplotlib.mlab as mlab 
    import numpy as np 

    class Annotate(object): 
     def __init__(self): 
      self.ax = plt.gca() 

      delta = 0.025 
      x = np.arange(-3.0, 3.0, delta) 
      y = np.arange(-2.0, 2.0, delta) 
      X, Y = np.meshgrid(x, y) 
      Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) 
      Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1) 
      # difference of Gaussians 
      Z = 10.0 * (Z2 - Z1) 

      CS = plt.contour(X, Y, Z) 
      plt.clabel(CS, inline=1, fontsize=10) 
      plt.title('Simplest default with labels') 

      self.rect = Rectangle((0,0), 1, 1,color=(0,0,1,0.3)) 
      self.x0 = None 
      self.y0 = None 
      self.x1 = None 
      self.y1 = None 
      self.ax.add_patch(self.rect) 
      self.ax.figure.canvas.mpl_connect('button_press_event', self.on_press) 
      self.ax.figure.canvas.mpl_connect('button_release_event', self.on_release) 
      self.ax.set_xlim(-3,3) 
      self.ax.set_ylim(-2,2) 

     def on_press(self, event): 
      print('press') 
      self.x0 = event.xdata 
      self.y0 = event.ydata 

     def on_release(self, event): 
      print('release') 
      self.x1 = event.xdata 
      self.y1 = event.ydata 
      self.rect.set_width(self.x1 - self.x0) 
      self.rect.set_height(self.y1 - self.y0) 
      self.rect.set_xy((self.x0, self.y0)) 
      print('Rectangle is:','POS:(',self.x1,',',self.y1,') SIZE:(',self.x1 - self.x0,',',self.y1 - self.y0,')') 
      self.ax.figure.canvas.draw() 

    a = Annotate() 
    plt.show() 

は、このコードは、このになります:あなたは、マウスが四角形を描くことができます

Rectangle selection over Countour plot

を、 (透明なので、その下にあるものが見えます)。また、コンソールに矩形の位置とサイズを出力します:

press 
release 
Rectangle is: POS:(0.967741935484 , -0.449790794979) SIZE:(1.43951612903 , -1.46443514644) 

このコードを使用してあなたは、交換(たぶん)で、あなたが要求してきた結果を得ることができます。

Recipe 1 Recipe 2

:私は次のレシピから、このコードを適応してきました注意してください
関連する問題