gpt4 book ai didi

python - pyqt5窗口中嵌入的Matplotlib导航工具栏-重置原始 View 崩溃

转载 作者:太空宇宙 更新时间:2023-11-04 02:34:30 25 4
gpt4 key购买 nike

我正在尝试使用嵌入到 pyqt5 窗口的 matplotlib 的交互式绘图功能,mpl 版本:2.1.1

我在这里提供了带有 UI 文件的最小示例:

https://ufile.io/8irs7

和python代码:

import matplotlib
matplotlib.use("Qt5Agg")
import sys

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure

import pandas as pd
import numpy as np

from PyQt5 import QtWidgets, uic # works for pyqt5
from PyQt5.Qt import QVBoxLayout


# import numpy as np
qtCreatorFile = "window.ui" # Enter file here.
# qtCreatorFile = "pssga.ui"


Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)

class PlotSeries(QtWidgets.QMainWindow, Ui_MainWindow):

def __init__(self):
QtWidgets.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)

#create figure
self.fig=Figure()

self.axes = self.fig.add_subplot(111)
self.axes.grid()



self.canvas=FigureCanvas(self.fig)
#add plot toolbar from matplotlib
self.toolbar = NavigationToolbar(self.canvas, self)

#create new layout
layout = QVBoxLayout()

layout.addWidget(self.canvas)
layout.addWidget(self.toolbar)

#add layout to qwidget
self.plotlayout=self.widgetplot.setLayout(layout)

#draw the canvas !
self.canvas.draw()
#now let's call widget
self.plot_basegraph()


def plot_basegraph(self):
#create simple time series
for i in range(0,5):
ts=pd.Series(np.random.randn(2000), index=pd.date_range('1/1/2015',freq='h', periods=2000),name="series "+str(i))
if i==0:
self.stackTSPD=ts.to_frame()
else:
self.stackTSPD=self.stackTSPD.join(ts.to_frame(),how='inner')
print(matplotlib.__version__)

self.axes.clear()
self.axes.grid()

for i in self.stackTSPD.columns.values:
t=self.stackTSPD.index.to_pydatetime()

y=self.stackTSPD[i].values
self.axes.plot(t,y, label=i)


if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = PlotSeries()
window.show()
sys.exit(app.exec_())

放大时间序列,平移按预期工作,但当我尝试按“主页”时 - 重置原始 View 它崩溃了:

Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_qt5agg.py", line 155, in __draw_idle_agg
self.draw()
File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_qt5agg.py", line 127, in draw
super(FigureCanvasQTAggBase, self).draw()
File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_agg.py", line 430, in draw
self.figure.draw(self.renderer)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/figure.py", line 1299, in draw
renderer, self, artists, self.suppressComposite)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/image.py", line 138, in _draw_list_compositing_images
a.draw(renderer)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/axes/_base.py", line 2437, in draw
mimage._draw_list_compositing_images(renderer, self, artists)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/image.py", line 138, in _draw_list_compositing_images
a.draw(renderer)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/axis.py", line 1133, in draw
ticks_to_draw = self._update_ticks(renderer)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/axis.py", line 974, in _update_ticks
tick_tups = list(self.iter_ticks())
File "/usr/local/lib/python3.5/dist-packages/matplotlib/axis.py", line 917, in iter_ticks
majorLocs = self.major.locator()
File "/usr/local/lib/python3.5/dist-packages/matplotlib/dates.py", line 1061, in __call__
self.refresh()
File "/usr/local/lib/python3.5/dist-packages/matplotlib/dates.py", line 1081, in refresh
dmin, dmax = self.viewlim_to_dt()
File "/usr/local/lib/python3.5/dist-packages/matplotlib/dates.py", line 839, in viewlim_to_dt
return num2date(vmin, self.tz), num2date(vmax, self.tz)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/dates.py", line 445, in num2date
return _from_ordinalf(x, tz)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/dates.py", line 260, in _from_ordinalf
dt = datetime.datetime.fromordinal(ix).replace(tzinfo=UTC)
ValueError: ordinal must be >= 1

当我试图将这个时间序列图嵌入到 pyqt5 表单中时,这种行为就开始了。当绘图在新的 GTK 窗口中单独打开时,它可以正常工作。我附上了在非 pyqt 窗口中打开的工作主页按钮的示例...

'''
Created on 16. jan. 2018

@author: gregor
'''

import matplotlib as mpl
import matplotlib.pyplot as plt
# 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

import pandas as pd
import numpy as np





class PlotSeries():

def __init__(self):


self.fig,self.axes = plt.subplots()

self.axes.grid()



# self.canvas=FigureCanvas(self.fig)
#add plot toolbar from matplotlib
# self.toolbar = NavigationToolbar(self.canvas, self)

#create new layout
# layout = QVBoxLayout()
#
# layout.addWidget(self.canvas)
# layout.addWidget(self.toolbar)
#
# #add layout to qwidget
# self.plotlayout=self.widgetplot.setLayout(layout)
#
# #draw the canvas !
# self.canvas.draw()
#now let's call widget
self.plot_basegraph()


def plot_basegraph(self):
#create simple time series
for i in range(0,5):
ts=pd.Series(np.random.randn(2000), index=pd.date_range('1/1/2015',freq='h', periods=2000),name="series "+str(i))
if i==0:
self.stackTSPD=ts.to_frame()
else:
self.stackTSPD=self.stackTSPD.join(ts.to_frame(),how='inner')
print(mpl.__version__)

self.axes.clear()
self.axes.grid()

for i in self.stackTSPD.columns.values:
t=self.stackTSPD.index.to_pydatetime()

y=self.stackTSPD[i].values
self.axes.plot_date(t,y, label=i)
plt.show()



if __name__ == "__main__":
PlotSeries()

谢谢!

最佳答案

简答:删除行 self.canvas.draw()

解释:
在它包含任何数据之前绘制 Canvas 。但由于那时 Canvas 已经配备了工具栏,因此“主页”状态是轴没有数据的状态。

这本身不是问题,但一旦绘制出日期,它就会成为一个问题。
空轴的默认初始限制为 (0,1)。所以“主页”状态将引用这些限制,点击按钮将尝试恢复这些限制。
然而与此同时,轴已被告知要保存日期。日期范围从 (1,随便)。因此 0 不是日期轴上的有效数字。

解决方案就很简单了。只需从代码中删除 self.canvas.draw() 行。它无论如何都不会完成任何必需的任务。

关于python - pyqt5窗口中嵌入的Matplotlib导航工具栏-重置原始 View 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48264553/

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