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

matplotlib之多邊形選區(qū)(PolygonSelector)的使用

 更新時(shí)間:2021年02月24日 14:06:43   作者:mighty13  
這篇文章主要介紹了matplotlib之多邊形選區(qū)(PolygonSelector)的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

多邊形選區(qū)概述

多邊形選區(qū)是一種常見的對象選擇方式,在一個(gè)子圖中,單擊鼠標(biāo)左鍵即構(gòu)建一個(gè)多邊形的端點(diǎn),最后一個(gè)端點(diǎn)與第一個(gè)端點(diǎn)重合即完成多邊形選區(qū),選區(qū)即為多個(gè)端點(diǎn)構(gòu)成的多邊形。在matplotlib中的多邊形選區(qū)屬于部件(widgets),matplotlib中的部件都是中性(neutral )的,即與具體后端實(shí)現(xiàn)無關(guān)。

多邊形選區(qū)具體實(shí)現(xiàn)定義為matplotlib.widgets.PolygonSelector類,繼承關(guān)系為:Widget->AxesWidget->_SelectorWidget->PolygonSelector。

PolygonSelector類的簽名為class matplotlib.widgets.PolygonSelector(ax, onselect, useblit=False, lineprops=None, markerprops=None, vertex_select_radius=15)

PolygonSelector類構(gòu)造函數(shù)的參數(shù)為:

  • ax:多邊形選區(qū)生效的子圖,類型為matplotlib.axes.Axes的實(shí)例。
  • onselect:多邊形選區(qū)完成后執(zhí)行的回調(diào)函數(shù),函數(shù)簽名為def onselect( vertices),vertices數(shù)據(jù)類型為列表,列表元素格式為(xdata,ydata)元組。
  • drawtype:多邊形選區(qū)的外觀,取值范圍為{"box", "line", "none"},"box"為多邊形框,"line"為多邊形選區(qū)對角線,"none"無外觀,類型為字符串,默認(rèn)值為"box"。
  • lineprops:多邊形選區(qū)線條的屬性,默認(rèn)值為dict(color='k', linestyle='-', linewidth=2, alpha=0.5)。
  • markerprops:多邊形選區(qū)端點(diǎn)的屬性,默認(rèn)值為dict(marker='o', markersize=7, mec='k', mfc='k', alpha=0.5)。
  • vertex_select_radius:多邊形端點(diǎn)的選擇半徑,浮點(diǎn)數(shù),默認(rèn)值為15,用于端點(diǎn)選擇或者多邊形閉合。

PolygonSelector類中的state_modifier_keys公有變量 state_modifier_keys定義了操作快捷鍵,類型為字典。

  • “move_all”: 移動已存在的選區(qū),默認(rèn)為"shift"。
  • “clear”:清除現(xiàn)有選區(qū),默認(rèn)為 "escape",即esc鍵。
  • “move_vertex”:正方形選區(qū),默認(rèn)為"control"。

PolygonSelector類中的verts特性返回多邊形選區(qū)中的多有端點(diǎn),類型為列表,元素為(x,y)元組,即端點(diǎn)的坐標(biāo)元組。

案例

官方案例,https://matplotlib.org/gallery/widgets/polygon_selector_demo.html

案例說明

單擊鼠標(biāo)左鍵創(chuàng)建端點(diǎn),最終點(diǎn)擊初始端點(diǎn)閉合多邊形,形成多邊形選區(qū)。選區(qū)外的數(shù)據(jù)元素顏色變淡,選區(qū)內(nèi)數(shù)據(jù)顏色保持不變。

按esc鍵取消選區(qū)。按shift鍵鼠標(biāo)可以移動多邊形選區(qū)位置,按ctrl鍵鼠標(biāo)可以移動多邊形選區(qū)某個(gè)端點(diǎn)的位置。退出程序時(shí),控制臺輸出選區(qū)內(nèi)數(shù)據(jù)元素的坐標(biāo)。

控制臺輸出:

Selected points:
[[2.0 2.0]
 [1.0 3.0]
 [2.0 3.0]]

案例代碼

import numpy as np

from matplotlib.widgets import PolygonSelector
from matplotlib.path import Path


class SelectFromCollection:
  """
  Select indices from a matplotlib collection using `PolygonSelector`.

  Selected indices are saved in the `ind` attribute. This tool fades out the
  points that are not part of the selection (i.e., reduces their alpha
  values). If your collection has alpha < 1, this tool will permanently
  alter the alpha values.

  Note that this tool selects collection objects based on their *origins*
  (i.e., `offsets`).

  Parameters
  ----------
  ax : `~matplotlib.axes.Axes`
    Axes to interact with.
  collection : `matplotlib.collections.Collection` subclass
    Collection you want to select from.
  alpha_other : 0 <= float <= 1
    To highlight a selection, this tool sets all selected points to an
    alpha value of 1 and non-selected points to *alpha_other*.
  """

  def __init__(self, ax, collection, alpha_other=0.3):
    self.canvas = ax.figure.canvas
    self.collection = collection
    self.alpha_other = alpha_other

    self.xys = collection.get_offsets()
    self.Npts = len(self.xys)

    # Ensure that we have separate colors for each object
    self.fc = collection.get_facecolors()
    if len(self.fc) == 0:
      raise ValueError('Collection must have a facecolor')
    elif len(self.fc) == 1:
      self.fc = np.tile(self.fc, (self.Npts, 1))

    self.poly = PolygonSelector(ax, self.onselect)
    self.ind = []

  def onselect(self, verts):
    path = Path(verts)
    self.ind = np.nonzero(path.contains_points(self.xys))[0]
    self.fc[:, -1] = self.alpha_other
    self.fc[self.ind, -1] = 1
    self.collection.set_facecolors(self.fc)
    self.canvas.draw_idle()

  def disconnect(self):
    self.poly.disconnect_events()
    self.fc[:, -1] = 1
    self.collection.set_facecolors(self.fc)
    self.canvas.draw_idle()


