2016-04-06 11 views
0

ここにpyplot.barhの例があります。ユーザーが赤色または緑色のバーをクリックすると、スクリプトはx &のy値を取得するはずです。したがって、図のpick_eventを追加します。 enter image description herebarplに対してmatplotlib pick_eventは機能しませんか?

import matplotlib.pyplot as plt 
import pandas as pd 
import numpy as np 

# Random data 
bottom10 = pd.DataFrame({'amount':-np.sort(np.random.rand(10))}) 
top10 = pd.DataFrame({'amount':np.sort(np.random.rand(10))[::-1]}) 

# Create figure and axes for top10 
fig,axt = plt.subplots(1) 

# Plot top10 on axt 
top10.plot.barh(color='red',edgecolor='k',align='edge',ax=axt,legend=False) 

# Create twin axes 
axb = axt.twiny() 

# Plot bottom10 on axb 
bottom10.plot.barh(color='green',edgecolor='k',align='edge',ax=axb,legend=False) 

# Set some sensible axes limits 
axt.set_xlim(0,1.5) 
axb.set_xlim(-1.5,0) 

# Add some axes labels 
axt.set_ylabel('Best items') 
axb.set_ylabel('Worst items') 

# Need to manually move axb label to right hand side 
axb.yaxis.set_label_position('right') 
#add event handle 
def onpick(event): 
    thisline = event.artist 
    xdata = thisline.get_xdata() 
    ydata = thisline.get_ydata() 
    ind = event.ind 
    print 'onpick points:', zip(xdata[ind], ydata[ind]) 

fig.canvas.mpl_connect('pick_event', onpick) 

plt.show() 

しかし、何も私はカラーバーをクリックしたときに起こりました。なぜそれは反応がないのですか?

答えて

1

理由は、識別できるartistsを定義し、pickedmouseclickで定義する必要があるためです。これらのオブジェクトを作成する必要がありますpickable

ここにはの2つのhbarプロットがあり、mouseclickのオブジェクトを選択できる最小の例があります。あなたが質問した質問に集中するために、すべての書式を削除しました。数回のクリック後

import matplotlib.pyplot as plt 
import pandas as pd 
import numpy as np 
from matplotlib.patches import Rectangle 

top10 = pd.DataFrame({'amount' : - np.sort(np.random.rand(10))}) 
bottom10 = pd.DataFrame({'amount' : np.sort(np.random.rand(10))[::-1]}) 

# Create figure and axes for top10 
fig = plt.figure() 
axt = fig.add_subplot(1,1,1) 
axb = fig.add_subplot(1,1,1) 

# Plot top10 on axt 
bar_red = top10.plot.barh(color='red', edgecolor='k', align='edge', ax=axt, legend=False, picker=True) 
# Plot bottom10 on axb 
bar_green = bottom10.plot.barh(color='green', edgecolor='k', align='edge', ax=axb, legend=False, picker=True) 

#add event handler 
def onpick(event): 
    if isinstance(event.artist, Rectangle): 
     print("got the artist", event.artist) 

fig.canvas.mpl_connect('pick_event', onpick) 
plt.show() 

、出力は次のようになります。

got the artist Rectangle(-0.951754,9;0.951754x0.5) 
got the artist Rectangle(-0.951754,9;0.951754x0.5) 
got the artist Rectangle(-0.951754,9;0.951754x0.5) 
got the artist Rectangle(0,5;0.531178x0.5) 
got the artist Rectangle(0,5;0.531178x0.5) 
got the artist Rectangle(0,5;0.531178x0.5) 
got the artist Rectangle(0,2;0.733535x0.5) 
got the artist Rectangle(0,2;0.733535x0.5) 
got the artist Rectangle(0,2;0.733535x0.5) 
got the artist Rectangle(-0.423519,2;0.423519x0.5) 
got the artist Rectangle(-0.423519,2;0.423519x0.5) 

はあなたが選んだオブジェクトでやりたいことを指定しなかったので、私は唯一の印刷されたその標準__str__matplotlibのドキュメントを参照すると、データを取得するためにアクセスして操作できるpropertiesのリストが見つかります。

私はあなたの好みにプロットを再フォーマットするためにあなたに任せます。

+0

Greate!それで、魔法はプロット時にargv 'picker = True'を追加しています。しかし、私の場合は、完全ではありません。双軸(axb = axt.twiny())を使用しているので、 )それは双軸の働きです。 – dindom

関連する問題