2016-09-30 51 views
1

所以我一直在做的很有研究中的PyQt和MPL,和我仍然有一些问题,我不能让过去。定制MPL在PyQt的

第一个是这个:你如何改变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或图。在其造型方面被渲染为静音,并且它什么都不做。至于改变光标(或隐藏它,它是指针),当你输入一个mpl_widget我只是不知道如何做到这一点。我记得在mpl中看到了一些连接“event_leave_figure”的效果,但无法连接它和PyQt之间的连接 - 这是重要的一点。有任何想法吗?

+0

张贴一些代码,请。 – HYRY

+0

@HYRY我已经添加了我的代码jist(拿出了很多不必要的东西)。让我知道如果你需要更多,我希望它有帮助。 – NineTails

+0

'mpl_figure'不是QtWidget的子类,我认为'mpl_layout.addWidget(mpl_widget)'会引发异常。 – HYRY

回答

0

感谢提供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_()) 

enter image description here