到目前为止,我正在运行plt.hist
函数并将结果保存在图像中,然后在mdiArea
内使用以下代码进行绘图
def test(self):
self.result = QtGui.QMdiSubWindow()
self.result.setObjectName(_fromUtf8("subWindow"))
pixmap = QtGui.QPixmap(_fromUtf8('Result.png'))
graphicsView = QtGui.QGraphicsView(self.result)
self.mdiArea.addSubWindow(self.result)
graphicsView.setGeometry(QtCore.QRect(30, 30, pixmap.width() + 10, pixmap.height() + 10))
graphicsView.setObjectName(_fromUtf8("graphicView"))
scene = QtGui.QGraphicsScene()
scene.addPixmap(pixmap)
scene.setSceneRect(QtCore.QRectF(pixmap.rect()))
scene.update()
graphicsView.setScene(scene)
self.result.resize(pixmap.width() + 60, pixmap.height() + 60)
self.result.setWindowTitle('Results')
self.result.show()
但是当我绘制大数据集时,直方图会变得太小。有什么方法可以在plt.hist
内部直接使用mdiArea
绘制
我想要在窗口内
顺便问一下,有没有更好的方法可以在直方图中显示此数据?
答案 0 :(得分:2)
您必须使用Qt4后端的FigureCanvas创建一个画布(1),在下面的代码中,我显示一个示例:
import numpy as np
from PyQt4 import QtCore, QtGui
from matplotlib.backends.backend_qt4agg import (
FigureCanvas,
NavigationToolbar2QT as NavigationToolbar,
)
from matplotlib.figure import Figure
class ApplicationWindow(QtGui.QMainWindow):
def __init__(self):
super().__init__()
self.mdiArea = QtGui.QMdiArea()
self.setCentralWidget(self.mdiArea)
mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
self._canvas = FigureCanvas(Figure(figsize=(5, 3)))
self._ax = self._canvas.figure.subplots()
n, bins, patches = self._ax.hist(
x, 50, density=1, facecolor="green", alpha=0.75
)
self._ax.set_xlabel("Smarts")
self._ax.set_ylabel("Probability")
self._ax.set_title(r"$\mathrm{Histogram\ of\ IQ:}\ \mu=100,\ \sigma=15$")
self._ax.axis([40, 160, 0, 0.03])
self._ax.grid(True)
widget = QtGui.QMainWindow()
widget.setCentralWidget(self._canvas)
widget.addToolBar(
QtCore.Qt.BottomToolBarArea, NavigationToolbar(self._canvas, self)
)
sub_window = QtGui.QMdiSubWindow()
sub_window.setWidget(widget)
self.mdiArea.addSubWindow(sub_window)
if __name__ == "__main__":
import sys
qapp = QtGui.QApplication(sys.argv)
app = ApplicationWindow()
app.show()
sys.exit(qapp.exec_())