Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 35c1bfd

Browse files
committed
Avoid "dash motion" in qt zoom box.
Drawing the whole zoom box as a single rectangle causes the dashes on the "last" edge of the rectangle to keep shifting (basically depending on the total length of the other three edges). Instead, draw the zoom box as 4 separate lines always going from the "fixed" point (x0, y0) towards to "mobile" point (x1, y1) so to avoid this effect.
1 parent 015ff47 commit 35c1bfd

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

lib/matplotlib/backends/backend_qt5.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -478,16 +478,23 @@ def drawRectangle(self, rect):
478478
# Draw the zoom rectangle to the QPainter. _draw_rect_callback needs
479479
# to be called at the end of paintEvent.
480480
if rect is not None:
481+
x0, y0, w, h = [pt / self._dpi_ratio for pt in rect]
482+
x1 = x0 + w
483+
y1 = y0 + h
481484
def _draw_rect_callback(painter):
482-
scaled_rect = [pt / self._dpi_ratio for pt in rect]
483485
pen = QtGui.QPen(QtCore.Qt.black, 1 / self._dpi_ratio)
484486
pen.setDashPattern([3, 3])
485-
painter.setPen(pen)
486-
painter.drawRect(*scaled_rect)
487-
pen.setDashOffset(3)
488-
pen.setColor(QtCore.Qt.white)
489-
painter.setPen(pen)
490-
painter.drawRect(*scaled_rect)
487+
for color, offset in [
488+
(QtCore.Qt.black, 0), (QtCore.Qt.white, 3)]:
489+
pen.setDashOffset(offset)
490+
pen.setColor(color)
491+
painter.setPen(pen)
492+
# Draw the lines from x0, y0 towards x1, y1 so that the
493+
# dashes don't "jump" when moving the zoom box.
494+
painter.drawLine(x0, y0, x0, y1)
495+
painter.drawLine(x0, y0, x1, y0)
496+
painter.drawLine(x0, y1, x1, y1)
497+
painter.drawLine(x1, y0, x1, y1)
491498
else:
492499
def _draw_rect_callback(painter):
493500
return

0 commit comments

Comments
 (0)