Maria Qureshi
asked on
How to plot graphs in Python
plot where y = x**2 for x over the interval 1 to 5, properly labelled
Create a histogram where the mean = 0, std. dev. = 1, n = 300, and there are sqrt(n) bins
Create a line plot of your choosing with an appropriate legend which displays the formula of the curve depicted.
The formula should be formatted using TeX syntax to create a professional presentation.
Create a histogram where the mean = 0, std. dev. = 1, n = 300, and there are sqrt(n) bins
Create a line plot of your choosing with an appropriate legend which displays the formula of the curve depicted.
The formula should be formatted using TeX syntax to create a professional presentation.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
x_vals = (1, 2, 3, 4, 5)
y_vals = (x_vals**2)
plt.plot(x_vals, y_vals)
plt.xlabel("my x label")
plt.ylabel("my y label")
plt.legend(("$y=x^2$",))
plt.show()
TypeError Traceback (most recent call last)
<ipython-input-27-b734c710 68e0> in <module>()
6
7 x_vals = (1, 2, 3, 4, 5)
----> 8 y_vals = (x_vals**2)
9
10 plt.plot(x_vals, y_vals)
TypeError: unsupported operand type(s) for ** or pow(): 'tuple' and 'int'
I dont get why this is showing as an error?
y_vals = (x_vals**2)
plt.plot(x_vals, y_vals)
plt.xlabel("my x label")
plt.ylabel("my y label")
plt.legend(("$y=x^2$",))
plt.show()
TypeError Traceback (most recent call last)
<ipython-input-27-b734c710
6
7 x_vals = (1, 2, 3, 4, 5)
----> 8 y_vals = (x_vals**2)
9
10 plt.plot(x_vals, y_vals)
TypeError: unsupported operand type(s) for ** or pow(): 'tuple' and 'int'
I dont get why this is showing as an error?
`x_vals = (1, 2, 3, 4, 5)` is a tuple. You can't raise a tuple to a power, so you get an error.
On the other hand `x = linspace(1, 5, 1)` is an `ndarray`, and by using `x**2` you raise each element of `x` to the power of 2.
On the other hand `x = linspace(1, 5, 1)` is an `ndarray`, and by using `x**2` you raise each element of `x` to the power of 2.
ASKER
Still getting an error
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
from numpy import linspace
%matplotlib inline
x_vals = linspace(1, 2, 3, 4, 5)
y_vals = x_vals**2
plt.plot(x_vals, y_vals)
plt.xlabel("my x label")
plt.ylabel("my y label")
plt.legend(("$y=x^2$",))
plt.show()
-------------------------- ---------- ---------- ---------- ---------- ---------
TypeError Traceback (most recent call last)
<ipython-input-4-0c9e1253f 2b1> in <module>()
6
7
----> 8 x_vals = linspace[1, 2, 3, 4, 5]
9 y_vals = x_vals**2
10
TypeError: 'function' object is not subscriptable
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
from numpy import linspace
%matplotlib inline
x_vals = linspace(1, 2, 3, 4, 5)
y_vals = x_vals**2
plt.plot(x_vals, y_vals)
plt.xlabel("my x label")
plt.ylabel("my y label")
plt.legend(("$y=x^2$",))
plt.show()
--------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-0c9e1253f
6
7
----> 8 x_vals = linspace[1, 2, 3, 4, 5]
9 y_vals = x_vals**2
10
TypeError: 'function' object is not subscriptable
`linspace` needs start and stop as first and second argument respectively. Optionally you can provide other arguments. Check the link for more details.
Also you should be using parenthesis since linspace is a function and needs a parenthesis to be called: `x = linspace(1, 5, 50)`
Also you should be using parenthesis since linspace is a function and needs a parenthesis to be called: `x = linspace(1, 5, 50)`
ASKER
ahhh got it. Thank you so much!!
ASKER
Thank you so much I understand now
ASKER
TypeError Traceback (most recent call last)
<ipython-input-27-b734c710
6
7 x_vals = (1, 2, 3, 4, 5)
----> 8 y_vals = (x_vals**2)
9
10 plt.plot(x_vals, y_vals)
TypeError: unsupported operand type(s) for ** or pow(): 'tuple' and 'int'
I dont get why this is showing as an error?