2016-09-30 74 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