在 Python 中使用 Matplotlib 绘制抛物线图 [英] Graphing a Parabola using Matplotlib in Python

查看:97
本文介绍了在 Python 中使用 Matplotlib 绘制抛物线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to graph a simple parabola in matplotlib and I am confused as to how I am supposed to plot points on the parabola. So far, this is what I have:

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)

这篇关于在 Python 中使用 Matplotlib 绘制抛物线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