使用列表在matplotlib中使用for循环绘图

时间:2017-03-22 12:52:33

标签: python matplotlib

form_class = uic.loadUiType("GUI.ui")[0]  # Load the UI

class MainWindowClass(QtGui.QMainWindow, form_class):
  def __init__(self, target, Afb, np, conversion, parent=None):
    QtGui.QMainWindow.__init__(self, parent)
    self.setupUi(self)
    if self.RadioButton.isChecked():
        Ids, Ugs = unipolar_steuerkennlinie(self.target, self.Afb, self.np, self.conversion)

  def plot_matplotlibwidget(self, *args):
    for x in range(0, 40):
         self.matplotlibwidget.axes.plot([args[0][x]], [[args[1][x]]])
    self.matplotlibwidget.axes.figure.canvas.draw_idle()  

嗨,我有这个功能,我真的不知道如何正确地绘图。现在它什么也没有显示,但确实改变了规模。

1 个答案:

答案 0 :(得分:0)

由于很难完全复制你的代码和错误,我已经创建了一个简单的最小GUI,它有一个MPL小部件并绘制一些随机行。

from PyQt4 import QtGui, QtCore
import sys
import functools
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
import numpy as np
from matplotlib.figure import Figure

class test(QtGui.QWidget):

    def __init__(self,parent=None):

        self.widget=QtGui.QWidget.__init__(self, parent)

        # Button to plot
        self.btnPlot = QtGui.QPushButton('Plot')
        self.btnPlot.connect(self.btnPlot, QtCore.SIGNAL('clicked()'), self.btnPlotPressed)

        # Line edit for number of lines
        self.qleN = QtGui.QLineEdit(str(0))

        # Create canvas
        self.setupMPLWidget()

        # Layout
        self.hbox = QtGui.QHBoxLayout()
        self.hbox.addWidget(self.btnPlot)
        self.hbox.addWidget(self.qleN)
        self.hbox.addWidget(self.canvas)

        self.setLayout(self.hbox)   


        self.show()

    def btnPlotPressed(self):

        """Plots a few lines."""

        # Get number of buttons to add
        n=int(self.qleN.text())

        # Generate some data
        xs,ys=self.randData(n)

        # Plot
        self.plot_matplotlibwidget(xs,ys)

    def randData(self,n):

        """Creates n random data sets."""

        ys=[]
        xs=[]
        for i in range(n):
            xs.append(np.arange(n-i))
            ys.append(np.random.random(size=xs[-1].shape))

        return xs,ys

    def plot_matplotlibwidget(self,*args):

        """Plots list of datasets."""

        for x in range(0, len(args[0])):
            self.ax.plot(args[0][x], args[1][x])
        self.ax.figure.canvas.draw_idle()  

    def setupMPLWidget(self):

        """Sets up a MPL figure to draw on."""

        # Create parenting widget
        self.plotFrame = QtGui.QWidget()
        self.plotFrame.setMaximumWidth(1)

        # Create Figure
        self.fig = Figure( dpi=100)
        #self.fig.set_size_inches(100,100,forward=True)
        self.canvas = FigureCanvas(self.fig)
        self.canvas.setParent(self.plotFrame)

        self.ax = self.fig.add_subplot(111)


def main():

    #Creating application   
    app = QtGui.QApplication(sys.argv)

    main_win = test()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

它有一个小QLE,您可以在其中指定要绘制的行数。我希望这有帮助。

相关问题