我尝试使用matplotlib的交互式绘图功能嵌入pyqt5窗口,使用mpl版本:2.1.1
我在这里提供了最小的UI文件示例:
和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_())
缩放到时间序列中,平移按预期工作,但是当我尝试按下" Home" - 重置原始视图崩溃:
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窗口中单独打开时,它没有任何问题。我已经附上了工作主页按钮的示例,该按钮在na非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()
谢谢!
答案 0 :(得分:1)
简答:删除第self.canvas.draw()
行。
说明:
在绘制包含任何数据之前绘制画布。但由于画布在此时已经配备了工具栏,因此" Home" -state是轴没有数据的状态。
这本身不会成为问题,但是一旦绘制了日期,它就会成为一个问题
空轴的默认初始限制为(0,1)
。所以" Home" -state会引用这些限制,点击按钮后会尝试恢复这些限制。
然而,与此同时,轴被告知要保留日期。日期范围为(1, whatever)
。因此0
在日期轴上没有有效数字。
解决方案非常简单。只需从代码中删除行self.canvas.draw()
即可。它无论如何都不能完成任何必要的任务。