【问题标题】:Generating plot before end of program in python在python中程序结束之前生成绘图
【发布时间】:2013-04-08 16:13:06
【问题描述】:

我正在尝试将两个数组的元素相互绘制。虽然绘图工作正常,但在询问用户是否要继续之前它不会生成图像。 (此代码嵌入在一个真正的循环中)。

    print 'To achieve the million pound goal using your betting strategy, you would have to spin the wheel approximately',averageiterations, 'times on average.'
    print 'The theoretical probability of you obtaining a million based on your betting strategy is: %9.7e' % p
    print 'The approximate average total amount of times that you ran out of money while using your betting strategy (went below the minimum bet) is:',len(fails)/1000.,', which rounds up to', int(math.ceil(len(fails)/1000.)),'.'

i=0
millions=[]
millionspins=[]
while i<1000:
    millions.append(allnewbank[:][i][-1])
    i=i+1
i=0
while i<1000:
    millionspins.append(len(allnewbank[:][i]))
    i=i+1
fig = plt.figure()
matplotlib.pyplot.scatter(millionspins,millions)
plt.xlabel("Number of spins")
plt.ylabel("Bank")
plt.xlim(0,max(millionspins))
plt.ylim(1000000,max(millions))
plt.show()
while restart not in exit:
        restart=raw_input('Please type "y" to restart or "n" to exit:')
    if restart==exit[0]:
            continue    
    if restart==exit[1]:
        break

【问题讨论】:

  • 欢迎来到 StackOverflow!如果您可以包含一个完整的、最小的工作代码示例,将会很有帮助。有很多不是必需的信息(例如 `x,ylables),并且您的代码没有按照列出的方式运行(什么是 'allnewbank'?)。

标签: python image matplotlib plot


【解决方案1】:

看看程序在哪里要求用户输入会很好。不过,我认为您要问的是如何显示绘图并继续执行该程序?如果是这样,您可以简单地禁用 matplotlib 阻止程序:

plt.show(block = False)

【讨论】:

  • 嘿,我试过了,它返回“show() got an unexpected keyword argument 'block'”
  • 看来block 是实验性的——没有意识到这一点。另一种方法是在开始绘图之前使用plt.ion() 打开交互模式。但我会进一步研究它,因为我不知道它是否会影响其他任何东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-12
  • 1970-01-01
  • 1970-01-01
  • 2021-05-15
  • 1970-01-01
  • 1970-01-01
  • 2018-08-05
相关资源
最近更新 更多