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

matplotlib繪制鼠標(biāo)的十字光標(biāo)的實(shí)現(xiàn)(內(nèi)置方式)

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

相對(duì)于echarts等基于JavaScript的圖表庫(kù),matplotlib的交互能力相對(duì)較差。
在實(shí)際應(yīng)用中,我們經(jīng)常想使用十字光標(biāo)來(lái)定位數(shù)據(jù)坐標(biāo),matplotlib內(nèi)置提供支持。

官方示例

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

在這里插入圖片描述

原理

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

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

簡(jiǎn)化案例

光標(biāo)改為灰色豎虛線,線寬為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

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

相關(guān)文章

  • python創(chuàng)建與遍歷List二維列表的方法

    python創(chuàng)建與遍歷List二維列表的方法

    這篇文章主要介紹了python創(chuàng)建與遍歷List二維列表的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下
    2019-08-08
  • Django應(yīng)用程序入口WSGIHandler源碼解析

    Django應(yīng)用程序入口WSGIHandler源碼解析

    這篇文章主要介紹了Django應(yīng)用程序入口WSGIHandler源碼解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • python實(shí)現(xiàn)的AES雙向?qū)ΨQ加密解密與用法分析

    python實(shí)現(xiàn)的AES雙向?qū)ΨQ加密解密與用法分析

    這篇文章主要介紹了python實(shí)現(xiàn)的AES雙向?qū)ΨQ加密解密與用法,簡(jiǎn)單分析了AES加密解密算法的基本概念并結(jié)合實(shí)例形式給出了AES加密解密算法的相關(guān)實(shí)現(xiàn)技巧與使用注意事項(xiàng),需要的朋友可以參考下
    2017-05-05
  • Pytho樹的直徑的計(jì)算實(shí)現(xiàn)

    Pytho樹的直徑的計(jì)算實(shí)現(xiàn)

    樹的直徑是樹中任意兩個(gè)節(jié)點(diǎn)之間最長(zhǎng)路徑的長(zhǎng)度,本文主要介紹了Pytho樹的直徑的計(jì)算實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-11-11
  • Python中低維數(shù)組填充高維數(shù)組的實(shí)現(xiàn)

    Python中低維數(shù)組填充高維數(shù)組的實(shí)現(xiàn)

    今天小編就為大家分享一篇Python中低維數(shù)組填充高維數(shù)組的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-12-12
  • python在前端頁(yè)面使用?MySQLdb?連接數(shù)據(jù)

    python在前端頁(yè)面使用?MySQLdb?連接數(shù)據(jù)

    這篇文章主要介紹了MySQLdb?連接數(shù)據(jù)的使用,文章主要介紹的相關(guān)內(nèi)容又插入數(shù)據(jù),刪除數(shù)據(jù),更新數(shù)據(jù),搜索數(shù)據(jù),需要的小伙伴可以參考一下
    2022-03-03
  • 七個(gè)非常實(shí)用的Python工具包總結(jié)

    七個(gè)非常實(shí)用的Python工具包總結(jié)

    Python 擁有海量的包,無(wú)論是普通任務(wù)還是復(fù)雜任務(wù),我們經(jīng)常在應(yīng)用程序中使用大量的工具包.本文我將討論一些常被低估的數(shù)據(jù)科學(xué)包,包括:數(shù)據(jù)清理、應(yīng)用程序開發(fā)和調(diào)試方面,需要的朋友可以參考下
    2021-06-06
  • python操作mysql中文顯示亂碼的解決方法

    python操作mysql中文顯示亂碼的解決方法

    這篇文章主要介紹了python操作mysql中文顯示亂碼的解決方法,是Python數(shù)據(jù)庫(kù)程序設(shè)計(jì)中經(jīng)常會(huì)遇到的問(wèn)題,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2014-10-10
  • 解決pycharm導(dǎo)入numpy包的和使用時(shí)報(bào)錯(cuò):RuntimeError: The current Numpy installation (‘D:\\python3.6\\lib\\site-packa的問(wèn)題

    解決pycharm導(dǎo)入numpy包的和使用時(shí)報(bào)錯(cuò):RuntimeError: The current Numpy ins

    這篇文章主要介紹了解決pycharm導(dǎo)入numpy包的和使用時(shí)報(bào)錯(cuò):RuntimeError: The current Numpy installation (‘D:\\python3.6\\lib\\site-packa的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • python輪詢機(jī)制控制led實(shí)例

    python輪詢機(jī)制控制led實(shí)例

    這篇文章主要介紹了python輪詢機(jī)制控制led實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-05-05

最新評(píng)論

东至县| 醴陵市| 林西县| 都江堰市| 枝江市| 临城县| 辛集市| 乌鲁木齐县| 平原县| 蒙自县| 盐池县| 华池县| 江华| 荣成市| 上杭县| 郎溪县| 卫辉市| 孟州市| 汤阴县| 福安市| 孟津县| 上杭县| 北京市| 镇远县| 荆州市| 濉溪县| 晋中市| 黔西县| 广丰县| 开鲁县| 楚雄市| 永靖县| 南阳市| 融水| 仁化县| 舞阳县| 静海县| 武胜县| 大田县| 新兴县| 海淀区|