最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

matplotlib繪制鼠標(biāo)的十字光標(biāo)的實(shí)現(xiàn)(自定義方式,官方實(shí)例)

 更新時(shí)間:2021年01月10日 08:40:44   作者:mighty13  
這篇文章主要介紹了matplotlib繪制鼠標(biāo)的十字光標(biāo)(自定義方式,官方實(shí)例),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

matplotlib在widgets模塊提供Cursor類用于支持十字光標(biāo)的生成。另外官方還提供了自定義十字光標(biāo)的實(shí)例。

widgets模塊Cursor類源碼

class Cursor(AxesWidget):
  """
  A crosshair cursor that spans the axes and moves with mouse cursor.

  For the cursor to remain responsive you must keep a reference to it.

  Parameters
  ----------
  ax : `matplotlib.axes.Axes`
    The `~.axes.Axes` to attach the cursor to.
  horizOn : bool, default: True
    Whether to draw the horizontal line.
  vertOn : bool, default: True
    Whether to draw the vertical line.
  useblit : bool, default: False
    Use blitting for faster drawing if supported by the backend.

  Other Parameters
  ----------------
  **lineprops
    `.Line2D` properties that control the appearance of the lines.
    See also `~.Axes.axhline`.

  Examples
  --------
  See :doc:`/gallery/widgets/cursor`.
  """

  def __init__(self, ax, horizOn=True, vertOn=True, useblit=False,
         **lineprops):
    AxesWidget.__init__(self, ax)

    self.connect_event('motion_notify_event', self.onmove)
    self.connect_event('draw_event', self.clear)

    self.visible = True
    self.horizOn = horizOn
    self.vertOn = vertOn
    self.useblit = useblit and self.canvas.supports_blit

    if self.useblit:
      lineprops['animated'] = True
    self.lineh = ax.axhline(ax.get_ybound()[0], visible=False, **lineprops)
    self.linev = ax.axvline(ax.get_xbound()[0], visible=False, **lineprops)

    self.background = None
    self.needclear = False

  def clear(self, event):
    """Internal event handler to clear the cursor."""
    if self.ignore(event):
      return
    if self.useblit:
      self.background = self.canvas.copy_from_bbox(self.ax.bbox)
    self.linev.set_visible(False)
    self.lineh.set_visible(False)
    
  def onmove(self, event):
    """Internal event handler to draw the cursor when the mouse moves."""
    if self.ignore(event):
      return
    if not self.canvas.widgetlock.available(self):
      return
    if event.inaxes != self.ax:
      self.linev.set_visible(False)
      self.lineh.set_visible(False)

      if self.needclear:
        self.canvas.draw()
        self.needclear = False
      return
    self.needclear = True
    if not self.visible:
      return
    self.linev.set_xdata((event.xdata, event.xdata))

    self.lineh.set_ydata((event.ydata, event.ydata))
    self.linev.set_visible(self.visible and self.vertOn)
    self.lineh.set_visible(self.visible and self.horizOn)

    self._update()

  def _update(self):
    if self.useblit:
      if self.background is not None:
        self.canvas.restore_region(self.background)
      self.ax.draw_artist(self.linev)
      self.ax.draw_artist(self.lineh)
      self.canvas.blit(self.ax.bbox)
    else:
      self.canvas.draw_idle()
    return False

自定義十字光標(biāo)實(shí)現(xiàn)

簡(jiǎn)易十字光標(biāo)實(shí)現(xiàn)

首先在 Cursor類的構(gòu)造方法__init__中,構(gòu)造了十字光標(biāo)的橫線、豎線和坐標(biāo)顯示;然后在on_mouse_move方法中,根據(jù)事件數(shù)據(jù)更新橫豎線和坐標(biāo)顯示,最后在調(diào)用時(shí),通過(guò)mpl_connect方法綁定on_mouse_move方法和鼠標(biāo)移動(dòng)事件'motion_notify_event'。

import matplotlib.pyplot as plt
import numpy as np


class Cursor:
  """
  A cross hair cursor.
  """
  def __init__(self, ax):
    self.ax = ax
    self.horizontal_line = ax.axhline(color='k', lw=0.8, ls='--')
    self.vertical_line = ax.axvline(color='k', lw=0.8, ls='--')
    # text location in axes coordinates
    self.text = ax.text(0.72, 0.9, '', transform=ax.transAxes)

  def set_cross_hair_visible(self, visible):
    need_redraw = self.horizontal_line.get_visible() != visible
    self.horizontal_line.set_visible(visible)
    self.vertical_line.set_visible(visible)
    self.text.set_visible(visible)
    return need_redraw

  def on_mouse_move(self, event):
    if not event.inaxes:
      need_redraw = self.set_cross_hair_visible(False)
      if need_redraw:
        self.ax.figure.canvas.draw()
    else:
      self.set_cross_hair_visible(True)
      x, y = event.xdata, event.ydata
      # update the line positions
      self.horizontal_line.set_ydata(y)
      self.vertical_line.set_xdata(x)
      self.text.set_text('x=%1.2f, y=%1.2f' % (x, y))
      self.ax.figure.canvas.draw()


