简体   繁体   English

使用 matplotlib 的空白图

[英]Blank plot using matplotlib

I am doing a course in Python (I do not want them to solve the course questions or anything like that) and in this part, the course gives you a code so that you can visualize through graphics the number of robots (x-axis) and time- steps (y-axis), but when I run the code, it just doesn't graph anything.我正在用 Python 做一门课程(我不希望他们解决课程问题或类似的问题),在这一部分,该课程为您提供了一个代码,以便您可以通过图形可视化机器人的数量(x 轴)和时间步长(y 轴),但是当我运行代码时,它只是没有绘制任何图形。 What should I do?我应该怎么办?

Sorry if it's a silly question but I'm not quite good with python visualization.对不起,如果这是一个愚蠢的问题,但我不太擅长 python 可视化。

Here is some of the code:这是一些代码:

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")

Here is what it shows:这是它显示的内容:

在空白处绘制

Here is what it prints:这是它打印的内容:

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

Thanks for your help!谢谢你的帮助!

I wrote a dummy runSimulation我写了一个虚拟的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

and, using exactly the showPlot1 that you posted, I got a perfect plot并且,完全使用您发布的showPlot1 ,我得到了一个完美的情节

在此处输入图片说明

then I removed the return result statement from my dummy code and this is what I got (note the axes'limits, the same as in your figure)然后我从我的虚拟代码中删除了return result语句,这就是我得到的(注意轴的限制,与你的图中相同)

在此处输入图片说明

My conclusion, you have not a consequential return something in your version of runSimulation — please check your code and please let us know if my guess is correct.我的结论是,您的runSimulation版本中没有相应的return something runSimulation - 请检查您的代码,如果我的猜测正确,请告诉我们。

Your axes limits are not correct: you need x limits to be at least [1, 11] and y limits to be [0, 2200] (depending on the range of your simulation results, it could be even more) to be able to visualize your data.您的轴限制不正确:您需要 x 限制至少为 [1, 11] 并且 y 限制为 [0, 2200](取决于您的模拟结果的范围,甚至可能更多)才能可视化您的数据。

Normally matplotlib automatically set these limits.通常 matplotlib 会自动设置这些限制。 Not sure why it did not do it in this case (prob the answer is in the code that you have not posted).不知道为什么在这种情况下它没有这样做(可能答案在您尚未发布的代码中)。

Try adding尝试添加

  pylab.gca().set_xlim([0, 12])
  pylab.gca().set_ylim([0, 2200])

(see matplotlib reference here ) just before pylab.show() , maybe it will help. (请参阅此处的matplotlib 参考)就在pylab.show()之前,也许它会有所帮助。

Also, as in the comments, it would be better for everyone if you could post a minimal reproducible example - please do, if this answer does not help!此外,正如在评论中一样,如果您可以发布一个最小的可重复示例,对每个人都会更好-如果此答案没有帮助,请这样做!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM