问题
I have implemented this example:
http://matplotlib.sourceforge.net/examples/pylab_examples/finance_demo.html?highlight=candlestick
I would like to implement mouse hover features for the candlestick so that I can see the candlestick's open/high/low/close either in a popup or a label in a certain panel. I was following along with:
http://matplotlib.sourceforge.net/examples/event_handling/pick_event_demo.html
Unfortunately, the function:
candlestick(ax, quotes, width=0.6)
does not have a picker=True
keyword. Is there another way to implement the mouse hover feature?
Docs for matplotlib.finance http://doc.astro-wise.org/matplotlib.finance.html
回答1:
I wrote an answer to handle "mouse over" events here. That would be a good first point of call to find out how to do something similar.
回答2:
I have already provided an answer which links to some valuable information. Since you want to approach things by looking at the data rather than artists on an axes I thought I would give you a little bit more aid. What I have done is to take the matplotlib candlestick example, and added an on mouse move event which prints the date that the mouse is currently over, and the 3 nearest (in time) stocks. From this point, it should just be a matter of combining the two answers to produce the desired result:
#!/usr/bin/env python
import matplotlib.pyplot as plt
import pylab
from matplotlib.dates import DateFormatter, WeekdayLocator, HourLocator, \
DayLocator, MONDAY, num2date
from matplotlib.finance import quotes_historical_yahoo, candlestick,\
plot_day_summary, candlestick2
# (Year, month, day) tuples suffice as aregs for quotes_historical_yahoo
date1 = ( 2004, 2, 1)
date2 = ( 2004, 4, 12 )
mondays = WeekdayLocator(MONDAY) # major ticks on the mondays
alldays = DayLocator() # minor ticks on the days
weekFormatter = DateFormatter('%b %d') # Eg, Jan 12
dayFormatter = DateFormatter('%d') # Eg, 12
quotes = quotes_historical_yahoo('INTC', date1, date2)
if len(quotes) == 0:
raise SystemExit
fig = plt.figure()
fig.subplots_adjust(bottom=0.2)
ax = fig.add_subplot(111)
ax.xaxis.set_major_locator(mondays)
ax.xaxis.set_minor_locator(alldays)
ax.xaxis.set_major_formatter(weekFormatter)
candlestick(ax, quotes, width=0.6)
ax.xaxis_date()
ax.autoscale_view()
plt.setp(ax.get_xticklabels(), rotation=45, horizontalalignment='right')
def on_move(event):
ax = event.inaxes
if ax is not None:
# convert x y device coordinates to axes data coordinates
date_ordinal, y = ax.transData.inverted().transform([event.x, event.y])
# convert the numeric date into a datetime
date = num2date(date_ordinal)
# sort the quotes by their distance (in time) from the mouse position
def sorter(quote):
return abs(quote[0] - date_ordinal)
quotes.sort(key=sorter)
print 'on date %s the nearest 3 openings were %s at %s respectively' % \
(date,
', '.join([str(quote[1]) for quote in quotes[:3]]),
', '.join([str(num2date(quote[0])) for quote in quotes[:3]]))
on_move_id = fig.canvas.mpl_connect('motion_notify_event', on_move)
plt.show()
HTH
来源:https://stackoverflow.com/questions/11779315/mouse-hover-in-matplotlib-candlestick-for-python