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

matplotlib繪制多子圖共享鼠標(biāo)光標(biāo)的方法示例

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

matplotlib官方除了提供了鼠標(biāo)十字光標(biāo)的示例,還提供了同一圖像內(nèi)多子圖共享光標(biāo)的示例,其功能主要由widgets模塊中的MultiCursor類提供支持。

MultiCursor類與Cursor類參數(shù)類似,差異主要在:

  • Cursor類參數(shù)只有一個(gè)ax,即需要顯示光標(biāo)的子圖;MultiCursor類參數(shù)為canvasaxes,其中axes為需要共享光標(biāo)的子圖列表。
  • Cursor類中,光標(biāo)默認(rèn)是十字線;MultiCursor類中,光標(biāo)默認(rèn)為豎線。

官方示例

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

在這里插入圖片描述

簡(jiǎn)易修改版

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

到此這篇關(guān)于matplotlib繪制多子圖共享鼠標(biāo)光標(biāo)的方法示例的文章就介紹到這了,更多相關(guān)matplotlib 多子圖鼠標(biāo)光標(biāo)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python貪吃蛇核心功能實(shí)現(xiàn)下

    python貪吃蛇核心功能實(shí)現(xiàn)下

    我想大家都玩過(guò)諾基亞上面的貪吃蛇吧,這篇文章將帶你一步步用python語(yǔ)言實(shí)現(xiàn)一個(gè)snake小游戲,文中的示例代碼講解詳細(xì),感興趣的可以了解一下
    2022-09-09
  • Python單元和文檔測(cè)試實(shí)例詳解

    Python單元和文檔測(cè)試實(shí)例詳解

    這篇文章主要介紹了Python單元和文檔測(cè)試,結(jié)合實(shí)例形式分析了Python單元測(cè)試模塊unittest及文檔測(cè)試模塊doctest相關(guān)使用技巧,需要的朋友可以參考下
    2019-04-04
  • Python圖像處理之簡(jiǎn)單畫板實(shí)現(xiàn)方法示例

    Python圖像處理之簡(jiǎn)單畫板實(shí)現(xiàn)方法示例

    這篇文章主要介紹了Python圖像處理之簡(jiǎn)單畫板實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Python基于cv2模塊與numpy模塊的數(shù)值計(jì)算及矩形圖形繪制簡(jiǎn)單操作技巧,需要的朋友可以參考下
    2018-08-08
  • Python開發(fā)如何在ubuntu 15.10 上配置vim

    Python開發(fā)如何在ubuntu 15.10 上配置vim

    這篇文章主要介紹了Python開發(fā)如何在ubuntu 15.10 上配置vim 的相關(guān)資料,需要的朋友可以參考下
    2016-01-01
  • 分享Python?的24個(gè)編程超好用技巧

    分享Python?的24個(gè)編程超好用技巧

    這篇文章主要給大家分享Python?的24個(gè)編程超好用技巧,下面分享一些python技巧和?tips,這些技巧將根據(jù)其首字母按?A-Z?的順序進(jìn)行展示,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-02-02
  • 對(duì)Python3 * 和 ** 運(yùn)算符詳解

    對(duì)Python3 * 和 ** 運(yùn)算符詳解

    今天小編就為大家分享一篇對(duì)Python3 * 和 ** 運(yùn)算符詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-02-02
  • Opencv實(shí)現(xiàn)傾斜圖片轉(zhuǎn)正示例

    Opencv實(shí)現(xiàn)傾斜圖片轉(zhuǎn)正示例

    本文主要介紹了Opencv實(shí)現(xiàn)傾斜圖片轉(zhuǎn)正示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • Python Reduce函數(shù)的高級(jí)用法詳解

    Python Reduce函數(shù)的高級(jí)用法詳解

    這篇文章主要介紹了reduce函數(shù)的工作原理和應(yīng)用,同時(shí)提供豐富的示例代碼,方便更好地理解如何使用reduce函數(shù)來(lái)輕松解決復(fù)雜的數(shù)據(jù)聚合問題,需要的可以參考下
    2023-11-11
  • python math模塊的基本使用教程

    python math模塊的基本使用教程

    這篇文章主要介紹了python math模塊的基本使用教程,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2021-01-01
  • python本地降級(jí)pip的方法步驟

    python本地降級(jí)pip的方法步驟

    高版本的pip在使用過(guò)程中會(huì)出現(xiàn)很多的不兼容問題,而且不留神很容易把pip給升級(jí)了,下面這篇文章主要給大家介紹了關(guān)于python本地降級(jí)pip的方法步驟,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2022-12-12

最新評(píng)論