x = np.arange(0, 1, 0.01)
y = np.sin(2 * 2 * np.pi * x)

fig, ax = plt.subplots()
ax.set_title('Simple cursor')
ax.plot(x, y, 'o')
cursor = Cursor(ax)
#關(guān)鍵部分,綁定鼠標(biāo)移動(dòng)事件處理
fig.canvas.mpl_connect('motion_notify_event', cursor.on_mouse_move)
plt.show()

優(yōu)化十字光標(biāo)實(shí)現(xiàn)

在簡(jiǎn)易實(shí)現(xiàn)中,每次鼠標(biāo)移動(dòng)時(shí),都會(huì)重繪整個(gè)圖像,這樣效率比較低。
在優(yōu)化實(shí)現(xiàn)中,每次鼠標(biāo)移動(dòng)時(shí),只重繪光標(biāo)和坐標(biāo)顯示,背景圖像不再重繪。

import matplotlib.pyplot as plt
import numpy as np


class BlittedCursor:
  """
  A cross hair cursor using blitting for faster redraw.
  """
  def __init__(self, ax):
    self.ax = ax
    self.background = None
    self.horizontal_line = ax.axhline(color='k', lw=0.8, ls='--')
    self.vertical_line = ax.axvline(color='k', lw=0.8, ls='--')
    # text location in axes coordinates
    self.text = ax.text(0.72, 0.9, '', transform=ax.transAxes)
    self._creating_background = False
    ax.figure.canvas.mpl_connect('draw_event', self.on_draw)

  def on_draw(self, event):
    self.create_new_background()

  def set_cross_hair_visible(self, visible):
    need_redraw = self.horizontal_line.get_visible() != visible
    self.horizontal_line.set_visible(visible)
    self.vertical_line.set_visible(visible)
    self.text.set_visible(visible)
    return need_redraw

  def create_new_background(self):
    if self._creating_background:
      # discard calls triggered from within this function
      return
    self._creating_background = True
    self.set_cross_hair_visible(False)
    self.ax.figure.canvas.draw()
    self.background = self.ax.figure.canvas.copy_from_bbox(self.ax.bbox)
    self.set_cross_hair_visible(True)
    self._creating_background = False

  def on_mouse_move(self, event):
    if self.background is None:
      self.create_new_background()
    if not event.inaxes:
      need_redraw = self.set_cross_hair_visible(False)
      if need_redraw:
        self.ax.figure.canvas.restore_region(self.background)
        self.ax.figure.canvas.blit(self.ax.bbox)
    else:
      self.set_cross_hair_visible(True)
      # update the line positions
      x, y = event.xdata, event.ydata
      self.horizontal_line.set_ydata(y)
      self.vertical_line.set_xdata(x)
      self.text.set_text('x=%1.2f, y=%1.2f' % (x, y))

      self.ax.figure.canvas.restore_region(self.background)
      self.ax.draw_artist(self.horizontal_line)
      self.ax.draw_artist(self.vertical_line)
      self.ax.draw_artist(self.text)
      self.ax.figure.canvas.blit(self.ax.bbox)


x = np.arange(0, 1, 0.01)
y = np.sin(2 * 2 * np.pi * x)

fig, ax = plt.subplots()
ax.set_title('Blitted cursor')
ax.plot(x, y, 'o')
blitted_cursor = BlittedCursor(ax)
fig.canvas.mpl_connect('motion_notify_event', blitted_cursor.on_mouse_move)
plt.show()

捕捉數(shù)據(jù)十字光標(biāo)實(shí)現(xiàn)

在前面的兩種實(shí)現(xiàn)中,鼠標(biāo)十字光標(biāo)可以隨意移動(dòng)。在本實(shí)現(xiàn)中,十字光標(biāo)只會(huì)出現(xiàn)在離鼠標(biāo)x坐標(biāo)最近的數(shù)據(jù)點(diǎn)上。

import matplotlib.pyplot as plt
import numpy as np


class SnappingCursor:
  """
  A cross hair cursor that snaps to the data point of a line, which is
  closest to the *x* position of the cursor.

  For simplicity, this assumes that *x* values of the data are sorted.
  """
  def __init__(self, ax, line):
    self.ax = ax
    self.horizontal_line = ax.axhline(color='k', lw=0.8, ls='--')
    self.vertical_line = ax.axvline(color='k', lw=0.8, ls='--')
    self.x, self.y = line.get_data()
    self._last_index = None
    # text location in axes coords
    self.text = ax.text(0.72, 0.9, '', transform=ax.transAxes)

  def set_cross_hair_visible(self, visible):
    need_redraw = self.horizontal_line.get_visible() != visible
    self.horizontal_line.set_visible(visible)
    self.vertical_line.set_visible(visible)
    self.text.set_visible(visible)
    return need_redraw

  def on_mouse_move(self, event):
    if not event.inaxes:
      self._last_index = None
      need_redraw = self.set_cross_hair_visible(False)
      if need_redraw:
        self.ax.figure.canvas.draw()
    else:
      self.set_cross_hair_visible(True)
      x, y = event.xdata, event.ydata
      index = min(np.searchsorted(self.x, x), len(self.x) - 1)
      if index == self._last_index:
        return # still on the same data point. Nothing to do.
      self._last_index = index
      x = self.x[index]
      y = self.y[index]
      # update the line positions
      self.horizontal_line.set_ydata(y)
      self.vertical_line.set_xdata(x)
      self.text.set_text('x=%1.2f, y=%1.2f' % (x, y))
      self.ax.figure.canvas.draw()


