gpt4 book ai didi

python - 在运行时验证 MathText 字符串

转载 作者:太空宇宙 更新时间:2023-11-03 14:10:04 24 4
gpt4 key购买 nike

我正在尝试构建一个包含 Matplotlib 嵌入式面板的 GUI。其中一个小部件(QLineEdit)用于图例标签,当人们在其中输入值时,它会在图表中直接更改(使用 self.widget.textChanged)。所有这一部分都工作得很好并且符合预期,除了以下情况:当人们输入 latex 符号并且该符号写得不好时,我得到一个 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 = 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_())

最佳答案

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

为此,我们将相关字符串设置为标签并调用标签的 _get_layout 函数。如果成功,我们就知道绘图已保存,否则我们会捕获错误而不绘制 Canvas 。

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()

完整的示例如下所示。 (请注意,我在这里使用了 Qt4,它应该按原样工作,将每个 4 替换为 5。)

import sys
######Qt5
from PyQt4 import *
from PyQt4.QtCore import *
from PyQt4.QtGui 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):
QWidget.__init__(self)
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(lambda : self.changelabl("x"))
self.xlabl.setText('Xlabel')

self.ylabl = QLineEdit('qwdwed')
grid.addWidget(self.ylabl, 3, 2, 1, 1)
self.ylabl.textChanged.connect(lambda : self.changelabl("y"))
self.ylabl.setText('Ylabel')

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


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()


app = QApplication(sys.argv)
main = Main_window()
sys.exit(app.exec_())

关于python - 在运行时验证 MathText 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48572340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com