4K屏幕上的matplotlib

2024-05-19 14:32:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用下面的脚本(在Jupyter笔记本和JupyterLab上)使用matplotlib qt界面选择图像上的ROI。该脚本在我测试过的任何一台计算机上都能完美运行,除了在我自己的笔记本电脑上,当我将屏幕分辨率设置为4k时。当我想要选择ROI时,打印的图像会被剪裁(带有ROI的裁剪图像:
Cropped image with ROI)。如果我选择1080p的屏幕分辨率,我就不会有这个问题(和其他人一样)。此外,如果我隐藏绘图窗口并再次打开,剪裁的部分将消失。每次修改ROI时,它都会再次出现。 关于如何解决这个问题有什么线索吗

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import RectangleSelector

ROI = [0,0,0,0]

def line_select_callback(eclick, erelease):
    global ROI
    'click and erelease are the press and release events'
    x1, y1 = eclick.xdata, eclick.ydata
    x2, y2 = erelease.xdata, erelease.ydata
    ROI = [np.int(x1),np.int(x2),np.int(y1),np.int(y2)]
    print("(%3.2f, %3.2f) --> (%3.2f, %3.2f)" % (x1, y1, x2, y2))
    print(" The button you used were: %s %s" % (eclick.button, erelease.button))


def toggle_selector(event):

    print(' Key pressed.')
    if event.key in ['Q', 'q'] and toggle_selector.RS.active:
        print(' RectangleSelector deactivated.')
        toggle_selector.RS.set_active(False)
    if event.key in ['A', 'a'] and not toggle_selector.RS.active:
        print(' RectangleSelector activated.')
        toggle_selector.RS.set_active(True)

%matplotlib qt
delta = 0.025
x = y = np.arange(-3.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2

fig, current_ax = plt.subplots()
plt.imshow(Z)
toggle_selector.RS = RectangleSelector(current_ax, line_select_callback,
                                       drawtype='box', useblit=True,
                                       button=[1, 3],  # don't use middle button
                                       minspanx=5, minspany=5,
                                       spancoords='pixels',
                                       interactive=True)
plt.connect('key_press_event', toggle_selector)

plt.show()

Tags: andeventmatplotlibnpbuttonpltselectorint