我正在尝试绘制具有不同行为的函数,具体取决于x轴的值。对于超过2.0的x轴('ec'变量)的值,它是单向的,对于大于此值的值,它以另一种方式起作用。
import numpy as np
import matplotlib.pyplot as plt
ec = np.linspace(0., 3.,20) #range of 'x' axis values
def ten_def(x):
for i in ec:
if i <= 2.:
return 0.85*x*(1-(1-(ec/2))**2) #behavior for ec < than 2.0
else:
return 0.85*x #behavior for ec > 2.0
fcd = float(input('Fck (MPa): '))/1.4
plt.plot(ec, ten_def(fcd), 'b-', ec,(ec*0)+fcd*0.85,'r-')
plt.title('Tensão-deformação')
plt.xlabel('Deformação (‰)')
plt.ylabel('Tensão (MPa)')
plt.show()
代码的绘图部分正在运行。但它绘制了一条曲线,整个曲线具有相同的行为(它始终考虑ec <2.0)。我做错了什么?
答案 0 :(得分:0)
您必须返回数据列表作为函数的输出。
import numpy as np
import matplotlib.pyplot as plt
ec = np.linspace(0., 3.,20) #range of 'x' axis values
def ten_def(x):
result = []
for i in ec:
if i <= 2.:
result.append(0.85*x*(1-(1-(i/2))**2)) #behavior for ec < than 2.0
else:
result.append(0.85*x) #behavior for ec > 2.0
return result
fcd = float(input('Fck (MPa): '))/1.4
plt.plot(ec, ten_def(fcd), 'b-')
plt.axhline(0.85*fcd, color='r')
plt.title('Tensão-deformação')
plt.xlabel('Deformação (‰)')
plt.ylabel('Tensão (MPa)')
plt.show()
这应该会给你想要的结果