濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > matplotlib之多邊形選區(qū)(PolygonSelector)的使用

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

熱門標(biāo)簽:高德地圖標(biāo)注字母 騰訊地圖標(biāo)注有什么版本 柳州正規(guī)電銷機(jī)器人收費(fèi) 千呼ai電話機(jī)器人免費(fèi) 深圳網(wǎng)絡(luò)外呼系統(tǒng)代理商 申請(qǐng)辦個(gè)400電話號(hào)碼 400電話辦理費(fèi)用收費(fèi) 鎮(zhèn)江人工外呼系統(tǒng)供應(yīng)商 外呼系統(tǒng)前面有錄音播放嗎

多邊形選區(qū)概述

多邊形選區(qū)是一種常見(jiàn)的對(duì)象選擇方式,在一個(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)無(wú)關(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ū)對(duì)角線,"none"無(wú)外觀,類型為字符串,默認(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”: 移動(dòng)已存在的選區(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

案例說(shuō)明

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

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

控制臺(tái)輸出:

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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • 詳解Golang并發(fā)操作中常見(jiàn)的死鎖情形
  • Go 語(yǔ)言中的死鎖問(wèn)題解決
  • Go語(yǔ)言死鎖與goroutine泄露問(wèn)題的解決
  • golang coroutine 的等待與死鎖用法
  • go select編譯期的優(yōu)化處理邏輯使用場(chǎng)景分析
  • Django實(shí)現(xiàn)jquery select2帶搜索的下拉框
  • Go語(yǔ)言使用select{}阻塞main函數(shù)介紹
  • golang中的select關(guān)鍵字用法總結(jié)
  • Go select 死鎖的一個(gè)細(xì)節(jié)

標(biāo)簽:烏蘭察布 哈爾濱 海南 平頂山 大慶 郴州 合肥 烏蘭察布

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《matplotlib之多邊形選區(qū)(PolygonSelector)的使用》,本文關(guān)鍵詞  matplotlib,之,多邊形,選區(qū),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《matplotlib之多邊形選區(qū)(PolygonSelector)的使用》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于matplotlib之多邊形選區(qū)(PolygonSelector)的使用的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    潜江市| 青海省| 西藏| 和龙市| 孝昌县| 涿州市| 资中县| 辰溪县| 霍城县| 潞西市| 大姚县| 柘城县| 罗源县| 思茅市| 孟州市| 永清县| 什邡市| 西乌珠穆沁旗| 平利县| 旬阳县| 清涧县| 黄骅市| 松原市| 屯留县| 澳门| 西和县| 微山县| 兴文县| 浠水县| 遂川县| 海盐县| 瑞安市| 岢岚县| 浪卡子县| 常宁市| 堆龙德庆县| 四川省| 常山县| 东安县| 巴南区| 通渭县|