Python交互式控制台在matplotlib期间挂起?

时间:2013-10-08 02:28:45

标签: python

当我运行以下绘图时,控制台总是挂起而且绘图永远不会完成。计算不重,但我不确定哪里不合适。

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

我正在使用Python Tools for Visual Studio和Python 2.7,如果这些重要。


showTrajectories(datafilename)可以正确绘制。它涉及showRepresentatives(resultfilename)时会挂起。因此,我将在此处发布resultfilename数据,如下所示:

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 

1 个答案:

答案 0 :(得分:3)

通常,plt.show()会阻止其他命令,直到绘图窗口关闭。因此,您通常只想在绘制完所有内容后调用plt.show()。

当然,如果你想在添加内容时看到情节改变,你可以通过调用

来使用matplotlib的“交互模式”
plt.ion() 
plt.show()

然后,您可以在绘制更多行后使用plt.draw()更新绘图。 (完成后,您可以使用plt.off(); plt.show()使绘图保持不变)。

Spyder可能会自动进入交互模式 - 我不确定这一点。

相关问题