2017-11-02 13 views
1

次の例では、画像の上にカーソルを置くと、左下隅に表示したいテキストだけでなく、対応するピクセルのグレースケール値も表示されます。この情報を抑制する(またはフォーマットする)方法はありますか?matplotlib imshowカーソル位置からの書式設定

import numpy as np 
import matplotlib.pyplot as plt 

class test(): 
    def __init__(self): 
     fig=plt.figure() 
     ax=fig.add_subplot(111) 
     ax.imshow(np.random.rand(20,20)) 
     def format_coord(x,y): 
      return "text_string_made_from(x,y)" 
     ax.format_coord=format_coord 
     fig.canvas.draw() 
+0

は、それがどのように見えるおよび/またはどのようにそれが – Murmel

答えて

0

一つは、カスタムものと方法を置き換えることによりステータスバーメッセージ

enter image description here

xy座標一部を変更するax.format_coordを使用することができ

def format_coord(x,y): 
    return "text_string_made_from({:.2f},{:.2f})".format(x,y) 
ax.format_coord=format_coord 

enter image description here

残念ながら、角括弧内のデータ値はax.format_coordではなく、代わりにナビゲーションツールバーのmouse_moveメソッドによって設定されます。 それを悪化させるのは、mouse_moveが別のメソッド.set_messageを実際に表示するクラスメソッドであることです。ツールバーはバックエンド依存であるため、単純に置き換えることはできません。

代わりに、ツールバーのインスタンスがクラスメソッドの最初の引数として与えられるように、それを猿で修正する必要があります。これにより、ソリューションが少し面倒になります。

以下では、表示するメッセージを設定する関数mouse_moveがあります。これは、通常のx & y座標format_coordからです。ツールバーのインスタンスを引数として取り、ツールバーの.set_messageメソッドを呼び出します。ツールバーインスタンスを引数としてmouse_move関数を呼び出す別の関数mouse_move_patchを使用します。 mouse_move_patch関数は 'motion_notify_event'に接続されています。

import numpy as np 
import matplotlib.pyplot as plt 

def mouse_move(self,event): 
    if event.inaxes and event.inaxes.get_navigate(): 
     s = event.inaxes.format_coord(event.xdata, event.ydata) 
     self.set_message(s) 

class test(): 
    def __init__(self): 
     fig=plt.figure() 
     ax=fig.add_subplot(111) 
     ax.imshow(np.random.rand(20,20)) 
     def format_coord(x,y): 
      return "text_string_made_from({:.2f},{:.2f})".format(x,y) 
     ax.format_coord=format_coord 

     mouse_move_patch = lambda arg: mouse_move(fig.canvas.toolbar, arg) 
     fig.canvas.toolbar._idDrag = fig.canvas.mpl_connect(
         'motion_notify_event', mouse_move_patch) 

t = test() 
plt.show() 

これは

enter image description here

+0

ように見えるべきかのスクリーンショットを提供するために、考えてみましょうステータスバーのメッセージ内のデータ値の所望のommitanceになります私はその質問から* "(またはそれをフォーマットする)*"の部分を省いた。あなたはこれにも解決策を得ることに興味がありますか?もしそうなら、私に教えてください。しかし、私はそれが現時点で必要であるので、もっと複雑にしたくありませんでした。 – ImportanceOfBeingErnest

+0

パーフェクト。ありがとう。 –

関連する問題