简体   繁体   English

为什么我的图形无法正确绘制?

[英]Why wont my graph plot correctly?

I'm trying to plot the graph y= 5*x - 300/x, but my ouptut is just a straight line and I don't understand why. 我正在尝试绘制图y = 5 * x-300 / x,但是我的输出只是一条直线,我不明白为什么。 Can anyone help? 有人可以帮忙吗? Thanks 谢谢

xvls= []
Rvls= []

for x in range(-100,100,1):

    if x != 0:
       def R(x):
           f1 = 5*x
           f2 = 300/x
           f3 = f1+f2
           return f3
       error = 10
       while error >= 1e-6:
           error = R(x)-x
           x = x -error
           Rvls.append(R(x))
           xvls.append(x)
    else:
        print 0 

fig = plt.figure(figsize=(10,5))
xvls=np.linspace(-300,300, 25)
Rvls= np.linspace(-300,300, 25)
plt.subplot(1,2, 1)
plt.plot(xvls, Rvls, linestyle='-', marker='o', color='blue')
plt.xlabel('Distance from right hand pin')
plt.ylabel('Reaction force at left hand pin')
plt.title('The relationship between R(x) and x')
plt.rc('font', size=12)
plt.grid()

我认为linspace返回给定值之间的直线的值。

The lines 线

xvls=np.linspace(-300,300, 25)
Rvls= np.linspace(-300,300, 25)

override all the calculation you did before. 覆盖您之前所做的所有计算。 You probably want to leave them out. 您可能想将它们排除在外。

I would guess that the line f2 = 300/x is a possible culprit. 我猜想那行f2 = 300/x可能是罪魁祸首。 If you're using Python 2.x f2 will evaluate to an integer because both 300 and x are integers. 如果您使用的是Python 2.x,则f2的值将为整数,因为300和x均为整数。 It wouldn't account for the fact that you get a straight line, but you haven't given a completely compilable code example to troubleshoot. 这不会说明您得到一条直线的事实,但是您没有给出完整的可编译代码示例进行故障排除。 Could you provide the minimum needed code that compiles and produces the problem? 您能提供编译和产生问题的最少所需代码吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM