使用matplotlib的空白绘图

2024-05-28 11:17:26 发布

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

我正在用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")

下面是它显示的内容:

Plot in blank

下面是它打印的内容:

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

谢谢你的帮助


Tags: 代码图形plottitlerobotrangeplottingnum
1条回答
网友
1楼 · 发布于 2024-05-28 11:17:26

我写了一个虚拟的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,我得到了一个完美的情节

enter image description here

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

enter image description here

我的结论是,您的return something版本中没有相应的runSimulation-请检查您的代码,并让我们知道我的猜测是否正确

相关问题 更多 >