if __name__ == '__main__':
  import matplotlib.pyplot as plt

  fig, ax = plt.subplots()
  grid_size = 5
  grid_x = np.tile(np.arange(grid_size), grid_size)
  grid_y = np.repeat(np.arange(grid_size), grid_size)
  pts = ax.scatter(grid_x, grid_y)

  selector = SelectFromCollection(ax, pts)

  print("Select points in the figure by enclosing them within a polygon.")
  print("Press the 'esc' key to start a new polygon.")
  print("Try holding the 'shift' key to move all of the vertices.")
  print("Try holding the 'ctrl' key to move a single vertex.")

  plt.show()

  selector.disconnect()

  # After figure is closed print the coordinates of the selected points
  print('\nSelected points:')
  print(selector.xys[selector.ind])

到此這篇關(guān)于matplotlib之多邊形選區(qū)(PolygonSelector)的使用的文章就介紹到這了,更多相關(guān)matplotlib 多邊形選區(qū)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python中注釋用法簡單示例

    python中注釋用法簡單示例

    注釋即對程序代碼的解釋,在寫程序時(shí)需適當(dāng)使用注釋,以方便自己和他人理解程序各部分的作用,下面這篇文章主要給大家介紹了關(guān)于python中注釋用法的相關(guān)資料,需要的朋友可以參考下
    2023-02-02
  • python機(jī)器學(xué)習(xí)實(shí)現(xiàn)oneR算法(以鳶尾data為例)

    python機(jī)器學(xué)習(xí)實(shí)現(xiàn)oneR算法(以鳶尾data為例)

    本文主要介紹了python機(jī)器學(xué)習(xí)實(shí)現(xiàn)oneR算法(以鳶尾data為例),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Python函數(shù)基本使用原理詳解

    Python函數(shù)基本使用原理詳解

    這篇文章主要介紹了Python函數(shù)基本使用原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • 詳解django中視圖函數(shù)的FBV和CBV

    詳解django中視圖函數(shù)的FBV和CBV

    FBV是指視圖函數(shù)以普通函數(shù)的形式,CBV是指視圖函數(shù)以類的方式,這篇文章主要介紹了django中視圖函數(shù)的FBV和CBV,需要的朋友可以參考下
    2022-08-08
  • Python利用xmltodict模塊實(shí)現(xiàn)處理XML數(shù)據(jù)

    Python利用xmltodict模塊實(shí)現(xiàn)處理XML數(shù)據(jù)

    理解和處理XML數(shù)據(jù)在Python中是一項(xiàng)常見任務(wù),xmltodict便是一個(gè)Python庫,用于將XML數(shù)據(jù)解析為易于處理的Python字典,下面我們就來學(xué)習(xí)一下xmltodict庫的具體使用吧
    2023-11-11
  • Python實(shí)現(xiàn)線性擬合及繪圖的示例代碼

    Python實(shí)現(xiàn)線性擬合及繪圖的示例代碼

    在數(shù)據(jù)處理和繪圖中,我們通常會遇到直線或曲線的擬合問題,本文主要介紹了Python實(shí)現(xiàn)線性擬合及繪圖的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-04-04
  • Django連接MQTT的示例代碼

    Django連接MQTT的示例代碼

    本文主要介紹了Django連接MQTT的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • python控制臺英漢漢英電子詞典

    python控制臺英漢漢英電子詞典

    這篇文章主要為大家詳細(xì)介紹了python控制臺英漢漢英電子詞典,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2014-06-06
  • 一文理解Python命名機(jī)制

    一文理解Python命名機(jī)制

    這篇文章主要介紹的是Python的命名機(jī)制,文章回先提出問題,然后根據(jù)問題逐步解析,感興趣的小伙伴可以參考一下,希望對你有所幫助
    2021-10-10
  • Python socket實(shí)現(xiàn)的簡單通信功能示例

    Python socket實(shí)現(xiàn)的簡單通信功能示例

    這篇文章主要介紹了Python socket實(shí)現(xiàn)的簡單通信功能,結(jié)合實(shí)例形式分析了Python socket通信的相關(guān)概念、原理、客戶端與服務(wù)器端實(shí)現(xiàn)技巧以及socketserver模塊多并發(fā)簡單實(shí)現(xiàn)方法,需要的朋友可以參考下
    2018-08-08

最新評論