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.
Maria QureshiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fermi ParadoxPhysicistCommented:
You question is rather unclear. I'm assuming you need 2 things:
- plot y=x**2 with TeX labels.
- plot a normal distribution within a range.

To plot y=x**2:

from numpy import linspace
import matplotlib.pyplot as mpl

x_vals = linspace(1, 5, 100)
y_vals = x_vals**2

mpl.plot(x_vals, y_vals)
mpl.xlabel("my x label")
mpl.ylabel("my y label")
mpl.legend(("$y=x^2$",))
mpl.show()

Open in new window


Figure_1.png
To plot a normal distribution you can create a function that picks points within the range you want (eg. 1 STD) and then plot it:

import matplotlib.pyplot as plt
import numpy


STANDARD_DEVIATION_VALUE = 1
DISTRIBUTION_CENTER = 0
TOTAL_POINTS = 300
NUMBER_OF_BINS = int(numpy.sqrt(TOTAL_POINTS))


def single_num(n):
    # Repeats until a number within the scale is found.
    while 1:
        num = numpy.random.normal(loc=DISTRIBUTION_CENTER, scale=STANDARD_DEVIATION_VALUE)

        if abs(DISTRIBUTION_CENTER-num) <= (STANDARD_DEVIATION_VALUE * n):
            return num

# One standard deviation apart.
lst_of_nums = [single_num(n=1) for _ in range(TOTAL_POINTS)]

plt.hist(lst_of_nums, bins=NUMBER_OF_BINS)
plt.xlabel("value")
plt.ylabel("frequency")
plt.show()

Open in new window


(Note that I'm using "n" as "number of STDs")

Figure_2.png

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Maria QureshiAuthor Commented:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-27-b734c71068e0> 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?
Maria QureshiAuthor Commented:
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-b734c71068e0> 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?
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Fermi ParadoxPhysicistCommented:
`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.
Maria QureshiAuthor Commented:
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-0c9e1253f2b1> 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
Fermi ParadoxPhysicistCommented:
`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)`
Maria QureshiAuthor Commented:
ahhh got it. Thank you so much!!
Maria QureshiAuthor Commented:
Thank you so much I understand now
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.