简体   繁体   English

当我 plot python 中的某些东西时,程序执行停止,直到我关闭 plot 图

[英]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.

相关问题 我怎样才能在 python 中 plot 类似的东西? - How can I plot something similar to this in python? 如何在python中关闭图并重新打开它? - How could I close a plot in python and then reopen it? 当我尝试用一个(线)图和一个图上的条形图 plot 制作一个图时,我得到一个奇怪的图 - I get an odd figure when I try to make a figure with both one (line)plot and a bar plot at one figure 如何使用 Python 绘制能源排名图? - How do I plot an energy ranking figure using Python? subprocess.Popen 通信方法自动打开文件,停止程序执行,直到我手动关闭文件 - subprocess.Popen communicate method open automaticaly the file, stops the program execution until I manually close the file 在python为什么要关闭一个plot才能看到下一个? - In python why do I have to close a plot to see the next one? 如何在一个绘图上绘制两个熊猫系列? - How do I plot two Pandas Series on a single plot figure? 为什么当我在 tkinter 窗口内绘制 3d 图形时,它不允许我旋转图形或缩放 - why when i plot a 3d figure inside a tkinter window it doesnt allow me to rotate the figure or zoom 我试图在python中绘制一个5 * 2的情节 - I am trying to plot a 5*2 plot in python Python Plot-将图中的数据倍增 - Python Plot- Multiple the data in plot figure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM