Skip to main content
 首页 » 编程设计

python之matplotlib 只显示点

2025年02月15日14kevingrace

我在下一个代码中绘制线和点时遇到问题,当我使用 plt.plot 和 x4 时,y4 只显示点,我想知道问题出在哪里。我正在使用 python 3.52 和 wingware python IDE 101 5.1。

import matplotlib.pyplot as plt 
from itertools import chain 
 
 
 
od = float(input("Ingrese el diametro externo: ")) 
id = float(input("Ingrese el diametro interno: ")) 
Yp = int(input("Ingrese la cedencia de la tuberia: ")) 
DF2 = float(input("Ingrese factor de diseño triaxial: ")) 
 
area=((od**2)-(id**2))*0.7854 
Fy=area*Yp 
Fy=int(Fy) 
 
Fy2=-(1.16*Fy) 
Fy2=int(Fy2) 
Fy3=(1.16*Fy) 
Fy3=int(Fy3) 
t = ((od - id) / 2) 
 
 
 
s= (od/t) 
s=float(s) 
 
ca=Fy2 
 
 
c_com=range(Fy2,0,2000) 
c_ten=range(0,Fy3,2000) 
 
 
for ca in chain(c_com,c_ten): 
 
 
   ca1 = (ca / area)  
   BB = (ca1 / Yp) 
   cc = ((1 - (0.75 * ((BB) ** (2))))) 
   if cc<0: 
      cc = 0 
   d = (((cc) **(0.5)) - (0.5 * BB)) 
   d1 = (((cc) **(0.5)) + (0.5 * BB)) 
   Ype= d * (Yp) 
   Ype1=d1 * (Yp) 
 
   a = 2.8762 + (0.10679* (0.00001)*Ype )+(0.21301 * (0.0000000001) * ((Ype) ** (2)))- (0.53132 * (1E-16) * ((Ype) **(3))) 
 
   b = 0.026233 + (0.50609 * (0.000001) * Ype) 
   c = -465.93 + (0.030867 * Ype) - (0.10483 * (0.0000001) * ((Ype) ** (2))) + (0.36989 * (0.0000000000001) * ((Ype) ** (3))) 
   if c < -20000 : 
      break 
   O = (b / a) 
   f = ((46.95 * 1000000) * (((3 * O) / (2 + O)) ** (3))) / ((Ype * (((3 * O) / (2 + O) - O))) * ((1 - ((3 * O) / (2 + O)))) ** (2)) 
 
   g = f * O 
 
 
   L = (c/ Ype) 
   s1 = ((((a - 2) + (8 * (b+ L))) ** (0.5)) + (a - 2)) / (2 * (b + L)) 
   s1=abs(s1) 
   s2 = (Ype * (a - f)) / (c + (Ype * (b - g))) 
   s2=float(s2) 
   s3 = (2 + (b / a)) / (3 * b / a) 
   s3=float(s3)   
 
 
   Pc1 = 2 * Ype * ((s - 1) / (s * s)) 
   Pc2 = (Ype * ((a / s) - b)) - c  
   Pc3 = (Ype * ((f / s) - g)) 
   Pc4 = (46.95 * 1000000) / (s * ((s - 1) ** 2)) 
 
 
   Pc=0   
   Pc=int(Pc) 
 
 
   if s<s1 :  
      Pc= Pc1 
 
   elif s1<s<s2: 
      Pc = Pc2 
   elif s2 < s < s3: 
      Pc= Pc3 
   elif s>s3: 
      Pc = Pc4 
 
   Pe=0 
   Pe = (0.875 * 2 * Ype1 * t) / od 
   Pe=int(Pe) 
   Pc=int(Pc)  
   if 0<ca<Fy-7000: 
      coo=((-Pc/1)) 
      caa=(ca) 
      x4=(caa,Fy) 
      y4=(coo,0) 
      plt.plot(x4,y4,"ro-",markersize=3) 
      plt.show() 
 
 
   if ca==0: 
      co=str((-Pc)) 
      Pb=str((Pe)) 
      x3=[0,-Fy,-Fy,-Fy,0,Fy,Fy] 
      y3=[co,co,0,Pb,Pb,Pb,0] 
      plt.plot(x3,y3,color='y',linewidth=2) 

请您参考如下方法:

将 plt.show 移到 if 子句之外,并将其放在代码的最后,或者 for 的末尾环形。

当您调用 plt.plot 时,会构建一个新图,但不会显示,因此不会绘制图像,但结构已经创建。您可以使用新数据多次调用 plt.plot,这些数据会累积,当您调用 show 时,将导致在同一图像上绘制许多图形。