Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

matplotlib - 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! 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
question from:https://stackoverflow.com/questions/65951965/when-i-plot-something-in-python-the-programs-execution-stops-until-i-close-the-p

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

So instead, try replacing plt.show() by

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...