在运行时验证MathText字符串

时间:2018-02-01 21:53:15

标签: python matplotlib latex pyqt5

我正在尝试构建一个包含Matplotlib嵌入式面板的GUI。其中一个小部件(QLineEdit)用于图例标签,当人们在那里输入值时,它会在图形中直接更改(使用self.widget.textChanged)。除了以下情况之外,所有这一部分都很有效,并且正如预期的那样:当人们输入乳胶符号并且写得不好时,我得到一个ValueError,我无法弄清楚如何捕获。

当我输入' $$':

时的典型追溯
Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/matplotlib/mathtext.py", line 2516, in parse
result = self._expression.parseString(s)
File "/usr/lib/python3.6/site-packages/pyparsing.py", line 1632, in parseString
raise exc
File "/usr/lib/python3.6/site-packages/pyparsing.py", line 1622, in parseString
loc, tokens = self._parse( instring, 0 )
File "/usr/lib/python3.6/site-packages/pyparsing.py", line 1529, in _parseCache
value = self._parseNoCache(instring, loc, doActions, callPreParse)
File "/usr/lib/python3.6/site-packages/pyparsing.py", line 1379, in _parseNoCache
loc,tokens = self.parseImpl( instring, preloc, doActions )
File "/usr/lib/python3.6/site-packages/pyparsing.py", line 3717, in parseImpl
return self.expr._parse( instring, loc, doActions, callPreParse=False )
File "/usr/lib/python3.6/site-packages/pyparsing.py", line 1529, in _parseCache
value = self._parseNoCache(instring, loc, doActions, callPreParse)
File "/usr/lib/python3.6/site-packages/pyparsing.py", line 1379, in _parseNoCache
loc,tokens = self.parseImpl( instring, preloc, doActions )
File "/usr/lib/python3.6/site-packages/pyparsing.py", line 3395, in parseImpl
loc, exprtokens = e._parse( instring, loc, doActions )
File "/usr/lib/python3.6/site-packages/pyparsing.py", line 1529, in _parseCache
value = self._parseNoCache(instring, loc, doActions, callPreParse)
File "/usr/lib/python3.6/site-packages/pyparsing.py", line 1383, in _parseNoCache
loc,tokens = self.parseImpl( instring, preloc, doActions )
File "/usr/lib/python3.6/site-packages/pyparsing.py", line 3183, in parseImpl
raise ParseException(instring, loc, self.errmsg, self)
pyparsing.ParseException: Expected end of text (at char 0), (line:1, col:1)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/alien/Dropbox/Vcatpy/src/VcatPy", line 974, in make_scatplot
self.win.draw()
File "/usr/lib/python3.6/site-packages/matplotlib/backends/backend_qt5agg.py", line 133, in draw
super(FigureCanvasQTAggBase, self).draw()
File "/usr/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py", line 430, in draw
self.figure.draw(self.renderer)
File "/usr/lib/python3.6/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/usr/lib/python3.6/site-packages/matplotlib/figure.py", line 1299, in draw
renderer, self, artists, self.suppressComposite)
File "/usr/lib/python3.6/site-packages/matplotlib/image.py", line 138, in _draw_list_compositing_images
a.draw(renderer)
File "/usr/lib/python3.6/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/usr/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 2437, in draw
mimage._draw_list_compositing_images(renderer, self, artists)
File "/usr/lib/python3.6/site-packages/matplotlib/image.py", line 138, in _draw_list_compositing_images
a.draw(renderer)
File "/usr/lib/python3.6/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/usr/lib/python3.6/site-packages/matplotlib/legend.py", line 772, in draw
bbox = self._legend_box.get_window_extent(renderer)
File "/usr/lib/python3.6/site-packages/matplotlib/offsetbox.py", line 263, in get_window_extent
w, h, xd, yd, offsets = self.get_extent_offsets(renderer)
File "/usr/lib/python3.6/site-packages/matplotlib/offsetbox.py", line 385, in get_extent_offsets
for c in self.get_visible_children()]
File "/usr/lib/python3.6/site-packages/matplotlib/offsetbox.py", line 385, in <listcomp>
for c in self.get_visible_children()]
File "/usr/lib/python3.6/site-packages/matplotlib/offsetbox.py", line 256, in get_extent
w, h, xd, yd, offsets = self.get_extent_offsets(renderer)
File "/usr/lib/python3.6/site-packages/matplotlib/offsetbox.py", line 456, in get_extent_offsets
for c in self.get_visible_children()]
File "/usr/lib/python3.6/site-packages/matplotlib/offsetbox.py", line 456, in <listcomp>
for c in self.get_visible_children()]
File "/usr/lib/python3.6/site-packages/matplotlib/offsetbox.py", line 256, in get_extent
w, h, xd, yd, offsets = self.get_extent_offsets(renderer)
File "/usr/lib/python3.6/site-packages/matplotlib/offsetbox.py", line 385, in get_extent_offsets
for c in self.get_visible_children()]
File "/usr/lib/python3.6/site-packages/matplotlib/offsetbox.py", line 385, in <listcomp>
for c in self.get_visible_children()]
File "/usr/lib/python3.6/site-packages/matplotlib/offsetbox.py", line 256, in get_extent
w, h, xd, yd, offsets = self.get_extent_offsets(renderer)
File "/usr/lib/python3.6/site-packages/matplotlib/offsetbox.py", line 456, in get_extent_offsets
for c in self.get_visible_children()]
File "/usr/lib/python3.6/site-packages/matplotlib/offsetbox.py", line 456, in <listcomp>
for c in self.get_visible_children()]
File "/usr/lib/python3.6/site-packages/matplotlib/offsetbox.py", line 829, in get_extent
bbox, info, d = self._text._get_layout(renderer)
File "/usr/lib/python3.6/site-packages/matplotlib/text.py", line 317, in _get_layout
ismath=ismath)
File "/usr/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py", line 231, in get_text_width_height_descent
self.mathtext_parser.parse(s, self.dpi, prop)
File "/usr/lib/python3.6/site-packages/matplotlib/mathtext.py", line 3303, in parse
box = self._parser.parse(s, font_output, fontsize, dpi)
File "/usr/lib/python3.6/site-packages/matplotlib/mathtext.py", line 2522, in parse
six.text_type(err)]))
ValueError: 
$$
^
Expected end of text (at char 0), (line:1, col:1)
Aborted (core dumped)

