Python:未绘制图形

Python:未绘制图形,python,matplotlib,Python,Matplotlib,我的问题是,我正在尝试使用matplotlib.pyplot创建一个图形,以绘制由另一个函数计算的最大偏移量和该最大偏移量的位置,用于“person”对象列表,该列表包含创建加载元组以输入函数所需的信息 我当前的代码如下所示 第1部分beamModel.py(工作正常): 第2部分personModel.py(已给出且不应更改): 函数simulatebamrun的输入示例如下: >>> ps = createPersonList('personData.csv') >&

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

我当前的代码如下所示

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

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

函数
simulatebamrun
的输入示例如下:

>>> 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轴(时间轴)上的一条直线,因此没有绘制max挠度值和max挠度位置。救命啊

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

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

  • beamSimulation.py
    的顶部,删除
    beam=beamModel.beam
  • simulateBeamRun
    中,将
    loadTuples=personModel.person.loadDisplacement(时间)
    更改为
    loadTuples=person.loadDisplacement(时间)
  • simulateBeamRun
    中,将
    if beamModel.beam.L>加载元组[1]>0:
    更改为
    if beam.L>加载元组[1]>0:
beamModel.beam
personModel.person
是可用于创建新实例的类。一般来说,对其中任何一个(或任何
)的任何引用都应后跟括号(例如,
b=beamModel.beam(8.0E9,1.333E-4,5.0)
,这是正确的)。一旦您有了一个实例(例如,
b
),只需使用该实例,不再参考
beamModel.beam


顺便问一下,您使用的是什么版本的Python?事实上,我很惊讶这段代码会运行-我会预期与缺少
self
参数相关的错误消息。

您是否检查了
dList
中的值是否符合预期?这将有助于您将绘图问题与计算问题分开。这可能只是一个输入错误,但您在最后第二个代码块的末尾缺少了一些旁白
matplotlib.pyplot.show
应该是
matplotlib.pyplot.show(),虽然听起来这不会导致您的错误。
personData.csv
中有什么?@cxw我刚刚检查了一下,发现dList返回的元组列表都等于(0.0,0.0),但无法找出原因。谢谢
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)
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
>>> ps = createPersonList('personData.csv')
>>> b = beamModel.beam(8.0E9, 1.333E-4, 5.0)
>>> ts = numpy.linspace(0, 35, 500)
>>> simulateBeamRun(ps, b, ts)