Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When I plot something in python the programs execution stops until I close the plot figure [duplicate]

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
like image 952
Mohammad Amin Avatar asked Jul 21 '26 10:07

Mohammad Amin


1 Answers

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.

like image 55
Christian Karcher Avatar answered Jul 22 '26 23:07

Christian Karcher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!