X Tutup
Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -1772,7 +1772,41 @@ def __init__(self, figure=None):
figure._original_dpi = getattr(figure, '_original_dpi', figure.dpi)
self._device_pixel_ratio = 1
self._blit_backgrounds = {}
self._overlay_primitives = []

super().__init__() # Typically the GUI widget init (if any).
def add_overlay_line(self, x1, y1, x2, y2, **style):

if not hasattr(self, "_overlay_primitives"):
self._overlay_primitives = []

self._overlay_primitives.append(
{"type": "line", "coords": (x1, y1, x2, y2), "style": style}
)

print("Added overlay:", self._overlay_primitives)

self.draw_idle()

def clear_overlay(self):
"""
Remove all overlay primitives and request a lightweight repaint.

This clears overlay elements drawn above the rendered figure
without triggering a full figure redraw.
"""
self._overlay_primitives.clear()
self._request_overlay_draw()


def _request_overlay_draw(self):
"""
Request a lightweight overlay repaint.

Backends must override this.
"""
raise NotImplementedError


callbacks = property(lambda self: self.figure._canvas_callbacks)
button_pick_id = property(lambda self: self.figure._button_pick_id)
Expand Down
68 changes: 67 additions & 1 deletion lib/matplotlib/backends/backend_qtagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,64 @@


class FigureCanvasQTAgg(FigureCanvasAgg, FigureCanvasQT):
def __init__(self, figure):
super().__init__(figure)

self._overlay_lines = []
self._overlay_start = None
self._overlay_end = None
self._drawing_overlay = False


def add_overlay_line(self, x1, y1, x2, y2, **style):
"""Add a lightweight overlay line drawn directly with Qt."""

self._overlay_lines.append((x1, y1, x2, y2, style))

self._request_overlay_draw()

def clear_overlay(self):
"""Remove all overlay drawings from the canvas."""
self._overlay_lines.clear()

self._request_overlay_draw()

def _request_overlay_draw(self):
self.update()

def mousePressEvent(self, event):
if event.button() == QtCore.Qt.MouseButton.LeftButton:
self._overlay_start = (event.position().x(), event.position().y())
self._overlay_end = None
self._drawing_overlay = True


def mouseMoveEvent(self, event):
if self._drawing_overlay and self._overlay_start:
self._overlay_end = (event.position().x(), event.position().y())
self._request_overlay_draw()


def mouseReleaseEvent(self, event):
if event.button() == QtCore.Qt.MouseButton.LeftButton and self._drawing_overlay:
self._overlay_end = (event.position().x(), event.position().y())
self._drawing_overlay = False

# store the finished line
x1, y1 = self._overlay_start
x2, y2 = self._overlay_end
self._overlay_lines.append((x1, y1, x2, y2, {}))

self._overlay_start = None
self._overlay_end = None

self._request_overlay_draw()




def paintEvent(self, event):
print("PAINT EVENT RUNNING")
"""
Copy the image from the Agg canvas to the qt.drawable.

Expand Down Expand Up @@ -67,7 +123,17 @@ def paintEvent(self, event):
if QT_API == "PySide2" and QtCore.__version_info__ < (5, 12):
ctypes.c_long.from_address(id(buf)).value = 1

self._draw_rect_callback(painter)
# --- Overlay drawing ---
for x1, y1, x2, y2, style in getattr(self, "_overlay_lines", []):

color = style.get("color", "red")
width = style.get("width", 2)

pen = QtGui.QPen(QtGui.QColor(color), width)
painter.setPen(pen)

painter.drawLine(int(x1), int(y1), int(x2), int(y2))

finally:
painter.end()

Expand Down
5 changes: 3 additions & 2 deletions lib/mpl_toolkits/mplot3d/axes3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,13 @@ def __init__(
If None, defaults to 4:4:3
computed_zorder : bool, default: True
If True, the draw order is computed based on the average position
of the `.Artist`\\s along the view direction.
along the view direction for supported artist types (currently
Collections and Patches only).
Set to False if you want to manually control the order in which
Artists are drawn on top of each other using their *zorder*
attribute. This can be used for fine-tuning if the automatic order
does not produce the desired result. Note however, that a manual
zorder will only be correct for a limited view angle. If the figure
order will only be correct for a limited view angle. If the figure
is rotated by the user, it will look wrong from certain angles.

**kwargs
Expand Down
12 changes: 12 additions & 0 deletions overlay_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
canvas = fig.canvas

plt.show(block=False)

canvas.add_overlay_line(50, 50, 300, 300)
canvas.add_overlay_line(50, 300, 300, 50)

canvas.draw()
plt.pause(0.1)
Loading
X Tutup