matplot畫圖,python畫十字_matplotlib繪制鼠標的十字光標的實現(內置方式)

 2023-11-11 阅读 16 评论 0

摘要:相對于echarts等基于JavaScript的圖表庫,matplotlib的交互能力相對較差。在實際應用中,我們經常想使用十字光標來定位數據坐標,matplotlib內置提供支持。官方示例matplot畫圖,matplotlib提供了官方示例https://matplotlib.org/gallery/widgets/cursor.h

相對于echarts等基于JavaScript的圖表庫,matplotlib的交互能力相對較差。

在實際應用中,我們經常想使用十字光標來定位數據坐標,matplotlib內置提供支持。

官方示例

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

from matplotlib.widgets import Cursor

import numpy as np

import matplotlib.pyplot as plt

matplotlib中文手冊,# Fixing random state for reproducibility

np.random.seed(19680801)

fig = plt.figure(figsize=(8, 6))

ax = fig.add_subplot(111, facecolor='#FFFFCC')

matplotlib 顏色。x, y = 4*(np.random.rand(2, 100) - .5)

ax.plot(x, y, 'o')

ax.set_xlim(-2, 2)

ax.set_ylim(-2, 2)

Python畫一條豎線?# Set useblit=True on most backends for enhanced performance.

cursor = Cursor(ax, useblit=True, color='red', linewidth=2)

plt.show()

原理

matplotlib text?由源碼可知,實現十字光標的關鍵在于widgets模塊中的Cursor類。

class matplotlib.widgets.Cursor(ax, horizOn=True, vertOn=True, useblit=False, **lineprops)

ax:參數類型matplotlib.axes.Axes,即需要添加十字光標的子圖。

horizOn:布爾值,是否顯示十字光標中的橫線,默認值為顯示。

matplotlib 交互?vertOn:布爾值,是否顯示十字光標中的豎線,默認值為顯示。

useblit:布爾值,是否使用優化模式,默認值為否,優化模式需要后端支持。

**lineprops:十字光標線形屬性, 參見axhline函數支持的屬性,https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.axhline.html#matplotlib.axes.Axes.axhline。

簡化案例

python畫箭頭?光標改為灰色豎虛線,線寬為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繪制鼠標的十字光標的實現(內置方式)的文章就介紹到這了,更多相關matplotlib 十字光標內容請搜索得牛網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持得牛網!

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/170372.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息