当我在python中绘图时,程序执行停止,直到我关闭绘图 [英] When I plot something in python the programs execution stops until I close the plot figure

查看:148
本文介绍了当我在python中绘图时,程序执行停止,直到我关闭绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,在计算了一些内容之后,我希望它在每一步都绘制它们

This is my code and after calculating some stuff I want it to draw them at each step

import time
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
FilePatch='E:\\# Civil Engineering Undergraduate\\Projects\\Python\\Frame'
NodesFile=FilePatch+'\\nodes.xlsx'
MemsFile=FilePatch+'\\members.xlsx'
MatsFile=FilePatch+'\\sections.xlsx' 

nodes=pd.read_excel(NodesFile)

mems=pd.read_excel(MemsFile)
mats=pd.read_excel(MatsFile)

nodes=np.array(nodes)
mems=np.array(mems)
mats=np.array(mats)

np.nan_to_num(nodes)
np.nan_to_num(mems)
np.nan_to_num(mats)

Segments=100
Scale=1

n=np.size(nodes[:,0])
m=np.size(mems[:,0]) 
UsedEIA=np.zeros((m,3)) 
.
.
.

但是问题是,当它第一次调用plt.plot(...)时,它将停止执行,除非我关闭该图,否则它将不会继续!这个问题有什么解决办法吗?

But the problem is that when it calls the plt.plot(...) for the first time it stops execution and won't go on unless I close the figure! Is there any solution for this issue??

. 
. 
.

for i in range(1,1+n):
    dx=Scale*D[3*i-3,0] 
    dy=Scale*D[3*i-2,0] 
    xn=nodes[nodes[:,0]==i,1]+dx 
    yn=nodes[nodes[:,0]==i,2]+dy 
    plt.text(xn,yn,str(i))
    s=np.sum(nodes[nodes[:,0]==i,3:5]) 
    if nodes[nodes[:,0]==i,5]==1:
        plt.scatter(xn,yn,c='r',marker='s')
    elif nodes[nodes[:,0]==i,3]==1 or nodes[nodes[:,0]==i,4]==1:
        plt.scatter(xn,yn,c='g',marker='^')    
    plt.axis('equal')
    plt.show()
    time.sleep(0.1)

我也想在情节中添加一些文字,但这给了我一个我听不懂的错误!在这里:

Also I wanna add some text in to my plot but it gives me an error which I can't understand it! Here it is:

p=mems[i,4] 
px=mems[i,3] 
dl=mems[i,5]*L 
w=mems[i,6]


xtxt=(FrameShape[0,0]+FrameShape[0:])/2 
ytxt=(FrameShape[1,0]+FrameShape[1:])/2 
xtxtp=FrameShape[0,0] 
xtxtpx=FrameShape[0,0]+abs(px)/(1+abs(p)) 
xtxtw=FrameShape[0,0]+abs(p)/(1+abs(p))+abs(px)/(1+abs(px)) 

if p!=0 or px!=0:
    btxt=' Py='+str(p)+' , Px=',str(px)+' @'+str(dl)
    plt.text(xtxtp,ytxt-0.5,btxt)


XY=np.array([X,Shape])
FrameShape=np.transpose(T[0:2,0:2])@XY 
FrameShape[0,:]=FrameShape[0,:]+xi 
FrameShape[1,:]=FrameShape[1,:]+yi 

if w!=0:
    atxt='UL='+str(w)
    plt.text(xtxtw,ytxt+0.5,atxt)

这是它在控制台中给我的错误:

This is the error it gives me in the console:

 TypeError: only size-1 arrays can be converted to Python scalars

推荐答案

plt.show()阻止代码的执行.为避免这种情况,您可以用 plt.show(block = False)替换该行.然后,您的应用程序将运行,但是,如本文中所述,,您的绘图可能不会在执行期间显示.

plt.show() blocks the execution of your code. To avoid that, you could replace that line by plt.show(block=False). Your application will then run, but, as described in this post, your plots will likely not show up during execution.

因此,相反,尝试将 plt.show()替换为

So instead, try replacing plt.show() by

plt.show(block=False)
plt.pause(0.001)

,以便在运行时查看图表.最后,在程序的最后添加一个 plt.show(),以保持绘图打开,否则在程序终止时每个图都将关闭.

in order to see the plots during runtime. Finally, add a plt.show() at the very end of your program to keep the plots open, elsewise every figure will be closed upon program termination.

这篇关于当我在python中绘图时,程序执行停止,直到我关闭绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