Graphing Equations

Lets revisit high school and think about those tiny screens a lot of us had to use.

We may recreate those kinds of plots, of mathematical functions, quite straightforwardly.


In [ ]:
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt

Lets define a domain from -5 to 5, of 100 points, and plot some XY curves that show some functions.


In [ ]:
domain = np.linspace(-5.0,5.0,100)

In [ ]:
y = np.power(domain, 2)

In [ ]:
%matplotlib inline  
# "magic" command telling Jupyter NB to embed plots

# always label and title your plot, at minimum
plt.xlabel("X")
plt.ylabel("Y")
plt.title("Parabolic Curve")
p = plt.plot(domain, y)

In [ ]:
x3 = np.power(domain, 3)

In [ ]:
plt.xlabel("X")
plt.ylabel("Y")
plt.title("X to the 3rd Power")
p = plt.plot(domain, x3)

In [ ]:
def poly(x):
    return (x - 3) * (x + 5) * (x - 1) * x

In [ ]:
Poly = np.vectorize(poly)

In [ ]:
y = Poly(domain)

In [ ]:
plt.xlabel("X")
plt.ylabel("Y")
plt.title("4th Degree Polynomial")
plt.grid()
p = plt.plot(domain, y)

In [ ]:
y0 = np.sin(domain)
y1 = np.cos(domain)

In [ ]:
plt.xlabel("X")
plt.ylabel("Y")
plt.title("Sine & Cosine")
plt.grid()
plt.plot(domain, y0, color = "orange", label="Sine")
plt.plot(domain, y1, color = "green", label="Cosine")
p = plt.legend()

Now that we've plotted some data, lets organize the data into a data table, or "data frame" to be more precise. Pandas is all about the DataFrame object.

Our domain is a 1-D ndarray lets remember. As such, we may turn it into a Series, which is the one dimensional equivalent in pandas. DataFrames are 2-dimensional and above.


In [ ]:
domain.shape

In [ ]:
col0 = pd.Series(domain)
col1 = pd.Series(np.power(domain,2))
col2 = pd.Series(x3)
col3 = Poly(domain)

datadict = {"Input":col0, "Parabola":col1, "3rd Power":col2, "Polynomial":col3}
df = pd.DataFrame(datadict, columns = ["Input", "Parabola", "3rd Power", "Polynomial"])

Without the columns argument, there's no guarantee that datadict will gives us the left-to-right column order we desire.


In [ ]:
df.head()

Here we're starting to introduce how data may be selected by numeric indexes, yes, but also by labels.


In [ ]:
df.loc[:,"3rd Power"].head()

In [ ]:
df.loc[:10,["Input", "3rd Power"]]  # rows 0-10 inclusive, two columns