使用 matplotlib 的空白图

Blank plot using matplotlib

我正在 Python 上一门课程(我不希望他们解决课程问题或类似问题),在这部分中,课程为您提供代码,以便您可以通过图形进行可视化机器人的数量(x 轴)和时间步长(y 轴),但是当我 运行 代码时,它只是不绘制任何图形。我该怎么办?

对不起,如果这是一个愚蠢的问题,但我对 python 可视化不是很好。

下面是部分代码:

def showPlot1(title, x_label, y_label):
    """
    What information does the plot produced by this function tell you?
    """
    num_robot_range = range(1, 11)
    times1 = []
    times2 = []
    for num_robots in num_robot_range:
        print("Plotting", num_robots, "robots...")
        times1.append(runSimulation(num_robots, 1.0, 20, 20, 0.8, 20, StandardRobot))
        times2.append(runSimulation(num_robots, 1.0, 20, 20, 0.8, 20, RandomWalkRobot))
    pylab.plot(num_robot_range, times1)
    pylab.plot(num_robot_range, times2)
    pylab.title(title)
    pylab.legend(('StandardRobot', 'RandomWalkRobot'))
    pylab.xlabel(x_label)
    pylab.ylabel(y_label)
    pylab.show()

showPlot1("Title", "Number of robots", "Time-steps")

这是它显示的内容:

这是它打印的内容:

Plotting 1 robots...
801.95
2019.15
Plotting 2 robots...
387.35
997.35
Plotting 3 robots...
256.4
732.7
Plotting 4 robots...
201.3
430.2
Plotting 5 robots...
158.25
366.3
Plotting 6 robots...
133.15
328.0
Plotting 7 robots...
113.65
276.1
Plotting 8 robots...
100.0
239.9
Plotting 9 robots...
85.9
191.85
Plotting 10 robots...
78.1
162.3

感谢您的帮助!

我写了一个假人runSimulation

def runSimulation(nrobots, a,b,c,d,e, randomrobot):
    if randomrobot:
        result = (450+100*rand())/(nrobots*(1+0.05-rand()*0.10))
    else:
        result = (900+200*rand())/(nrobots*(1+0.05-rand()*0.10))
    print("%10.3f"%result, end='')
    return result

并且,完全使用您发布的 showPlot1,我得到了一个完美的情节

然后我从我的虚拟代码中删除了 return result 语句,这就是我得到的(注意轴的限制,与您的图中相同)

我的结论是,您的 runSimulation 版本中没有相应的 return something — 请检查您的代码,如果我的猜测正确,请告诉我们。