I have a simple interactive plot. When I click on the magnifying glass button I can draw a rectangle to do interactive zooming. You can see the dotted rectangle in the image below.
However, when I use white grid on a dark background (with plt.style.use('dark_background')
), the zoom rectangle is barely visible. It is still there but black on a largely black plot.
For completeness, the plots where generated with Matplotlib 3.1.3 as follows:
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()
So my question therefore is: how can I change the color of the zoom rectangle?
If you like, you can change the border color and/or inside color to the same color or different colors of your choice. This tutorial will show you how to change the color of the translucent selection rectangle for your account in Windows 7, Windows 8, and Windows 10.
Click the toolbar button to activate panning and zooming, then put your mouse somewhere over an axes. Press the left mouse button and hold it to pan the figure, dragging it to a new position. When you release it, the data under the point where you pressed will be moved to the point where you released.
Again, all of these buttons should feel very familiar to any user of a web browser. This button has two modes: pan and zoom. Click the toolbar button to activate panning and zooming, then put your mouse somewhere over an axes. Press the left mouse button and hold it to pan the figure, dragging it to a new position.
This option is enabled by default. Display the interactive selection graphic —Maintains the selection shape graphic after drawing a selection shape. This can be useful for making additional dynamic interactive selections. Click and drag the graphic to a new location and update the selection.
It depends on what backend you're using there is no (at least I don't know a) universal solution. As stated in the comments this can be achieved only with monkey-patching. Here is my attempt using Qt5 backend. Note that you need to have PyQt5 installed too in order to make this work.
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()
which produces the following picture:
EDIT: This seem to be fixed in release 3.3.1. See the release notes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With