Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Dragging Legend not working properly  #24755

Copy link
Copy link
Open
@inxp

Description

@inxp
Issue body actions

Bug summary

This may not be a bug. It is my use case.

I have embedded matplotlib in PyQt5 and I am using Ctrl+C to copy the figure to clipboard (for pasting to ppt)

So in the minimum example attached, pressing Ctrl+C reduces figure size to specified size (6x4). This is kind of a print preview for me. So I adjust legend/annotations in the smaller figure.

Dragging legend is not working properly in the smaller figure. It used to work in older versions for me. So I went through the changes. Reverting the change done in the following line of blit function of /backends/backend_qt.py fixes the problem (Changed as part of #17478)

self.repaint(l, self.rect().height() - t, w, h)
to
self.repaint(l, int(self.renderer.height / self.device_pixel_ratio)- t, w, h)

For the normal figure self.rect().height() is equal to int(self.renderer.height / self.device_pixel_ratio)

But for the smaller figure self.rect().height() is different from int(self.renderer.height / self.device_pixel_ratio)

Is this ok?

Thanks

Code for reproduction

import io
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QShortcut, QVBoxLayout
from PyQt5.QtCore import QMimeData
from PyQt5.QtGui import QKeySequence

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure

import random

class Window(QWidget):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.figure = Figure()

        data = [random.random() for _ in range(10)]

        ax = self.figure.add_subplot(111)
        ax.plot(data, '*-',label='RandomData')
        legend = ax.legend()
        
        self.canvas = FigureCanvas(self.figure)
        self.canvas.draw()

        legend.set_draggable(True,use_blit=True) # Dragging is not ok in smaller figure/ ok in normal figure
        # legend.set_draggable(True) # Dragging is ok in smaller figure when blitting is off


        ctc = QShortcut(QKeySequence("Ctrl+C"), self)
        ctc.activated.connect(self.copy_to_clipboard)

        layout = QVBoxLayout()
        layout.addWidget(self.canvas)
        self.setLayout(layout)

    def copy_to_clipboard(self):
        fig = self.canvas.figure
        fig.set_size_inches(6,4)
        buf = io.BytesIO()
        fig.savefig(buf,dpi=300,transparent=True,bbox_inches='tight')
        mimeData = QMimeData()
        mimeData.setData("PNG",buf.getvalue())
        QApplication.clipboard().setMimeData(mimeData)
        buf.close()

    
if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = Window()
    main.showMaximized()
    sys.exit(app.exec_())

Actual outcome

N5hiEqY3Hg

Expected outcome

gUBmnxj0rD

Additional information

No response

Operating system

Windows

Matplotlib Version

3.6.2

Matplotlib Backend

PyQt5

Python version

3.8.10

Jupyter version

NA

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.