Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中使用Matplotlib绘制抛物线_Python_Python 2.7_Matplotlib - Fatal编程技术网

在Python中使用Matplotlib绘制抛物线

在Python中使用Matplotlib绘制抛物线,python,python-2.7,matplotlib,Python,Python 2.7,Matplotlib,我试图在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) p

我试图在
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
这应该做到:

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)

这是您的方法,只需进行尽可能少的更改即可使其生效(因为很明显,您是初学者,这是一个学习练习)。我所做的改变是:

  • plt.figure
    和其他绘图语句移出循环。循环现在为您提供要打印的数据,然后在循环完成后进行打印

  • x^2
    更改为
    x**2

  • 在主循环控制语句中将
    更改为

  • 注释掉了几行没有任何作用的内容。它们都有相同的错误源(实际上是非实用性的):在for循环中,在循环控制行中设置
    x
    ,然后直接计算
    y
    ,因此您不需要给它们初始值或增量
    x
    ,尽管您需要在一段时间的循环中执行这些步骤

  • 代码如下:

    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()
    

    将第三行调整到最后一行:

    axes.plot(a,b, 'r-^')
    
    添加“
    r-^
    ”将向图形中添加红色三角形点。或者,您可以使用“
    b-o

    注意:您应该包括引号

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

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


    或者,还有一个命令…
    scatter(x,y)
    ,它相当于“
    r^
    ”和“
    bo

    ,我认为您可以使用它

    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()
    

    导入matplotlib.pyplot作为plt导入numpy作为np xval=[]yval=[]y=0 x=-50,而x在范围内(-50,51,1):y=x*x xval.append(x)yval.append(y)x=x+1 print xval print yval plt.plt(xval,yval)plt.show()