gpt4 book ai didi

python - 如何更改交互式缩放矩形的颜色?

转载 作者:行者123 更新时间:2023-12-03 14:42:37 25 4
gpt4 key购买 nike

我有一个简单的交互式情节。当我点击放大镜按钮时,我可以绘制一个矩形来进行交互式缩放。您可以在下图中看到虚线矩形。

enter image description here

但是,当我在深色背景上使用白色网格(使用 plt.style.use('dark_background') )时,缩放矩形几乎不可见。它仍然存在,但在一个主要是黑色的情节上是黑色的。

enter image description here

为完整起见,使用 Matplotlib 3.1.3 生成的图如下:

import matplotlib.pyplot as plt
import numpy as np

plt.style.use('dark_background')

fig = plt.figure()
ax = fig.add_subplot(111)

data = 2.5 * np.random.randn(400) + 3
ax.plot(data)
plt.show()

所以我的问题是:如何更改缩放矩形的颜色?

最佳答案

这取决于您使用的是什么后端,没有(至少我不知道)通用解决方案。正如评论中所述,这只能通过猴子补丁来实现。这是我使用 Qt5 后端的尝试。请注意,您还需要安装 PyQt5 才能完成这项工作。

from PyQt5 import QtGui, QtCore
from matplotlib.backends.backend_qt5 import FigureCanvasQT

# extending the original FigureCanvasQT class

class NewFigureCanvasQT(FigureCanvasQT):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

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):
pen = QtGui.QPen(QtCore.Qt.red, 1 / self._dpi_ratio, # <-- change the color here
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()

# do the imports and replace the old FigureCanvasQT
import matplotlib
import matplotlib.pyplot as plt
matplotlib.backends.backend_qt5.FigureCanvasQT = NewFigureCanvasQT
# switch backend and setup the dark background
matplotlib.use('Qt5Agg')
matplotlib.style.use('dark_background')

# do the plotting
plt.plot(range(9))
plt.show()
产生以下图片:
Result
编辑 :
这似乎在 3.3.1 版中得到修复。 See the release notes.

关于python - 如何更改交互式缩放矩形的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61296297/

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