【发布时间】:2018-11-27 01:06:15
【问题描述】:
我想在小部件 (QWidget) 中显示 pyplot 图像,我把它放在 QtDesigner 中设计的 gui 中:
当我按下 Çiz 按钮时,我想显示我可以使用该代码在 python 中创建的图像:
points = np.array([(1, 1), (2, 4), (3, 1), (9, 3)])
x = points[:,0]
y = points[:,1]
# calculate polynomial
z = np.polyfit(x, y, 2)
f = np.poly1d(z)
x_fit = np.linspace(min(x), max(x), 10000)
y_fit = [f(_x) for _x in x_fit]
plt.plot(x, y)
plt.plot(x_fit, y_fit)
plt.show()
编辑
我根据answer做了一些修改,但是又出现了新的问题。
在我推广之后:
我在下面重新排列我的代码:
# calculate polynomial and r
self.x_fit = np.linspace(min(self.itemX), max(self.itemY), 10000)
self.y_fit = [f(_x) for _x in self.x_fit]
self._dlg.plotwidget.plot(self.itemX, self.itemY)
self._dlg.plotwidget.plot(self.x_fit, self.y_fit)
self.itemX 是列表中的 x 个值。
self.itemY 是列表中的 y 值。
self._dlg 是您看到的主窗口。
当我尝试打开该窗口时,我收到以下错误消息:
【问题讨论】:
标签: python python-3.x matplotlib pyqt pyqt5