【发布时间】:2020-08-01 05:45:22
【问题描述】:
我的代码将这段代码绘制在我的主窗体中完美地:
self.graphics_View_widget.clear()
pg.setConfigOption('background', 'k')
h=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
t=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
self.graphics_View_widget.plot(h, t, pen=(0,0,255))
它在指定位置绘制没有任何问题
但是当我想将 x 轴更改为这样的字符串对象时:
self.graphics_View_widget.clear()
h=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
t=['a','b','c','d','e','f','g','h','i','j']
xDict=dict(enumerate(t))
xValue=list(xDict.keys())
self.graphics_View_widget= pg.GraphicsWindow()
bottomAxis = pg.AxisItem(orientation='bottom')
pp=self.graphics_View_widget.addPlot(axisItems={'bottom': bottomAxis},name='h')
xtickts=[xDict.items()]
bottomAxis.setTicks(xtickts)
pp.plot(xValue,h)
现在它在新窗口中绘制此图表(而不是在 qt 设计器中指定位置)
我的错在哪里???
.py 代码
from PyQt5 import QtWidgets, uic,QtCore, QtGui
from PyQt5.QtGui import QColor
import pyqtgraph as pg
import sys
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
#Load the UI Page
uic.loadUi('teeeeest.ui', self)
self.pushButton.clicked.connect(self.show_plot)
def show_plot(self):
############################################ this code draws in the specified location without any problem:
# self.graphics_View_widget.clear()
# pg.setConfigOption('background', 'k')
# h=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# t=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# self.graphics_View_widget.plot(h, t, pen=(0,0,255))
############################################ this code draws a new window (instead of specified location in qt designer)
self.graphics_View_widget.clear()
h=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
t=['a','b','c','d','e','f','g','h','i','j']
xDict=dict(enumerate(t))
xValue=list(xDict.keys())
self.graphics_View_widget= pg.GraphicsWindow()
bottomAxis = pg.AxisItem(orientation='bottom')
pp=self.graphics_View_widget.addPlot(axisItems={'bottom': bottomAxis},name='h')
xtickts=[xDict.items()]
bottomAxis.setTicks(xtickts)
pp.plot(xValue,h)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
【问题讨论】:
-
嗯,这是有道理的:您正在创建一个新的小部件,Qt(或重要的 Python)无法知道您想要替换现有的小部件。只需删除覆盖
self.graphics_View_widget的行。 -
删除
self.graphics_View_widget= pg.GraphicsWindow() -
@eyllanesc 当我删除该行时,错误提示 (NameError: addPlot) 并且代码无法绘制
-
@michaelbiehn 请提供minimal reproducible example(.ui 和 .py)
-
@eyllanesc 我添加了确切的代码并更新了问题。还有什么我应该分享的吗?如果有请告诉我
标签: python python-3.x pyqt5 qt-designer pyqtgraph