如何抑制/忽略(/修复)matplotlib Axes3D ValueError?

时间:2019-02-18 16:10:19

标签: python matplotlib pyqt suppress-warnings mplot3d

根据要求,这是最小的工作示例(如下);非常抱歉,但这似乎是一个需要开销结构的问题(特别是同时在两个单独的选项卡上同时存在2d和3d轴,但单独使用3d或2d时并没有):

/data/ees/anaconda/envs/manifold/lib/python2.7/site-packages/mpl_toolkits/mplot3d/axis3d.py:270: RuntimeWarning: invalid value encountered in true_divide
  ax_scale = self.axes.bbox.size / self.figure.bbox.size
Traceback (most recent call last):
  File "/data/ees/anaconda/envs/manifold/lib/python2.7/site-packages/matplotlib/backends/backend_qt5agg.py", line 176, in __draw_idle_agg
    FigureCanvasAgg.draw(self)
  File "/data/ees/anaconda/envs/manifold/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 474, in draw
    self.figure.draw(self.renderer)
  File "/data/ees/anaconda/envs/manifold/lib/python2.7/site-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/data/ees/anaconda/envs/manifold/lib/python2.7/site-packages/matplotlib/figure.py", line 1159, in draw
    func(*args)
  File "/data/ees/anaconda/envs/manifold/lib/python2.7/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 290, in draw
    ax.draw(renderer)
  File "/data/ees/anaconda/envs/manifold/lib/python2.7/site-packages/mpl_toolkits/mplot3d/axis3d.py", line 288, in draw
    self.label.draw(renderer)
  File "/data/ees/anaconda/envs/manifold/lib/python2.7/site-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/data/ees/anaconda/envs/manifold/lib/python2.7/site-packages/matplotlib/text.py", line 757, in draw
    raise ValueError("posx and posy should be finite values")
ValueError: posx and posy should be finite values

该图在linux和macOS上运行良好(在macOS上没有警告),但是在linux上,每次在GUI上显示该图时,都会收到这段可怕的错误段落(最终会污染终端):

{{1}}

我想抑制此错误或对其进行修复;任何建议将不胜感激。

1 个答案:

答案 0 :(得分:0)

更新:这里的解决方案是简单地删除工具栏-显然在2d和3d工具栏同时处于活动状态时会发生一些背景竞争(但对于2d和2d工具栏则不会) ... 奇怪的。如果有人遇到类似的情况而这种解决方法可能会有所帮助,我将在此下面给出其他答复。


以下是此问题的解决方法;请查看注释NEW,了解修复所需的所有内容。基本上,当同时需要matplotlib的2d和3d轴时(尽管在两个单独的选项卡上),会发生此错误。现在,单击选项卡即可实时构建每个图。

import sys, os
pyDir = os.path.dirname(os.path.abspath(__file__)) #python file location

from pyface.qt import QtGui, QtCore
os.environ['ETS_TOOLKIT'] = 'qt4'
import sip
sip.setapi('QString', 2)
import matplotlib
matplotlib.use('Agg')
#matplotlib.use('Qt4Agg')
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
from matplotlib.widgets import Slider, Button
import mpl_toolkits.axes_grid1
import matplotlib.path as pltPath
import matplotlib.image as mpimg
from matplotlib.ticker import MaxNLocator
from matplotlib import rc
from mpl_toolkits.mplot3d import Axes3D


class P4(QtGui.QWidget):

    def __init__(self, parent=None):
        super(P4, self).__init__(parent)
        P4.layout = QtGui.QGridLayout(self)
        P4.layout.setContentsMargins(20,20,20,20)
        P4.layout.setSpacing(10)

        P4.button_pic1 = QtGui.QPushButton('View Plots', self)
        P4.button_pic1.clicked.connect(self.RC_vid1)
        P4.layout.addWidget(P4.button_pic1, 2, 8, 1, 1)
        P4.button_pic1.show()

    def RC_vid1(self):

        global PrD_window
        try:
            PrD_window.close()
        except:
            pass

        PrD_window = PrD_Viz()
        PrD_window.show()

