所以我一直在对PyQt和MPL进行大量的研究,而且我仍然遇到一些我无法解决的问题。
第一个是:如何更改FigureCanvas背景颜色(它是this image中的灰色部分)。 FigureCanvas没有facecolor或backgroundcolor的属性或我能想到或发现的任何其他内容。我可以改变轴上的颜色,图(这就是我可以看到的黑色背景),但不是画布本身。
第二个问题:如何摆脱十字线顶部仍然可见的鼠标指针?或者更好的是在光标进入mpl FigureCanvas小部件时更改光标(当它离开时再次返回)。
编辑:以下是我要求的部分代码。我通过以下方式从Qt调用MPL数据。
我的主窗口:
from PyQt5.QtCore import pyqtSlot as Slot
from PyQt5 import QtCore
from PyQt5 import QtWidgets
from PyQt5 import QtGui
from PyQt5 import uic
# MPL class
from pyqt5_mpl import mpl_figure
# Select Qt5 user interface.
qtCreatorFile = '<local filepath>'
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
class main(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
# Inititate UI.
QtWidgets.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
# Create data structures.
global a
a = a_dataclass()
# Create MPL Canvases (Layout->CreateMPLCanvas->AddWidget).
# Create class instances for each figure.
a.im1 = mpl_figure()
a.im2 = mpl_figure()
# Create Layouts and add widgets.
a_layout = QtWidgets.QHBoxLayout(self.tab1)
a_layout.addWidget(a.im1.canvas)
a_layout.addWidget(a.im2.canvas)
# Load images.
a.im1.loadimage('image file path',a.pix)
a.im2.loadimage('image file path',a.pix)
# a.pix is dimensions for extent in plot.
if __name__ == "__main__":
# QApp
app = QtWidgets.QApplication(sys.argv)
# QWidget (MainWindow)
window = main()
window.show()
sys.exit(app.exec_())
我的mpl_figure类:
import matplotlib as mpl
mpl.use('Qt5Agg')
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
class mpl_figure:
def __init__(self):
# Set up empty plot variables.
self.image = None
# Create a figure to plot on.
fig = plt.figure()
fig.set_facecolor('black') # This does nothing...
# Create an axis in the figure.
self.ax = fig.add_subplot(111, axisbg='black')
# Create a canvas widget for Qt to use.
self.canvas = FigureCanvas(fig)
# Create cursor widget.
self.cursor = mpl.widgets.Cursor(self.ax)
# Refresh the canvas.
self.canvas.draw()
def loadimage(self,fn,pix):
# Read in image.
self.data = plt.imread(fn)
# Calculate the extent.
self.dims = np.array([0, pix[0]*np.shape(self.data)[1],0,pix[1]*np.shape(self.data)[0]])
# Display the image.
self.image = self.ax.imshow(self.data, cmap='gray', extent=self.dims)
self.ax.set_autoscale_on(False)
# Refresh the canvas.
self.canvas.draw()
# Start Callback ID
self.cid = self.canvas.mpl_connect('button_press_event', self.onclick)
def onclick(self,event):
# Do stuff...
pass
我试图尝试对self.canvas或fig进行更改。在它的样式方面呈现静音,它什么都不做。至于当你输入mpl_widget时更改光标(或隐藏它,它是指针),我只是不确定如何做到这一点。我记得在mpl中看到连接“event_leave_figure”效果的东西,但无法在它和PyQt&lt;之间建立连接 - 这是重要的一点。有什么想法吗?
答案 0 :(得分:1)
感谢您在提问之前提供MINIMAL WORKING example和谷歌搜索。这总是节省了很多时间。
因此,以下代码生成如下所示的图像。
import sys
from PyQt4 import QtGui, QtCore
import matplotlib
import numpy as np
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
class ApplicationWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.setWindowTitle("application main window")
self.main_widget = QtGui.QWidget(self)
self.setCentralWidget(self.main_widget)
l = QtGui.QVBoxLayout(self.main_widget)
self.fig = Figure(figsize=(5,3), dpi=100)
self.ax = self.fig.add_subplot(111)
self.canvas = FigureCanvas(self.fig)
l.addWidget(self.canvas)
t = np.arange(0.0, 3.0, 0.01)
s = np.sin(2*np.pi*t)*np.exp(-t/7/np.pi)
self.ax.plot(t, s, color="#0095a2", lw=3)
#set some alpha
self.fig.patch.set_alpha(0.5)
#set color
self.fig.patch.set_facecolor('#cd0e49')
self.cursor = matplotlib.widgets.Cursor(self.ax)
#set cursor
#self.canvas.setCursor(QtGui.QCursor(QtCore.Qt.BlankCursor))
#since I find Blank cursor very confusing, I will use a better one:
self.canvas.setCursor(QtGui.QCursor( QtCore.Qt.SizeAllCursor))
qApp = QtGui.QApplication(sys.argv)
aw = ApplicationWindow()
aw.show()
sys.exit(qApp.exec_())