一名用户在 Windows 系统上开发了一个使用 WXPython 库实现的图形界面,在移动到 Linux 系统后发现,Matplotlib 面板上的图像被截断,无法看到图形,并且无法通过简单的调整尺寸来解决这一问题。
解决方案
-
在 Python 代码中,您需要使用
matplotlib.use('WXAgg')明确指定后端为WXAgg,以解决图形被截断的问题:import matplotlib matplotlib.use('WXAgg') -
在 WXPython 应用程序中,您还需要将
FigureCanvas的类名更正为FigureCanvasWxAgg,以避免出现 "no attribute 'FigureCanvas'" 的错误:from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas -
在创建
MainPanel类时,您需要将FigureCanvas作为其子类,并使用FigureCanvasWxAgg来创建画布:class MainPanel(wx.Panel): def __init__(self, parent): ... self.canvas = FigureCanvasWxAgg(self, -1, self.figure) ...
代码例子
import wx
import sys
import numpy
import matplotlib
import os
import matplotlib.pyplot as plt
matplotlib.use('WXAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import \
FigureCanvasWxAgg as FigCanvas, \
NavigationToolbar2WxAgg as NavigationToolbar
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
ini3=0
fin3=0
pico3=[]
nomeAbre3=''
iniespec=0
intervaloespec=400
class TestFrame(wx.Frame):
def __init__(self,parent,title):
wx.Frame.__init__(self,parent,title=title,size=(2000,800))
self.sp = wx.SplitterWindow(self)
self.p1 = MainPanel(self.sp)
self.p2 = MatplotPanel(self.sp)
self.statusbar = self.CreateStatusBar()
self.box = wx.BoxSizer(wx.VERTICAL)
self.box.Add(self.p1, 1, wx.EXPAND)
self.box.Add(self.p2, 1, wx.EXPAND)
self.p2.draw()
self.create_menu()
self.SetAutoLayout(True)
self.SetSizer(self.box)
self.Layout()
self.statusbar.SetStatusText('Oi')
def create_menu(self):
self.menubar = wx.MenuBar()
menu_file = wx.Menu()
m_expt = menu_file.Append(-1, "&Save plot\tCtrl-S", "Save plot to file")
menu_file.AppendSeparator()
m_exit = menu_file.Append(-1, "E&xit\tCtrl-X", "Exit")
menu_help = wx.Menu()
m_about = menu_help.Append(-1, "&About\tF1", "About the demo")
self.menubar.Append(menu_file, "&File")
self.menubar.Append(menu_help, "&Help")
self.SetMenuBar(self.menubar)
class MainPanel(wx.Panel):
def __init__(self,parent):
""" Creates the main panel with all the controls on it:
* mpl canvas
* mpl navigation toolbar
* Control panel for interaction
"""
self.nomeAbre = 'Spectrum'
self.nomeFundo = 'Spectrum2'
self.tipoDoAjuste = '1'
self.grauDoFundo = '2'
self.posicaoDosPicos = ''
self.regiaoDoAjusteI = ''
self.regiaoDoAjusteF = ''
wx.Panel.__init__(self, parent)
self.dpi = 100
self.fig = Figure((10.0, 6.0), dpi=self.dpi)
self.abreArquivo = wx.Button(self, -1, "Open:", (10,10))
self.Bind(wx.EVT_BUTTON, self.openFile, self.abreArquivo)
self.editname = wx.TextCtrl(self, value=self.nomeAbre, pos=(100, 10), size=(200,-1))
self.AbreArquivoFundo = wx.Button(self, -1, "Fundo:", (10,40))
self.editname2 = wx.TextCtrl(self, value=self.nomeFundo, pos=(100, 40), size=(200,-1))
self.Bind(wx.EVT_BUTTON, self.openFile2, self.AbreArquivoFundo)
self.ajusteTipo = wx.StaticText(self, -1, "A:", pos=(20, 80))
self.ajusteTipoCaixa = wx.TextCtrl(self, value=self.tipoDoAjuste, pos=(100, 78), size=(25,-1))
self.fundoGrau = wx.StaticText(self, -1, "B parameter:", pos=(140, 80))
self.fundoGrauCaixa = wx.TextCtrl(self, value=self.grauDoFundo, pos=(220, 78), size=(25,-1))
self.posicaoPicos = wx.StaticText(self, -1, "Posicao dos Picos (max. 10 valores):", pos=(20, 120))
self.posicaoPicosCaixa = wx.TextCtrl(self, value=self.posicaoDosPicos, pos=(245, 118), size=(350,-1))
self.regiaoAjuste = wx.StaticText(self, -1, "Regiao do Ajuste:", pos=(20, 160))
self.regiaoAjusteI = wx.TextCtrl(self, value='', pos=(140, 158), size=(60,-1))
self.regiaoAjusteF = wx.TextCtrl(self, value='', pos=(220, 158), size=(60,-1))
self.inicioEspectroTexto = wx.StaticText(self, -1, "Start:", pos=(250, 302))
self.inicioEspectro = wx.TextCtrl(self, -1, "", pos=(300, 300))
self.Bind(wx.EVT_TEXT,self.funcInicioEspectro,self.inicioEspectro)
self.intervaloEspectroTexto = wx.StaticText(self, -1, "Interval:", pos=(420, 302))
self.intervaloEspectro = wx.TextCtrl(self, -1, "400", pos=(490, 300),size=(50,-1))
self.Bind(wx.EVT_TEXT,self.funcintervaloEspectro,self.intervaloEspectro)
self.idf = wx.Button(self, -1, "IDF", (100,300))
self.Bind(wx.EVT_BUTTON,self.criaIDF,self.idf)
global ini3
global fin3
global pico3
global iniespec
global intervaloespec
self.Bind(wx.EVT_MOTION,lambda event: self.OnMove(event,ini3,1))
self.Bind(wx.EVT_MOTION,lambda event: self.OnMove(event,fin3,2))
self.Bind(wx.EVT_MOTION,lambda event: self.OnMove(event,pico3,3))
self.Bind(wx.EVT_MOTION,lambda event: self.OnMove(event,iniespec,4))
self.Bind(wx.EVT_MOTION,lambda event: self.OnMove(event,intervaloespec,5))
def criaIDF(self, event):
a1 = nomeAbre3
a2 = str(len(pico3))+','+str(ini3)+','+str(fin3)+',,'
for j in pico3:
a2 += str(j) + ','
a2 += ','
print(a1)
print(a2)
def funcInicioEspectro(self,event):
global iniespec