然后它崩溃了所有的GUI。

导致此错误的命令是:

self.plot.legend(handles, labels) 

handles, labels来自

handles, labels = self.plot.get_legend_handles_labels()`

我试图用一个简单的方法来捕捉错误:

try:
    self.plot.legend(handles, labels)
except ValueError:
    print('ok')

我还确保标签采用原始字符串格式r'my label'

但是我无法让它发挥作用......非常感谢任何帮助。

下面给出了一个示例,不是图例而是带有轴标签:

####Public General Libraries
import sys

######Qt5
from PyQt5 import *
import PyQt5.QtCore as QtCore
import PyQt5.QtGui as QtGui
from PyQt5.QtWidgets import *

###matplotlib
import matplotlib
matplotlib.use('Qt5Agg')
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
from matplotlib.widgets import Cursor

class Main_window(QWidget):

    def __init__(self):
        super().__init__() 
        self.initUI() 

    def initUI(self):

        ### 1 we create the grid
        self.Global = QVBoxLayout(self)
        self.setLayout(self.Global)
        grid= QGridLayout()
        self.Global.addLayout(grid)

        ### a- space for plot
        self.tab = QTabWidget()
        self.figure = Figure()
        self.figure.subplots_adjust(hspace = 0, right=0.95, top=0.94, left=0.15)
        self.win = FigureCanvas(self.figure)
        self.toolbar = NavigationToolbar(self.win, self.win)
        grid.addWidget(self.win,0, 0, 1, 3)
        grid.addWidget(self.toolbar, 1, 0, 1, 3)
        self.plot = self.figure.add_subplot(111)
        self.plot.legend()

        #### c- labels
        self.xlabl = QLineEdit('qwdwed')
        grid.addWidget(self.xlabl, 3, 1, 1, 1)
        self.xlabl.textChanged.connect(self.changexlabl)
        self.xlabl.setText('Xlabel')


        self.ylabl = QLineEdit('qwdwed')
        grid.addWidget(self.ylabl, 3, 2, 1, 1)
        self.ylabl.textChanged.connect(self.changeylabl)
        self.ylabl.setText('Ylabel')


        self.win.draw()
        self.show()


    def changeylabl(self):
        try:
            self.plot.set_ylabel(self.ylabl.text())
        except:
            self.plot.set_ylabel('Y-label')
        self.win.draw()

    def changexlabl(self):
        try:
            self.plot.set_xlabel(self.xlabl.text())
        except:
            self.plot.set_xlabel('X-label')

        self.win.draw()


###and start the tui
app = QApplication(sys.argv)
main = Main_window()
main.setFixedSize(730, 1030)
sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:1)

问题归结为找出给定的字符串是否是有效的数学文本,或者只是一般有效。因为我们不应该将绘图本身放在>中,因为这仍然会尝试渲染图形,并且如果错误中途停止,我们的想法是让matplotlib首先评估字符串,只有成功,才能画出数字。

为此,我们将有问题的字符串设置为标签并调用标签的try/except函数。如果成功,我们知道绘图是保存,否则我们会在不绘制画布的情况下捕获错误。

_get_layout

完整的示例如下所示。 (请注意,我在这里使用了Qt4,它应该按原样运行,用def changelabl(self, which="y"): if which=="y": lab = self.plot.yaxis.label text = self.ylabl.text() else: lab = self.plot.xaxis.label text = self.xlabl.text() try: lab.set_text(text) lab._get_layout(self.figure.canvas.renderer) except: pass else: self.win.draw_idle() 替换每个4。)

5