【问题标题】:graph functions with range in pythonpython中具有范围的图形函数
【发布时间】:2021-05-07 16:01:32
【问题描述】:

这是我关于 stackoverflow 的第一个问题,如果我遗漏了一些数据,他们会告诉我,我需要的是使用 python 语言创建一个函数图,但我没有找到有关如何执行此操作的信息,我找到了如何制作图表但不限制功能

我需要在 python 中绘制的图形类似于此函数的范围
这是函数

       { V₁x             0 ≤ x < a
       { V₁x - q(x-a)/2  a ≤ x ≤ a+l  # probably it's q(x-a)²/2
M(x) = { V₂(L-x)         a+l < x ≤ L
       { 0               otherwise



from matplotlib import pyplot as plt
x1 = [40, 50, 60, 70, 80, 90, 100]
y1 = [40, 50, 60, 70, 80, 90, 100]
plt.plot(x1, y1)
plt.xlabel('Like Geeks X Axis')
plt.ylabel('Like Geeks Y Axis')
axes = plt.axes()
axes.set_xlim([4, 8])
axes.set_ylim([-0.5, 2.5])
plt.show()

【问题讨论】:

  • 这能回答你的问题吗? Plot Piecewise Function in Python
  • Andres,¿ 你确定区间 a&lt;x&lt;a+l 的公式吗?在我的知情理解中,应该是V1*x-q*(x-a)²/2 ...
  • @sophros 据我了解,您引用的问题是关于表示具有单点不连续性的函数的(被误解的)方式,因此此处给出的答案不适用。

标签: python matplotlib graph


【解决方案1】:

我认为您可以创建一个函数来重现您的数学公式,然后使用该函数获取 ys。

如果你需要你的代码是通用的,那么这样做:

from matplotlib import pyplot as plt


def create_function(V1, V2, a, l, q, L):
    def f(x):
        if x >= 0 and x < a:
            return V1 * x
        elif x >= a and x <= a + l:
            return V2 * x - q * (x - l)
        elif x > a + l and x <= L:
            return V2 * (L - x)
        return 0

    return f


f = create_function(1, 2, 1, 2, 0.5, 5)

x1 = list(range(15))
y1 = list(map(f, x1))

plt.plot(x1, y1)
plt.xlabel("Like Geeks X Axis")
plt.ylabel("Like Geeks Y Axis")
plt.show()

这样你就有了一个通用函数create_function,它可以从你的图像中返回带有任何参数选择的函数

【讨论】:

    【解决方案2】:

    如果我理解您要做什么,绘制部分加载恒定分布载荷的梁的弯矩图,这是您的图表

    这里是产生它的代码……但首先是一些初步的评论

    • 如果真的是弯矩,定义有误,冒昧更正你的定义
    • Matplotlib(和它的兄弟 Numpy)不是关于符号的操作(如果需要,请查看 Sympy)所以所有涉及的数量,qlaL 必须指定为数字

    就是说,这里是代码

    import matplotlib.pyplot as plt
    
    def M(L, a, l, q, npoints=201):
        # npoints is an OPTIONAL argument with a default value
        from numpy import linspace, where
        # we MATERIALIZE our x axis as a sequence of points, an array in Python-speak
        x = linspace(0, L, npoints)
        # compute the reactions
        V2 = (l*q)*(a+l/2) / L
        V1 = l*q-V2
        # compute the bending moment,
        # our where is similar to Excel "IF()" function, if you know Excel 
        bm = where(x<a, V1*x, where(x<a+l, V1*x - q*(x-a)**2/2, V2*(L-x)))
        # the caller of this function wants to know the points in the arrays x and bm
        return x, bm
    
    # L = 10 units of length, a = 4, l = 2;  q = 40 units of force per unit of length
    x, bm = M(10, 4, 2, 40) # note: no optional npoints, 201 points will be used
    
    fig, ax = plt.subplots()
    ax.plot(x, bm)
    # good mannered individuals plot the bending moment on the side of tension stresses
    # so we invert the y axis
    ax.invert_yaxis()
    ax.grid()
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-16
      相关资源
      最近更新 更多