服务器之家

服务器之家 > 正文

matplotlib绘制多子图共享鼠标光标的方法示例

时间:2021-08-23 10:48     来源/作者:mighty13

matplotlib官方除了提供了鼠标十字光标的示例,还提供了同一图像内多子图共享光标的示例,其功能主要由widgets模块中的multicursor类提供支持。

multicursor类与cursor类参数类似,差异主要在:

  • cursor类参数只有一个ax,即需要显示光标的子图;multicursor类参数为canvasaxes,其中axes为需要共享光标的子图列表。
  • cursor类中,光标默认是十字线;multicursor类中,光标默认为竖线。

官方示例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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()

matplotlib绘制多子图共享鼠标光标的方法示例

简易修改版

?
1
multi = multicursor(fig.canvas, (ax1, ax2), color='r', lw=1, horizon=true, verton=true)

matplotlib绘制多子图共享鼠标光标的方法示例

multicursor类源码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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()

到此这篇关于matplotlib绘制多子图共享鼠标光标的方法示例的文章就介绍到这了,更多相关matplotlib 多子图鼠标光标内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/mighty13/article/details/112145863

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
返回顶部