Graphing a Parabola using Matplotlib in Python -
i trying graph simple parabola in matplotlib
, confused how supposed plot points on parabola. far, have:
import matplotlib.pyplot 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
this approach few changes possible make work (because it's clear you're beginner , learning exercise). changes made were:
moved
plt.figure
, , other plotting statements out of loop. loop gives data plot, , plot once loop finished.changed
x^2
x**2
.changed
while
for
in main loop control statement.commented out few lines weren't doing anything. had same source of error (or non-utility, really): in loop,
x
set in loop control line ,y
calculated directly, don't need give them initial values or incrementx
, though have had these steps while loop.
here code:
import matplotlib.pyplot plt a=[] b=[] # y=0 # x=-50 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()
Comments
Post a Comment