2016-04-06 6 views
0

Hier ist ein pyplot.barh Beispiel. Wenn der Benutzer auf einen roten oder grünen Balken klickt, sollte das Skript den x & y-Wert von bar erhalten, also füge ich pick_event auf fig hinzu. enter image description herematplotlib pick_event funktioniert nicht für barh?

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() 

Aber nichts passiert, wenn ich die Farbleiste klicken. Warum hat es keine Reaktion?

Antwort

1

Der Grund ist, dass Sie artists definieren müssen, das identifiziert werden kann, und picked durch eine mouseclick; dann müssen Sie diese Objekte pickable machen. Hier

ist ein Minimum Beispiel mit zwei hbar Plots, mit denen Sie die Objekte mit einem mouseclick auszuwählen; Ich habe alle Formatierungen entfernt, um mich auf die Frage zu konzentrieren, die Sie gestellt haben.

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() 

nach ein paar Klicks, die Ausgabe wie folgt aussehen:

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) 

Wie Sie nicht angeben, was Sie mit dem aufgenommenen Objekt tun wollte, habe ich nur gedruckt, dessen Standard __str__; Wenn Sie die matplotlib Dokumentation nachschlagen, finden Sie eine Liste der properties, auf die Sie zugreifen können, um Daten zu extrahieren.

Ich überlasse es Ihnen, die Handlung nach Ihren Wünschen zu formatieren.

+0

Greate! Also fügt die Magie beim Plotten einen argv 'picker = True' hinzu. In meinem Fall ist es nicht perfekt. Wegen der Verwendung von Doppelachsen (axb = axt.twiny()) gibt es nur eine Seite, die eine Reaktion hat (die grüne Seite). Vielleicht ist das die Doppelachse. – dindom

Verwandte Themen