优化矩形处理逻辑,支持不同版本的matplotlib中axvspan返回的对象类型

This commit is contained in:
marques
2025-09-04 11:45:18 +08:00
parent ca767d46cb
commit 2a0a62d201

View File

@ -12,7 +12,7 @@ from matplotlib import gridspec
from matplotlib.backends.backend_qt import NavigationToolbar2QT
from matplotlib.backends.backend_qtagg import FigureCanvasQTAgg
from matplotlib.collections import PolyCollection
from matplotlib.patches import Rectangle
from matplotlib.patches import Rectangle, Polygon
from numpy import array, zeros, append, linspace, place, nan
from overrides import overrides
@ -2004,15 +2004,16 @@ class MainWindow_SA_label(QMainWindow):
def __get_x_and_width__(self, event_rect):
"""Get the width of a rectangle in data coordinates."""
# pyside6.9.0 axvspan 返回的是一个Rectangle对象
# pyside6.9.1 axvspan 返回的是一个PolyCollection对象
# matplotlib >=3.9 axvspan 返回的是一个Rectangle对象
# matplorlib < 3.9 axvspan 返回的是一个PolyCollection对象
if isinstance(event_rect, Rectangle):
x = event_rect.get_x()
width = event_rect.get_width()
elif isinstance(event_rect, PolyCollection):
verts = event_rect.get_paths()[0].vertices
elif isinstance(event_rect, Polygon):
verts = event_rect.get_xy()
x = verts[0, 0]
width = verts[2, 0] - verts[0, 0]
else:
raise TypeError("Unsupported event_rect type")
return x, width