【问题标题】:Graphing a Parabola using Matplotlib in Python在 Python 中使用 Matplotlib 绘制抛物线
【发布时间】:2015-08-13 17:41:31
【问题描述】:

我正在尝试在matplotlib 中绘制一个简单的抛物线,但我对如何在抛物线上绘制点感到困惑。到目前为止,这就是我所拥有的:

import matplotlib.pyplot as plt
a=[]
b=[]
y=0
x=-50
while x in range(-50,50,1):
    y=x^2+2*x+2
    a=[x]
    b=[y]
    fig= plt.figure()
    axes=fig.add_subplot(111)
    axes.plot(a,b)
    plt.show()
    x= x+1

【问题讨论】:

    标签: python python-2.7 matplotlib


    【解决方案1】:

    应该这样做:

    import matplotlib.pyplot as plt
    import numpy as np
    
    # create 1000 equally spaced points between -10 and 10
    x = np.linspace(-10, 10, 1000)
    
    # calculate the y value for each element of the x vector
    y = x**2 + 2*x + 2  
    
    fig, ax = plt.subplots()
    ax.plot(x, y)
    

    【讨论】:

    • import matplotlib.pyplot as plt import numpy as np xval=[] yval=[] y=0 x=-50 while x in range(-50,51,1): y=x* x xval.append(x) yval.append(y) x= x+1 print xval print yval plt.plot(xval,yval) plt.show()
    【解决方案2】:

    这是您的方法,只需尽可能少的更改即可使其发挥作用(因为很明显您是初学者,而且这是一个学习练习)。我所做的更改是:

    1. plt.figure 和其他绘图语句移出循环。循环现在为您提供要绘制的数据,然后在循环完成后绘制它。

    2. x^2 更改为x**2

    3. 在主循环控制语句中将 while 更改为 for

    4. 注释掉了几行没有做任何事情的行。它们都有相同的错误来源(实际上是非实用程序):在for循环中,x设置在循环控制行中,然后直接计算y,所以你不需要给他们初始值或递增 x,尽管您必须在 while 循环中执行这些步骤。

    代码如下:

    import matplotlib.pyplot as plt
    
    a=[]
    b=[]
    # y=0
    # x=-50
    
    for x in range(-50,50,1):
        y=x**2+2*x+2
        a.append(x)
        b.append(y)
        #x= x+1
    
    fig= plt.figure()
    axes=fig.add_subplot(111)
    axes.plot(a,b)
    plt.show()
    

    【讨论】:

      【解决方案3】:

      将倒数第三行调整为:

      axes.plot(a,b, 'r-^')
      

      添加“r-^”会在图表中添加红色三角形点。或者,您可以使用“b-o”。

      注意:您应该包含引号。

      对于不同的颜色,您可以使用 'b' - 蓝色; 'g' - 绿色; 'r' - 红色; 'c' - 青色; 'm' - 洋红色; 'y' - 黄色; 'k' - 黑色; 'w' - 白色

      r-^”或“b-o”中的 - 将分别创建连接三角形或圆形的线。也就是说,如果没有破折号,您最终会得到一个散点图。

      或者,有命令 ....scatter(x,y) 相当于 'r ^' 和 'b o'

      【讨论】:

        【解决方案4】:

        你好,我认为你可以使用这个

        import matplotlib.pyplot as plt
        import numpy as np
        '''
        Set the values in the variable x
        The function arange helps to generate an array with the 
        following parameters arange(start,end,increment)
        '''
        x = np.arange(-100,100,1)
        '''
        Now set the formula in the variable y
        '''
        y = x**2
        '''
        Then add the pair (x,y) to the plot
        '''
        plt.plot(x,y)
        '''
        Finally show the graph
        '''
        plt.show()
        

        【讨论】:

          【解决方案5】:

          我认为您可以为此使用列表推导。

          import matplotlib.pyplot as plt
          from math import sqrt, pow
          plt.style.use('seaborn-darkgrid')
          fig, ax = plt.subplots()
          
          # Defining the range for the input values on the horizontal axis
          x_values = [x for x in range(-50, 50)]
          
          # Computiong the values of the quadratic equation for different values in x_values
          y_values = [(pow(x,2)+4*x+4) for x in x_values]
          
          ax.plot(x_values, y_values, linewidth=2)
          
          plt.show()
          

          a graph generated by python using the library matplotlib

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2022-11-13
            • 1970-01-01
            • 1970-01-01
            • 2017-03-06
            • 1970-01-01
            • 2015-09-29
            • 1970-01-01
            • 2019-10-17
            相关资源
            最近更新 更多