我在pyqtgraph中创建了以下图形。它可以正常运行并按应有的方式显示。
import numpy as np
import pyqtgraph as pg
import pyqtgraph.exporters
from pyqtgraph.Qt import QtCore, QtGui
import csv
import itertools
from datetime import datetime as dt
app = QtGui.QApplication([])
class TimeAxisItem(pg.AxisItem):
def tickStrings(self, values, scale, spacing):
return [dt.fromtimestamp(value) for value in values]
x_vals = [dt(2018, 7, 1, 13, 6, 38), dt(2018, 7, 1, 12, 46, 38), dt(2018, 7, 1, 12, 36, 38), dt(2018, 7, 1, 12, 26, 38), dt(2018, 7, 1, 12, 16, 38), dt(2018, 7, 1, 12, 6, 38), dt(2018, 7, 1, 11, 56, 38), dt(2018, 7, 1, 11, 46, 38), dt(2018, 7, 1, 11, 36, 38), dt(2018, 7, 1, 11, 26, 38), dt(2018, 7, 1, 10, 56, 38), dt(2018, 7, 1, 10, 46, 38), dt(2018, 7, 1, 10, 36, 38)]
y_vals = [23.5, 20.8, 28.0, 28.1, 28.0, 27.8, 27.3, 27.2, 25.7, 24.7, 25.0, 25.0, 24.9]
date_axis = TimeAxisItem(orientation='bottom')
plt = pg.PlotWidget(axisItems = {'bottom': date_axis})
# set properties
plt.setLabel('left', 'Temperature', units='C')
plt.setWindowTitle('Temperature over time')
plt.plot(x=[x.timestamp() for x in x_vals], y=y_vals, symbol=None, pen='r', symbolPen='r')
plt.show()
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
但是,当我尝试从某个函数运行它时,它会闪烁然后消失,但pyqt仍在运行。
import numpy as np
import pyqtgraph as pg
import pyqtgraph.exporters
from pyqtgraph.Qt import QtCore, QtGui
import csv
import itertools
from datetime import datetime as dt
app = QtGui.QApplication([])
class TimeAxisItem(pg.AxisItem):
def tickStrings(self, values, scale, spacing):
return [dt.fromtimestamp(value) for value in values]
def Gshow():
# app = QtGui.QApplication([])
x_vals = [dt(2018, 7, 1, 13, 6, 38), dt(2018, 7, 1, 12, 46, 38), dt(2018, 7, 1, 12, 36, 38), dt(2018, 7, 1, 12, 26, 38), dt(2018, 7, 1, 12, 16, 38), dt(2018, 7, 1, 12, 6, 38), dt(2018, 7, 1, 11, 56, 38), dt(2018, 7, 1, 11, 46, 38), dt(2018, 7, 1, 11, 36, 38), dt(2018, 7, 1, 11, 26, 38), dt(2018, 7, 1, 10, 56, 38), dt(2018, 7, 1, 10, 46, 38), dt(2018, 7, 1, 10, 36, 38)]
y_vals = [23.5, 20.8, 28.0, 28.1, 28.0, 27.8, 27.3, 27.2, 25.7, 24.7, 25.0, 25.0, 24.9]
date_axis = TimeAxisItem(orientation='bottom')
plt = pg.PlotWidget(axisItems = {'bottom': date_axis})
# set properties
plt.setLabel('left', 'Temperature', units='C')
plt.setWindowTitle('Temperature over time')
plt.plot(x=[x.timestamp() for x in x_vals], y=y_vals, symbol=None, pen='r', symbolPen='r')
plt.show()
Gshow()
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
如何在不崩溃的情况下从函数运行图形?
答案 0 :(得分:1)
问题真的很简单,在函数内创建的变量仅存在,直到函数完成执行为止,在您的情况下,plt被未显示的内容消除了,解决方案是增加该数据的范围,一种形式这样做是将其返回并传递给范围更大的变量。
import numpy as np
import pyqtgraph as pg
import pyqtgraph.exporters
from pyqtgraph.Qt import QtCore, QtGui
import csv
import itertools
from datetime import datetime as dt
app = QtGui.QApplication([])
class TimeAxisItem(pg.AxisItem):
def tickStrings(self, values, scale, spacing):
return [dt.fromtimestamp(value) for value in values]
def Gshow():
# app = QtGui.QApplication([])
x_vals = [dt(2018, 7, 1, 13, 6, 38), dt(2018, 7, 1, 12, 46, 38), dt(2018, 7, 1, 12, 36, 38), dt(2018, 7, 1, 12, 26, 38), dt(2018, 7, 1, 12, 16, 38), dt(2018, 7, 1, 12, 6, 38), dt(2018, 7, 1, 11, 56, 38), dt(2018, 7, 1, 11, 46, 38), dt(2018, 7, 1, 11, 36, 38), dt(2018, 7, 1, 11, 26, 38), dt(2018, 7, 1, 10, 56, 38), dt(2018, 7, 1, 10, 46, 38), dt(2018, 7, 1, 10, 36, 38)]
y_vals = [23.5, 20.8, 28.0, 28.1, 28.0, 27.8, 27.3, 27.2, 25.7, 24.7, 25.0, 25.0, 24.9]
date_axis = TimeAxisItem(orientation='bottom')
plt = pg.PlotWidget(axisItems = {'bottom': date_axis})
# set properties
plt.setLabel('left', 'Temperature', units='C')
plt.setWindowTitle('Temperature over time')
plt.plot(x=[x.timestamp() for x in x_vals], y=y_vals, symbol=None, pen='r', symbolPen='r')
plt.show()
return plt # <----
var = Gshow() # <----
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
答案 1 :(得分:0)
在main()内调用Gshow()