我试图在pyside6应用程序中显示matplotlib图,但它不运行,我使用的是macOS ventura。
这是密码
import sys
import matplotlib
matplotlib.use('Qt5Agg')
from PySide6.QtWidgets import QMainWindow, QApplication
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from matplotlib.figure import Figure
class MplCanvas(FigureCanvasQTAgg):
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
super(MplCanvas, self).__init__(fig)
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
# Create the maptlotlib FigureCanvas object,
# which defines a single set of axes as self.axes.
sc = MplCanvas(self, width=5, height=4, dpi=100)
sc.axes.plot([0,1,2,3,4], [10,1,20,3,40])
self.setCentralWidget(sc)
self.show()
app = QApplication(sys.argv)
w = MainWindow()
app.exec()
字符串
我得到的错误
Traceback (most recent call last):
File "/Users/u/Downloads/Function-Plotter-main/app/main.py", line 8, in <module>
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
File "/Users/u/Downloads/Function-Plotter-main/venv/lib/python3.7/site-packages/matplotlib/backends/backend_qt5agg.py", line 7, in <module>
from .backend_qtagg import ( # noqa: F401, E402 # pylint: disable=W0611
File "/Users/u/Downloads/Function-Plotter-main/venv/lib/python3.7/site-packages/matplotlib/backends/backend_qtagg.py", line 12, in <module>
from .backend_qt import (
File "/Users/u/Downloads/Function-Plotter-main/venv/lib/python3.7/site-packages/matplotlib/backends/backend_qt.py", line 76, in <module>
for mod, key in [
File "/Users/u/Downloads/Function-Plotter-main/venv/lib/python3.7/site-packages/matplotlib/backends/backend_qt.py", line 76, in <listcomp>
for mod, key in [
AttributeError: type object 'PySide6.QtCore.Qt' has no attribute 'ControzlModifier'
型
我尝试使用tkagg后端,但没有用,也尝试matplotlib.use('MACOS ')
2条答案
按热度按时间ar7v8xwq1#
代码可以在我的mac上运行,你可以检查包的版本,我的是
字符串
结果:
的数据
nbewdwxp2#
我已经设法解决了这个问题,并通过以下步骤在macOS上让Matplotlib与PySide6一起工作:
1.我安装了
Python 3.7.10
。1.我发现在更新
PySide6 to version 6.4.0 or higher
时,也需要更新Matplotlib to version 3.6.2 or higher
。1.要检查Matplotlib的可用版本,您可以使用以下命令:
字符串
如果您看到列出的最新版本是
3.5.3
,则可能是由于您正在使用的Python版本。4.在我的例子中,我必须通过运行以下命令将PySide6降级到版本6.3.2:型
1.在降级PySide6之后,Matplotlib与PySide6的集成工作得非常好。我希望这个解决方案可以帮助你在macOS上让Matplotlib与PySide6一起工作。