您好,登錄后才能下訂單哦!
這篇文章給大家介紹利用matplotlib怎么繪制一個多子圖共享鼠標光標,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
matplotlib
官方除了提供了鼠標十字光標的示例,還提供了同一圖像內多子圖共享光標的示例,其功能主要由widgets
模塊中的MultiCursor
類提供支持。
MultiCursor
類與Cursor
類參數類似,差異主要在:
Cursor
類參數只有一個ax
,即需要顯示光標的子圖;MultiCursor
類參數為canvas
和axes
,其中axes
為需要共享光標的子圖列表。
Cursor
類中,光標默認是十字線;MultiCursor
類中,光標默認為豎線。
官方示例
import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import MultiCursor t = np.arange(0.0, 2.0, 0.01) s1 = np.sin(2*np.pi*t) s2 = np.sin(4*np.pi*t) fig, (ax1, ax2) = plt.subplots(2, sharex=True) ax1.plot(t, s1) ax2.plot(t, s2) multi = MultiCursor(fig.canvas, (ax1, ax2), color='r', lw=1) plt.show()
簡易修改版
multi = MultiCursor(fig.canvas, (ax1, ax2), color='r', lw=1, horizOn=True, vertOn=True)
MultiCursor
類源碼
class MultiCursor(Widget): """ Provide a vertical (default) and/or horizontal line cursor shared between multiple axes. For the cursor to remain responsive you must keep a reference to it. Example usage:: from matplotlib.widgets import MultiCursor import matplotlib.pyplot as plt import numpy as np fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True) t = np.arange(0.0, 2.0, 0.01) ax1.plot(t, np.sin(2*np.pi*t)) ax2.plot(t, np.sin(4*np.pi*t)) multi = MultiCursor(fig.canvas, (ax1, ax2), color='r', lw=1, horizOn=False, vertOn=True) plt.show() """ def __init__(self, canvas, axes, useblit=True, horizOn=False, vertOn=True, **lineprops): self.canvas = canvas self.axes = axes self.horizOn = horizOn self.vertOn = vertOn xmin, xmax = axes[-1].get_xlim() ymin, ymax = axes[-1].get_ylim() xmid = 0.5 * (xmin + xmax) ymid = 0.5 * (ymin + ymax) self.visible = True self.useblit = useblit and self.canvas.supports_blit self.background = None self.needclear = False if self.useblit: lineprops['animated'] = True if vertOn: self.vlines = [ax.axvline(xmid, visible=False, **lineprops) for ax in axes] else: self.vlines = [] if horizOn: self.hlines = [ax.axhline(ymid, visible=False, **lineprops) for ax in axes] else: self.hlines = [] self.connect() def connect(self): """Connect events.""" self._cidmotion = self.canvas.mpl_connect('motion_notify_event', self.onmove) self._ciddraw = self.canvas.mpl_connect('draw_event', self.clear) def disconnect(self): """Disconnect events.""" self.canvas.mpl_disconnect(self._cidmotion) self.canvas.mpl_disconnect(self._ciddraw) def clear(self, event): """Clear the cursor.""" if self.ignore(event): return if self.useblit: self.background = ( self.canvas.copy_from_bbox(self.canvas.figure.bbox)) for line in self.vlines + self.hlines: line.set_visible(False) def onmove(self, event): if self.ignore(event): return if event.inaxes is None: return if not self.canvas.widgetlock.available(self): return self.needclear = True if not self.visible: return if self.vertOn: for line in self.vlines: line.set_xdata((event.xdata, event.xdata)) line.set_visible(self.visible) if self.horizOn: for line in self.hlines: line.set_ydata((event.ydata, event.ydata)) line.set_visible(self.visible) self._update() def _update(self): if self.useblit: if self.background is not None: self.canvas.restore_region(self.background) if self.vertOn: for ax, line in zip(self.axes, self.vlines): ax.draw_artist(line) if self.horizOn: for ax, line in zip(self.axes, self.hlines): ax.draw_artist(line) self.canvas.blit() else: self.canvas.draw_idle()
關于利用matplotlib怎么繪制一個多子圖共享鼠標光標就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。