欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

matplotlib繪制鼠標的十字光標的實現(xiàn)(內置方式)

 更新時間:2021年01月06日 08:41:26   作者:mighty13  
這篇文章主要介紹了matplotlib繪制鼠標的十字光標的實現(xiàn)(內置方式),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

相對于echarts等基于JavaScript的圖表庫,matplotlib的交互能力相對較差。
在實際應用中,我們經常想使用十字光標來定位數(shù)據坐標,matplotlib內置提供支持。

官方示例

matplotlib提供了官方示例https://matplotlib.org/gallery/widgets/cursor.html

from matplotlib.widgets import Cursor
import numpy as np
import matplotlib.pyplot as plt

# Fixing random state for reproducibility
np.random.seed(19680801)

fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, facecolor='#FFFFCC')

x, y = 4*(np.random.rand(2, 100) - .5)
ax.plot(x, y, 'o')
ax.set_xlim(-2, 2)
ax.set_ylim(-2, 2)

# Set useblit=True on most backends for enhanced performance.
cursor = Cursor(ax, useblit=True, color='red', linewidth=2)

plt.show()

在這里插入圖片描述

原理

由源碼可知,實現(xiàn)十字光標的關鍵在于widgets模塊中的Cursor類。
class matplotlib.widgets.Cursor(ax, horizOn=True, vertOn=True, useblit=False, **lineprops)

  • ax:參數(shù)類型matplotlib.axes.Axes,即需要添加十字光標的子圖。
  • horizOn:布爾值,是否顯示十字光標中的橫線,默認值為顯示。
  • vertOn:布爾值,是否顯示十字光標中的豎線,默認值為顯示。
  • useblit:布爾值,是否使用優(yōu)化模式,默認值為否,優(yōu)化模式需要后端支持。
  • **lineprops:十字光標線形屬性, 參見axhline函數(shù)支持的屬性,https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.axhline.html#matplotlib.axes.Axes.axhline。

簡化案例

光標改為灰色豎虛線,線寬為1。

from matplotlib.widgets import Cursor
import matplotlib.pyplot as plt

ax = plt.gca()
cursor = Cursor(ax, horizOn=False, vertOn= True, useblit=False, color='grey', linewidth=1,linestyle='--')
plt.show()

在這里插入圖片描述

## 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

到此這篇關于matplotlib繪制鼠標的十字光標的實現(xiàn)(內置方式)的文章就介紹到這了,更多相關matplotlib 十字光標內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 分享幾道你可能遇到的python面試題

    分享幾道你可能遇到的python面試題

    最近去筆試,在面試過程中遇到了幾個編程題,比較基礎。所以想著總結一下,所以下面這篇文章主要給大家分享了幾道你可能遇到的python面試題,文中給出了詳細的示例代碼供大家參考學習,需要的朋友們下面來一起看看吧。
    2017-07-07
  • pycharm激活碼快速激活及使用步驟

    pycharm激活碼快速激活及使用步驟

    這篇文章主要介紹了pycharm激活碼快速激活及使用步驟,本文分步驟給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • Python連接PostgreSQL數(shù)據庫的方法

    Python連接PostgreSQL數(shù)據庫的方法

    大家應該都有所了解,python可以操作多種數(shù)據庫,諸如SQLite、MySql、PostgreSQL等,這里不對所有的數(shù)據庫操作方法進行贅述,只針對目前項目中用到的PostgreSQL做一下簡單介紹,主要是Python連接PostgreSQL數(shù)據庫的方法。有需要的朋友們可以參考借鑒,下面來一起看看吧。
    2016-11-11
  • python數(shù)據處理之如何修改索引和行列

    python數(shù)據處理之如何修改索引和行列

    這篇文章主要介紹了python數(shù)據處理之如何修改索引和行列問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 詳解利用上下文管理器擴展Python計時器

    詳解利用上下文管理器擴展Python計時器

    本文將和大家一起了解什么是上下文管理器?和?Python?的?with?語句,以及如何完成自定義。然后擴展?Timer?以便它也可以用作上下文管理器,感興趣的可以了解一下
    2022-06-06
  • Matlab、Python為工具解析數(shù)據可視化之美

    Matlab、Python為工具解析數(shù)據可視化之美

    下面介紹一些數(shù)據可視化的作品(包含部分代碼),主要是地學領域,可遷移至其他學科,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2021-11-11
  • Django model序列化為json的方法示例

    Django model序列化為json的方法示例

    這篇文章主要介紹了Django model序列化為json的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-10-10
  • Django開發(fā)的簡易留言板案例詳解

    Django開發(fā)的簡易留言板案例詳解

    這篇文章主要介紹了Django開發(fā)的簡易留言板,結合實例形式詳細分析了基于Python框架Django開發(fā)留言板的具體文件結構、流程步驟與相關操作技巧,需要的朋友可以參考下
    2018-12-12
  • TensorFlow用expand_dim()來增加維度的方法

    TensorFlow用expand_dim()來增加維度的方法

    今天小編就為大家分享一篇TensorFlow用expand_dim()來增加維度的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • python如何實現(xiàn)convolution neural network卷積神經網絡算法

    python如何實現(xiàn)convolution neural network卷積神經網絡算法

    卷積神經網絡(CNN)是深度學習中重要的算法之一,主要應用于圖像識別和處理領域,其基本原理是模擬人類視覺系統(tǒng),通過卷積層、激活函數(shù)和池化層等組件提取圖像的特征,并通過全連接層進行分類或其他任務,CNN訓練過程中使用大量標記圖像數(shù)據
    2024-10-10

最新評論