# Very simple question y=x^2

Very simple question y=x^2
0
#1

Hi

Sorry for this simple question, but it’ll help me understand the later plots. How do you plot y=x^2?

This is what I got so far:

import pandas as pd
from matplotlib import pyplot as plt
plt.plot(x,x**2)
plt.show()

Doesn’t seem to work, what is missing?

0 Likes

#2

It’s a little hard to help you without some more definitive values for x any y (x^2 in this case).

If this is the exact code you are attempting to run, my guess as to the issue is that your X and Y values that you’re attempting to plot are not defined. Both the X and Y values need to have definitive values in order to plot them in this way (define both an x and y before attempting to plot them).
Here is some code that breaches this matter by assigning a list of x & y values to both an x & y variable prior to plotting:

``````from matplotlib import pyplot as plt
# pandas does not need to be imported in this code unless a dataset
# which warrants it is being evaluated
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [X**2 for X in x]  # both an X and a Y datset need to be defined for this, this is just an example
#  using a defined X and a list comprehension to define Y.
# y = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

plt.plot(x, y)  # produce a plot object that contains x as the X-axis and y as the Y-axis
plt.show()  # launch a new window showing the graph
``````

Hopefully this helps. For any matplotlib plot object you need to have some object (normally a list or array) that contains both the x values and the y values to plot against.

0 Likes