对于我的程序,我试图获得悬臂梁的用户输入属性,并且从它们的输入中,程序将输出剪切图。我从简单的向下负载开始(用户输入点在梁上发生负载和负载值)。我认为一个分段函数类型的设置可以工作,所以这里是我的代码:
length = input("input the length of your beam: ")
load = input("input your load: ")
distl = input("input the distance from the wall where your load starts: ")
distr = input("input the disance from the wall where your load ends: ")
load = float(load)
load = -load
length = int(length)
distl = int(distl)
distr = int(distr)
i = 0
graph = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
while i<=length:
if distl < i < distr:
graph[i] = load
else:
graph[i] = 0
i = i +1
plt.ylabel('Shear (V(x))')
plt.xlabel('x (meters)')
plt.plot(graph)
plt.show()
这最终输出如下图: enter image description here
有人可以向我解释为什么这包括斜坡而不仅仅是跳跃?感谢。
答案 0 :(得分:2)
(更新:)也许您正在寻找matplotlib.pyplot.step(X, Y, ...)
:
>>> import matplotlib.pyplot as plt
>>> X = range(8)
>>> Y = [2, 2, 3, 3, 4, 4, 2, 2]
>>> plt.step(X, Y)
[<matplotlib.lines.Line2D object at 0x000004124CEC0090>]
>>> plt.show()
通过为每个点提供Y和Y坐标,可以明确地请求垂直线:
>>> X = [0, 1, 1, 2, 2, 3, 3, 4]
>>> Y = [2, 2, 3, 3, 4, 4, 2, 2]
>>> plt.plot(X, Y)
[<matplotlib.lines.Line2D object at 0x000004124CEC0090>]
>>> plt.show()
要仅绘制 水平线或仅垂直线,请分别使用matplotlib的hlines
或vlines
。
想避免画线段吗? NaN没有连接:
>>> X = [0, 1, 1, 2, 2, 2, 3, 3, 4]
>>> Y = [2, 2, 3, 3, float('NaN'), 4, 4, 2, 2]
>>> plt.plot(X, Y)
[<matplotlib.lines.Line2D object at 0x0000016250A0C060>]
>>> plt.show()
回答默认情况下线段不垂直的原因: 因为分段线性图比分段常数图(视觉上)更接近现实生活函数。
答案 1 :(得分:0)
有人可以向我解释为什么这包括斜坡而不仅仅是跳跃?
因为(在图中)x = 2,剪切值为0,而在x = 3时,它为-4。连接它的唯一方法是使用“斜率”线 - 斜边,其中x和剪切值的变化是三角形的另外两个边。
对于该直线,剪切必须与x = 2时的值0和-4同时进行,这是不可能的(在非量子物理学中:D)。
正如在评论中正确指出的那样,出现的那一行是垂直的,你必须提高分辨率 - 拥有更多的x数据点。