x = np.arange(0, 1, 0.01)
y = np.sin(2 * 2 * np.pi * x)

fig, ax = plt.subplots()
ax.set_title('Snapping cursor')
line, = ax.plot(x, y, 'o')
snap_cursor = SnappingCursor(ax, line)
fig.canvas.mpl_connect('motion_notify_event', snap_cursor.on_mouse_move)
plt.show()

參考資料

https://www.matplotlib.org.cn/gallery/misc/cursor_demo_sgskip.html

到此這篇關(guān)于matplotlib繪制鼠標(biāo)的十字光標(biāo)的實(shí)現(xiàn)(自定義方式,官方實(shí)例)的文章就介紹到這了,更多相關(guān)matplotlib鼠標(biāo)十字光標(biāo) 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python多線程同步Lock、RLock、Semaphore、Event實(shí)例

    Python多線程同步Lock、RLock、Semaphore、Event實(shí)例

    這篇文章主要介紹了Python多線程同步Lock、RLock、Semaphore、Event實(shí)例,Lock & RLock 用來(lái)確保多線程多共享資源的訪問(wèn),Semaphore用來(lái)確保一定資源多線程訪問(wèn)時(shí)的上限,Event是最簡(jiǎn)單的線程間通信的方式,需要的朋友可以參考下
    2014-11-11
  • selenium+python自動(dòng)化78-autoit參數(shù)化與批量上傳功能的實(shí)現(xiàn)

    selenium+python自動(dòng)化78-autoit參數(shù)化與批量上傳功能的實(shí)現(xiàn)

    這篇文章主要介紹了selenium+python自動(dòng)化78-autoit參數(shù)化與批量上傳,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • Python3中對(duì)range()逆序的解釋

    Python3中對(duì)range()逆序的解釋

    這篇文章主要介紹了Python3中對(duì)range()逆序的解釋,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • python中property屬性的介紹及其應(yīng)用詳解

    python中property屬性的介紹及其應(yīng)用詳解

    這篇文章主要介紹了python中property屬性的介紹及其應(yīng)用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Python升級(jí)導(dǎo)致yum、pip報(bào)錯(cuò)的解決方法

    Python升級(jí)導(dǎo)致yum、pip報(bào)錯(cuò)的解決方法

    這篇文章主要給大家介紹了因?yàn)镻ython升級(jí)導(dǎo)致yum、pip報(bào)錯(cuò)的解決方法,文中通過(guò)示例代碼將解決的方法介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)下吧。
    2017-09-09
  • 詳解Python匿名函數(shù)(lambda函數(shù))

    詳解Python匿名函數(shù)(lambda函數(shù))

    這篇文章主要介紹了Python匿名函數(shù)(lambda函數(shù)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Python3 用什么IDE開發(fā)工具比較好

    Python3 用什么IDE開發(fā)工具比較好

    這篇文章主要介紹了Python3 用什么IDE開發(fā)工具比較好,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • Python 照片人物背景替換的實(shí)現(xiàn)方法

    Python 照片人物背景替換的實(shí)現(xiàn)方法

    本文主要介紹了如何通過(guò)Python實(shí)現(xiàn)照片中人物背景圖的替換,甚至可以精細(xì)到頭發(fā)絲,感興趣的小伙伴可以看看
    2021-11-11
  • Python最小二乘法矩陣

    Python最小二乘法矩陣

    今天小編就為大家分享一篇關(guān)于Python最小二乘法矩陣,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • Python中使用ElementTree解析XML示例

    Python中使用ElementTree解析XML示例

    這篇文章主要介紹了Python中使用ElementTree解析XML示例,本文同時(shí)講解了XML基本概念介紹、XML幾種解析方法和ElementTree解析實(shí)例,需要的朋友可以參考下
    2015-06-06

最新評(píng)論

永川市| 藁城市| 赫章县| 城口县| 本溪市| 临高县| 新绛县| 化州市| 稻城县| 互助| 定安县| 鄄城县| 乌拉特后旗| 郴州市| 新巴尔虎右旗| 洪泽县| 南陵县| 泰来县| 康保县| 阿鲁科尔沁旗| 吴旗县| 濮阳市| 临沭县| 南投市| 策勒县| 益阳市| 库车县| 灵石县| 建德市| 平舆县| 若尔盖县| 永登县| 介休市| 郯城县| 监利县| 东安县| 宝鸡市| 大竹县| 上栗县| 仙游县| 阿克苏市|