Python:图形未被绘制

时间:2016-01-05 18:43:57

标签: python matplotlib

我的问题是我正在尝试使用matplotlib.pyplot创建一个图形来绘制这个最大偏转的最大偏转和位置,由另一个函数计算,用于列出一个人物列表。包含创建加载元组以输入函数所需信息的对象。

我目前的代码如下所示。

第1部分beamModel.py(工作正常):

import scipy.misc
import scipy.optimize
import matplotlib.pyplot
import numpy

class beam(object):
    '''This class is models the deflection of a simply supported beam under
    multiple point loads, following Euler-Bernoulli theory and the principle of
    superposition
    '''


    def __init__(self, E, I, L):
        '''The class costructor
        '''
        self.E = E  # Young's modulus of the beam in N/m^2
        self.I = I  # Second moment of area of the beam in m^4
        self.L = L  # Length of the beam in m
        self.Loads = [(0.0, 0.0)]  # the list of loads applied to the beam

    def setLoads(self, Loads):
        '''This function allows multiple point loads to be applied to the beam
        using a list of tuples of the form (load, position)
        '''
        self.Loads = Loads

    def beamDeflection(self, Load, x):
        """Calculate the deflection at point x due to application of single
        load
        """
        Force, distanceA = Load  #P1 = Force , a = distanceA
        E = self.E
        I = self.I
        L = self.L
        distanceB = L - distanceA
        i = (Force*distanceB)/(6*L*E*I)
        j = ((L/distanceB)*(x-distanceA)**3 - x**3 + (L**2 - distanceB**2)*x)
        k = (Force*distanceB*x)/(6*L*E*I)        
        l = (L**2 - x**2 - distanceB**2)
        if x > distanceA:
            return i*j
        else:
            return k*l


    def getTotalDeflection(self, x):
        """Calculate total deflection of beam due to multiple loads
        """
        #return sum(self.beamDeflection(loadall, x) for loadall in self.Loads)
        return sum(self.beamDeflection(load, x) for load in self.Loads)


    def getMaxDeflection(self):
        """Return a two element tuple containing maximum value of deflection
        and position of maximum deflection
        """
        def D1(x): return self.getTotalDeflection(x)
        D2 = scipy.optimize.fmin(lambda x: -D1(x), 0, full_output = 1, disp = 0)
        return D2[1], D2[0][0]

第2部分personModel.py(已给出且不应更改):

class person(object):
    """This class models the displacement of a person's load as they run at 
    'speed' in one dimension. It assumes that the load is always concentrated 
    in a single point and that the displacement of that point is less than or 
    equal to the displacement of the person's centre of mass. Also the 
    displacement of the load will always be a multiple of 'gait'.
    """
    def __init__(self, arrivalTime, weight, gait, speed):
        """This constructor function defines the person's weight, gait and 
        running speed as well as the time that they arrive at the position of 
        zero displacement.
        """
        self.weight = weight
        self.gait = gait
        self.speed = speed
        self.arrivalTime = arrivalTime

    def loadDisplacement(self, time):
        """This function returns the load and displacement of the person's 
        weight at time 'time', in a tuple: (load,displacement).
        """
        dTime = time - self.arrivalTime
        if dTime < 0 :
            return (0.0,0.0)
        else:
            displacement = self.speed * dTime
            steps = int(displacement/self.gait)
            stepDisplacement = steps*self.gait

            return (9.81*self.weight, stepDisplacement)

第3部分beamSimulation.py(这给我提出了问题):

import personModel
import beamModel
import numpy
import matplotlib.pyplot

beam = beamModel.beam

def createPersonList(fileName):
    """Function will go through each line of file and
    create a person object using the data provided in
    the line and add it to a list
    """
    theFile = open(fileName)
    next(theFile)
    for line in theFile:
        aList = line.split(',')
        bList = map(lambda s: s.strip('\n'), aList)
        cList = [float(i) for i in bList]
        return cList


def simulateBeamRun(personList, beam, times):
    """Takes a list of times covering the duration of
    the simulation (0-35 s), the list of person
    objects and a beam object to simulate a beam run
    """
    dList = []
    for time in times:
        eList = []
        personList = []
        for person in personList:
            loadTuples = personModel.person.loadDisplacement(time)
            if beamModel.beam.L > loadTuples[1] > 0:
                eList.append(loadTuples)
            else:
                return None
        beam.setLoads(eList)
        dList.append(beam.getMaxDeflection())
    x = times
    y = []
    z = []
    for i in dList:
        y.append(i[0] * 10**3)
        z.append(i[1])
    matplotlib.pyplot.figure(0)
    matplotlib.pyplot.xlabel("Time (s)")
    matplotlib.pyplot.plot(x, y, 'r', label = "Maximum deflection (mm)")
    matplotlib.pyplot.plot(x, z, 'b', label = "Position of maximum deflection (m)")
    matplotlib.pyplot.show

函数simulateBeamRun的示例输入是:

>>> ps = createPersonList('personData.csv')
>>> b = beamModel.beam(8.0E9, 1.333E-4, 5.0)
>>> ts = numpy.linspace(0, 35, 500)
>>> simulateBeamRun(ps, b, ts)

但是,当我尝试运行该函数时,我给出的图形只是x轴(时间轴)上的一条直线,因此没有绘制maxDeflection的值和maxDeflection的位置。帮助?!

编辑:原因是dList返回的元组列表都等于(0.0,0.0)。我不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:1)

您正在引用而不是相应的实例:例如,beamModel.beam直接在您应该仅引用beam的位置。< / p>

  • beamSimulation.py的顶部,移除beam = beamModel.beam
  • simulateBeamRun中,将loadTuples = personModel.person.loadDisplacement(time)更改为loadTuples = person.loadDisplacement(time)
  • simulateBeamRun中,将if beamModel.beam.L > loadTuples[1] > 0:更改为if beam.L > loadTuples[1] > 0:

beamModel.beampersonModel.person是可用于创建新实例的类。一般来说,对任何一个(或任何class)的任何引用都应该后跟括号(例如,b = beamModel.beam(8.0E9, 1.333E-4, 5.0),这是正确的)。获得实例后(例如b),只需使用该实例,不再引用beamModel.beam

顺便问一下,您使用的是哪个版本的Python?我实际上对此代码运行感到惊讶 - 我原本期望有关于缺少self参数的错误消息。