Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Graphing a Parabola using Matplotlib in Python

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
like image 886
kman29 Avatar asked May 31 '15 03:05

kman29


People also ask

How do you plot a quadratic curve in Python?

Quadratic Equation Below is the Matplotlib code to plot the function y=x2 y = x 2 . It is a simple straight-forward code; the bulk of it in the middle is for setting the axes. As the exponent of x is 2 , there will only be positive values of y , so we can position ax.

Can you use matplotlib in Python?

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. Matplotlib makes easy things easy and hard things possible. Create publication quality plots.


4 Answers

This should do:

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)

mpl-plot

like image 179
cel Avatar answered Oct 03 '22 14:10

cel


Hi there I think that you can use this

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

Parabola

like image 32
taicoding Avatar answered Oct 03 '22 16:10

taicoding


This is your approach with as few changes as possible to make it work (because it's clear that you're a beginner and this is a learning exercise). The changes I made were:

  1. Moved the plt.figure, and other plotting statements out of the loop. The loop now gives you the data to plot, and then you plot it once the loop is finished.

  2. Changed x^2 to x**2.

  3. Changed while to for in your main loop control statement.

  4. Commented out a few lines that weren't doing anything. They all had the same source of error (or non-utility, really): in the for loop, x is set in the loop control line and then y is calculated directly, so you don't need to give them initial values or increment x, though you would have had to do these steps for a while loop.

enter image description here

Here the code:

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()
like image 32
tom10 Avatar answered Oct 03 '22 14:10

tom10


Adjust your third to last line to:

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

Adding the 'r-^' will add red triangular points to the graph. Alternatively, you can use 'b-o'.

Note: You should include the quotes.

For different colors you can use 'b' - blue; 'g' - green; 'r' - red; 'c' - cyan; 'm' - magenta; 'y' - yellow; 'k' - black; 'w' - white

The - in the 'r-^' or 'b-o' will create the line joining the triangles or circles respectively. That is, without the dash, you will end up with a scatter plot.

Alternatively, there is command ....scatter(x,y) which would be equivalent to 'r ^' and 'b o'

like image 29
heykay47 Avatar answered Oct 03 '22 15:10

heykay47