[英]When I plot something in python the programs execution stops until I close the plot figure
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))
.
.
.
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?但问题是,当它第一次调用 plt.plot(...) 时,它会停止执行,除非我关闭图形,否则不会打开 go? 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:我还想在我的 plot 中添加一些文本,但它给了我一个我无法理解的错误:它是:
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()
blocks the execution of your code. plt.show()
阻止代码的执行。 To avoid that, you could replace that line by plt.show(block=False)
.为避免这种情况,您可以用
plt.show(block=False)
替换该行。 Your application will then run, but, as described in this post , your plots will likely not show up during execution.然后您的应用程序将运行,但是,如本文所述,您的绘图可能不会在执行期间显示。
So instead, try replacing plt.show()
by因此,请尝试将
plt.show()
替换为
plt.show(block=False)
plt.pause(0.001)
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.最后,在程序的最后添加一个
plt.show()
以保持绘图打开,否则每个图形都将在程序终止时关闭。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.