- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试构建一个包含 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/
我试图让 mathtext 正确渲染我的图形轴标签(理想情况下我不想使用 Latex 本身来渲染文本)。然而,这个最小的工作示例会产生如下所示的错误。在我看来,它应该可以工作,也许是没有安装必要的软件
我尝试使用 mathtext 来获取经过特殊渲染的图形标题,但失败了。而不是我的测试标题,它只是打印了一些神秘的字符。我做错了什么? 操作系统:Fedora release 18 (Spherical
我正在尝试构建一个包含 Matplotlib 嵌入式面板的 GUI。其中一个小部件(QLineEdit)用于图例标签,当人们在其中输入值时,它会在图表中直接更改(使用 self.widget.text
当我给 matplotlib 一个注释字符串时,例如 '$\frac{A}{B} = C$' 我指定字体大小为 18,A 和 B 以 12.6 pt 呈现,而 C 以 18 pt 呈现。我希望 A、B
有没有办法在 matplotlib 图形中使用 AMSeuler 字体?我已经尝试过 rc('mathtext', fontset='eulermath'), 但当然不起作用因为该字体在 matplo
所以我一直在玩弄 pyplot pie() 函数,它运行良好,除了当我尝试在楔形标签中使用 mathtext 时(如在 autopct 标签中,我想要数字,一个\pm (+-) 符号和一些相关的数字)
我知道 matplotlib 可以很容易地呈现数学表达式,例如, txt=Text(x,y,r'$\frac{1}{2}') 这将使 x,y 处的分数 1 大于 2。但是,我不想将文本放在 x,y 处
我正在尝试使用 apache-commons-exec 从 java 程序运行 mathtext。问题是当我从 java 程序运行相同的命令时和通过 shell 运行它时,我得到不同的输出。因此,如果
我是一名优秀的程序员,十分优秀!