gpt4 book ai didi

python - 更改matplotlib中zoom-rect的边缘颜色

转载 作者:行者123 更新时间:2023-11-30 23:14:49 25 4
gpt4 key购买 nike

我使用 python+matplotlib+pyqt 编写了一个用于光谱分析的应用程序。应用程序中的绘图需要具有黑色背景、白色轴和符号。我保留了 matplotlib 的默认导航工具栏。由于反转颜色设置,我遇到的一个问题是缩放矩形的边缘是不可见的,因为它是黑色的。有没有一种简单的方法可以将缩放矩形的边缘颜色更改为明亮的颜色,例如白色。

提前谢谢您。

最佳答案

添加 Gloweye 的响应,在 PyQt5 中你应该这样做。

import six

import ctypes
import sys

from PyQt5 import QtCore, QtGui

from PyQt5.QtWidgets import QSizePolicy, QWidget, QVBoxLayout
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import (
FigureCanvasQTAgg as FigureCanvas,
NavigationToolbar2QT as NavigationToolbar)

QT_API = 'PyQt5'
DEBUG = False

_decref = ctypes.pythonapi.Py_DecRef
_decref.argtypes = [ctypes.py_object]
_decref.restype = None

class MplCanvas(FigureCanvas):
def __init__(self):
FigureCanvas.__init__(self,self.fig)
FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding,
QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)


#Change de color of rectangle zoom toolbar rewriting painEvent
#the original code is in the backend_qt5agg.py file inside
#matplotlib/backends directory
def paintEvent(self, e):
"""
Copy the image from the Agg canvas to the qt.drawable.
In Qt, all drawing should be done inside of here when a widget is
shown onscreen.
"""
# if the canvas does not have a renderer, then give up and wait for
# FigureCanvasAgg.draw(self) to be called
if not hasattr(self, 'renderer'):
return

if DEBUG:
print('FigureCanvasQtAgg.paintEvent: ', self,
self.get_width_height())

if len(self.blitbox) == 0:
# matplotlib is in rgba byte order. QImage wants to put the bytes
# into argb format and is in a 4 byte unsigned int. Little endian
# system is LSB first and expects the bytes in reverse order
# (bgra).
if QtCore.QSysInfo.ByteOrder == QtCore.QSysInfo.LittleEndian:
stringBuffer = self.renderer._renderer.tostring_bgra()
else:
stringBuffer = self.renderer._renderer.tostring_argb()

refcnt = sys.getrefcount(stringBuffer)

# convert the Agg rendered image -> qImage
qImage = QtGui.QImage(stringBuffer, self.renderer.width,
self.renderer.height,
QtGui.QImage.Format_ARGB32)
if hasattr(qImage, 'setDevicePixelRatio'):
# Not available on Qt4 or some older Qt5.
qImage.setDevicePixelRatio(self._dpi_ratio)
# get the rectangle for the image
rect = qImage.rect()
p = QtGui.QPainter(self)
# reset the image area of the canvas to be the back-ground color
p.eraseRect(rect)
# draw the rendered image on to the canvas
p.drawPixmap(QtCore.QPoint(0, 0), QtGui.QPixmap.fromImage(qImage))

# draw the zoom rectangle to the QPainter
########################################################
# HERE CHANGE THE COLOR, IN THIS EXAMPLE #
# THE COLOR IS WHITE #
########################################################
if self._drawRect is not None:
pen = QtGui.QPen(QtCore.Qt.white, 1 / self._dpi_ratio,
QtCore.Qt.DotLine)
p.setPen(pen)
x, y, w, h = self._drawRect
p.drawRect(x, y, w, h)
p.end()

# This works around a bug in PySide 1.1.2 on Python 3.x,
# where the reference count of stringBuffer is incremented
# but never decremented by QImage.
# TODO: revert PR #1323 once the issue is fixed in PySide.
del qImage
if refcnt != sys.getrefcount(stringBuffer):
_decref(stringBuffer)
else:
p = QtGui.QPainter(self)

while len(self.blitbox):
bbox = self.blitbox.pop()
l, b, r, t = bbox.extents
w = int(r) - int(l)
h = int(t) - int(b)
t = int(b) + h
reg = self.copy_from_bbox(bbox)
stringBuffer = reg.to_string_argb()
qImage = QtGui.QImage(stringBuffer, w, h,
QtGui.QImage.Format_ARGB32)
if hasattr(qImage, 'setDevicePixelRatio'):
# Not available on Qt4 or some older Qt5.
qImage.setDevicePixelRatio(self._dpi_ratio)
# Adjust the stringBuffer reference count to work
# around a memory leak bug in QImage() under PySide on
# Python 3.x
if QT_API == 'PySide' and six.PY3:
ctypes.c_long.from_address(id(stringBuffer)).value = 1

origin = QtCore.QPoint(l, self.renderer.height - t)
pixmap = QtGui.QPixmap.fromImage(qImage)
p.drawPixmap(origin / self._dpi_ratio, pixmap)

# draw the zoom rectangle to the QPainter
if self._drawRect is not None:
pen = QtGui.QPen(QtCore.Qt.black, 1 / self._dpi_ratio,
QtCore.Qt.DotLine)
p.setPen(pen)
x, y, w, h = self._drawRect
p.drawRect(x, y, w, h)

p.end()

对于 MATPLOTLIB 2.2.2

from PyQt5.QtWidgets import QSizePolicy, QWidget, QVBoxLayout
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import (
FigureCanvasQTAgg as FigureCanvas,
NavigationToolbar2QT as NavigationToolbar)
class MplCanvas(FigureCanvas):
def __init__(self):
FigureCanvas.__init__(self, self.fig)
FigureCanvas.setSizePolicy(self,
QSizePolicy.Expanding,
QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)

# HERE CHANGE THE COLOR OF ZOOM RECTANGLE
def drawRectangle(self, rect):
# Draw the zoom rectangle to the QPainter. _draw_rect_callback needs
# to be called at the end of paintEvent.
if rect is not None:
def _draw_rect_callback(painter):
# IN THIS EXAMPLE CHANGE BLACK FOR WHITE
pen = QtGui.QPen(QtCore.Qt.white, 1 / self._dpi_ratio,
QtCore.Qt.DotLine)
painter.setPen(pen)
painter.drawRect(*(pt / self._dpi_ratio for pt in rect))
else:
def _draw_rect_callback(painter):
return
self._draw_rect_callback = _draw_rect_callback
self.update()

class MplWidget (QWidget):

def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.canvas = MplCanvas()
# add the toolbar
self.ntb = NavigationToolbar(self.canvas, self)
self.vbl = QVBoxLayout()
self.vbl.addWidget(self.canvas)
self.vbl.addWidget(self.ntb)
self.setLayout(self.vbl)

关于python - 更改matplotlib中zoom-rect的边缘颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28599068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com