1

When I run the following drawing, the console always just hangs there and the drawing is never finished. The computation is not heavy, but I am not sure where is inappropriate.

import matplotlib.pyplot as plt

def showTrajectories(datafilename):
    with open(datafilename) as f1:
        content1 = f1.readlines()
    content1 = [line.strip() for line in content1] # remove newline character
    for i in range(int(content1[1])):
        traj = [float(x) for x in content1[i+2].split()]
        x = traj[2:][::2]
        y = traj[2:][1::2]
        plt.plot(x, y, color = '#99ffff')
    plt.title(datafilename)
    plt.xlabel('x (m)', fontsize=16)
    plt.ylabel('y (m)', fontsize=16)
    plt.show()


def showRepresentatives(resultfilename):
    with open(resultfilename) as f2:
        content2 = f2.readlines()
    content2 = [line.strip() for line in content2] # remove newline character
    for i in range(int(content2[0])):
        traj = [float(x) for x in content2[i+1].split()]
        x = traj[2:][::2]
        y = traj[2:][1::2]
        plt.plot(x, y, linewidth=3, color='#006633')
    plt.title(datafilename)
    plt.xlabel('x (m)', fontsize=16)
    plt.ylabel('y (m)', fontsize=16)
    plt.show()

I am using Python Tools for Visual Studio and Python 2.7, if these matter.


The showTrajectories(datafilename) can be plotted correctly. It hangs when it comes to showRepresentatives(resultfilename). So I will just post the resultfilename data here as follows:

10
0 4 -23.6 -2.7 -35.6 -2.1 -47.3 -1.2 -58.0 -1.0 
1 6 -56.6 4.5 -58.0 16.7 -59.2 27.4 -60.4 38.8 -61.5 49.5 -62.5 60.1 
2 7 -0.6 10.7 -1.5 21.9 -2.6 32.7 -3.6 43.4 -4.7 54.5 -5.6 65.1 -5.8 75.7 
3 10 -26.9 73.9 -11.1 75.4 -0.4 75.9 11.9 76.0 22.6 76.0 33.5 76.5 44.2 76.3 55.5 76.2 66.8 77.0 79.3 77.8 
4 10 9.2 -8.5 19.1 -14.1 29.6 -16.0 41.4 -16.5 52.2 -16.6 63.0 -16.7 74.1 -15.3 85.8 -12.9 96.5 -13.8 107.0 -15.4 
5 8 71.0 -21.0 72.1 -10.4 72.4 0.6 72.2 11.4 72.2 22.7 71.0 33.3 70.0 45.6 70.7 56.5 
6 2 72.2 -18.8 79.9 -26.3 
7 3 72.6 28.8 86.5 29.4 98.1 30.6 
8 5 -60.4 61.2 -71.0 54.2 -81.2 47.0 -90.0 40.9 -99.1 33.3 
9 3 73.2 56.2 86.4 58.0 97.7 59.1 
2
  • The code looks good... Does it hang during the showTrajectories or the showRepresentatives function? Perhaps you can try to generate some fake data and post that here so that we can play with a complete working example. One thought: plt.show() generally blocks all other commands until you close the plot window, so that might be the problem if you are trying to call both functions one after the other. Only call plt.show() at the very end. Commented Oct 8, 2013 at 2:46
  • @DanHickstein Awesome! You are correct! It works when I move show() to the end. But I copy and past it into Spyder it works. But fails in VS. Strange. Would you mind posting it as an answer? Commented Oct 8, 2013 at 2:56

1 Answer 1

3

Generally, plt.show() blocks other commands until the plot window is closed. So, you typically only want to call plt.show() after you are done plotting everything.

Of course, if you want to see the plot change as you add stuff, you could use matplotlib's "interactive mode" by calling

plt.ion() 
plt.show()

Then, you can update the plot using plt.draw() after you plot more lines. (When you are done, you can use plt.off(); plt.show() to make the plot stick around).

Spyder may automatically put you into interactive mode - I'm not sure about this.

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

1 Comment

A follow up question: stackoverflow.com/questions/19238578/… Please kindly help :)

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.