################################################################################

class PrD_Viz(QtGui.QMainWindow):

    currentTab = 0 #NEW

    def __init__(self):
        super(PrD_Viz, self).__init__()
        self.left = 10
        self.top = 10
        self.initUI()

    def initUI(self):
        vid_tab2 = ManifoldCanvas(self)
        vid_tab3 = PsiCanvas(self)
        global vid_tabs
        vid_tabs = QtGui.QTabWidget(self)
        vid_tabs.addTab(vid_tab2, 'Plot 1')
        vid_tabs.addTab(vid_tab3, 'Plot 2')


        style = """QTabWidget::tab-bar{
                alignment: center;
                }"""
        self.setStyleSheet(style)
        self.setCentralWidget(vid_tabs)
        self.setWindowModality(QtCore.Qt.ApplicationModal) #freezes out parent window

        if PrD_Viz.currentTab == 1: #NEW
            vid_tabs.setCurrentIndex(1) #NEW
        #NEW:
        vid_tabs.currentChanged.connect(self.onTabChange) #signal for tabs changed via direct click

        self.show()
    #NEW:
    def onTabChange(self, i):
        if i == 0:
            PrD_Viz.currentTab = 0
            self.initUI()
        elif i == 1:
            PrD_Viz.currentTab = 1
            self.initUI()


class ManifoldCanvas(QtGui.QDialog):
    pts_orig = []

    def __init__(self, parent=None):
        super(ManifoldCanvas, self).__init__(parent)

        if PrD_Viz.currentTab == 0: #NEW
            self.figure = Figure(dpi=200)
            self.canvas = FigureCanvas(self.figure)
            self.toolbar = NavigationToolbar(self.canvas, self)
            self.ax = self.figure.add_subplot(111)

            x=[1,5,9,8]
            y=[2,6,10,11]

            ManifoldCanvas.pts_orig = zip(x,y)
            for i in ManifoldCanvas.pts_orig:
                x,y = i
                self.ax.scatter(x,y,s=1,c='#1f77b4')

            self.canvas.draw_idle()

            layout = QtGui.QGridLayout()
            layout.setSizeConstraint(QtGui.QLayout.SetMinimumSize)
            layout.addWidget(self.toolbar, 0,0,1,5)
            layout.addWidget(self.canvas, 1,0,1,5)

            self.setLayout(layout)

class PsiCanvas(QtGui.QDialog):

    def __init__(self, parent=None):
        super(PsiCanvas, self).__init__(parent)

        if PrD_Viz.currentTab == 1: #NEW

            self.psi1 = [10,11,12]
            self.psi2 = [24,19,18]
            self.psi3 = [99,90, 105]

            # create canvas and plot data:
            self.figure = Figure(dpi=200)
            self.canvas = FigureCanvas(self.figure)
            self.toolbar = NavigationToolbar(self.canvas, self)
            self.ax = Axes3D(self.figure)
            self.ax.mouse_init()

            self.ax.scatter(self.psi1, self.psi2, self.psi3, label='psi_dif', linewidths= .5, edgecolors='k', c='#d62728') #C3

            self.canvas.draw_idle() #refresh canvas

            layout = QtGui.QGridLayout()
            layout.setSizeConstraint(QtGui.QLayout.SetMinimumSize)
            layout.addWidget(self.toolbar, 0,0,1,4)
            layout.addWidget(self.canvas, 1,0,4,4)

            self.setLayout(layout)


################################################################################
# overhead GUI control: 

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        tab4 = P4(self)

        global tabs
        tabs = QtGui.QTabWidget(self)
        tabs.resize(250,150)

        tabs.addTab(tab4, 'Eigenfunctions')

        self.setCentralWidget(tabs)
        self.show()


if __name__ == '__main__':

    app = QtGui.QApplication.instance()
    if app is None:
        app = QtGui.QApplication([])

    w = MainWindow()
    sys.exit(app.exec_())