不能分散,绘图,显示()在循环中

时间:2015-05-29 21:08:58

标签: python

import math
import pylab as plt
import numpy
from numpy import sqrt
from scipy.integrate import quad
import random

numpy.seterr(divide='ignore', invalid='ignore')
def integrand (x):
    return sqrt(1-x**2)

q1area, err = quad(integrand,0,1)

print "This program estimates the convergence of Pi to a ratio of one."
while True:
    print "Please choose from one of the five following options:"
    print " 1. 10^1\n 2. 10^2\n 3. 10^3\n"
    choice = int(raw_input())
    options = {1,2,3}

    if choice == 1:
        plt.xlim([0,15])
        plt.ylim([-5,5])

        x = numpy.linspace(0,15,500)
        y = numpy.sqrt(1-x**2)
        z = 1+x*0

        xcord = []
        ycord = []
        under = []
        above = []
        pratiolist = []
        yvalues = []
        xvalues = range(1,11)

        for i in range(10):
            xcord.append(random.random())
            ycord.append(random.random())

        for j in ycord:
            if (j <= q1area):
                under.append(1)

            else:
                above.append(1)

            punder = len(under)
            if punder == 0:
                punder = punder + 1

            pabove = len(above)
            if pabove == 0:
                pabove = pabove + 1

            pratio = punder / float(pabove)
            pratiolist.append(pratio)

        for k in pratiolist:
            rtpi = k / float(math.pi)
            yvalues.append(rtpi)

        plt.scatter(xvalues,yvalues,c='b')
        plt.plot(x,z,'g')
        plt.show()

    if choice == 2:
        plt.xlim([0,110])
        plt.ylim([-5,5])

        x = numpy.linspace(0,110,500)
        y = numpy.sqrt(1-x**2)
        z = 1+x*0

        xcord = []
        ycord = []
        under = []
        above = []
        pratiolist = []
        yvalues = []
        xvalues = range(1,101)

        for i in range(100):
            xcord.append(random.random())
            ycord.append(random.random())

        for j in ycord:
            if (j <= q1area):
                under.append(1)

            else:
                above.append(1)

            punder = len(under)
            if punder == 0:
                punder = punder + 1

            pabove = len(above)
            if pabove == 0:
                pabove = pabove + 1

            pratio = punder / float(pabove)
            pratiolist.append(pratio)

        for k in pratiolist:
            rtpi = k / float(math.pi)
            yvalues.append(rtpi)

        plt.scatter(xvalues,yvalues,c='b')
        plt.plot(x,z,'g')
        plt.show()

    if choice == 3:
        plt.xlim([0,1100])
        plt.ylim([-5,5])

        x = numpy.linspace(0,1100,500)
        y = numpy.sqrt(1-x**2)
        z = 1+x*0

        xcord = []
        ycord = []
        under = []
        above = []
        pratiolist = []
        yvalues = []
        xvalues = range(1,1001)

        for i in range(1000):
            xcord.append(random.random())
            ycord.append(random.random())

        for j in ycord:
            if (j <= q1area):
                under.append(1)

            else:
                above.append(1)

            punder = len(under)
            if punder == 0:
                punder = punder + 1

            pabove = len(above)
            if pabove == 0:
                pabove = pabove + 1

            pratio = punder / float(pabove)
            pratiolist.append(pratio)

        for k in pratiolist:
            rtpi = k / float(math.pi)
            yvalues.append(rtpi)

        plt.scatter(xvalues,yvalues,c='b')
        plt.plot(x,z,'g')
        plt.show()

    while choice not in options:
        print "Not a valid choice!\n"
        break

#plt.scatter(xvalues,yvalues,c='b')
#plt.plot(x,z,'g')
#plt.show()

我可以让图表显示的唯一方法是,如果我在每个if = = 1,2,3等的末尾放置break语句,然后放置:

plt.scatter(xvalues,yvalues,c='b')
plt.plot(x,z,'g')
plt.show()

在我的代码底部。这很不方便,我希望我无休止地循环,允许在1,2,3之间进行选择,而不必重新运行程序。为什么Python的图表会在崩溃时崩溃?

更新

通过使用plt.draw(),我能够让图表至少显示但仍然没有响应。

1 个答案:

答案 0 :(得分:1)

如果没有回复你的意思是它不再显示提示这是因为plt.show()将导致程序停止直到窗口关闭。你可以用plt.draw()替换plt.show(),但实际上有窗口出现你需要处于交互模式。这是通过在任何绘制调用之前调用plt.ion()来实现的(我把它放在while True之前)。我已经测试了它应该完成你正在寻找的行为。

编辑:由于您没有重绘相同的数据,因此调用draw()会将数据附加到特定的绘图(即,反复输入1将继续添加点)。我不知道你正在寻找什么类型的行为,但如果你想清除这个数字,你可能想在每个分散调用之前调用plt.clf。