1

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. What should I do?

Sorry if it's a silly question but I'm not quite good with python visualization.

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:

Plot in blank

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!

2
  • Unfortunately that is not all your code, so it is hard to tell what went wrong. Suggest you debug by reducing the code to find the problem. Commented Jul 26, 2021 at 18:40
  • The plot you show has not the correct title, nor the correct axes' labels. I second Jody in thinking you need to show us a little more of your code. Don't put new code in comments, but edit your question, thank you. Commented Jul 26, 2021 at 18:48

1 Answer 1

1

I wrote a dummy 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

enter image description here

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)

enter image description here

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, yes, you were right! I wrote by mistake return print(sum(results)/len(results)), on my runSimulation function, I removed the print and now my code works perfectly! Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.