Python:未绘制图形

2024-06-26 14:41:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我的问题是我试图用matplotlib.pyplot为“person”对象列表绘制由另一个函数计算的最大挠度和该最大挠度的位置,该列表包含创建要输入到函数中的加载元组所需的信息。在

我当前的代码如下所示。在

第一部分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]

第二部分个人模型.py(已给出,不应更改):

^{pr2}$

第三部分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轴(时间轴)上的一条直线,因此maxDefection的值和maxDefection的位置没有被绘制出来。救命?!在

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


Tags: ofthetoinimportselfforreturn
1条回答
网友
1楼 · 发布于 2024-06-26 14:41:37

您引用的是而不是相应的实例:例如,beamModel.beam直接位于您应该引用的beam。在

  • beamSimulation.py的顶部,移除{}。在
  • 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.beam和{}是可以用来创建新实例的类。一般来说,对其中任何一个(或任何class)的引用都应该后跟括号(例如,b = beamModel.beam(8.0E9, 1.333E-4, 5.0),这是正确的)。一旦你有了一个实例(例如,b),只需使用它,不再引用beamModel.beam

顺便问一下,您使用的Python版本是什么?实际上,我对这段代码的运行感到惊讶—我本以为会出现与缺少self参数有关的错误消息。在

相关问题 更多 >