```python
import matplotlib.pyplot as plt
import plotly as py
import plotly.graph_objs as go
import numpy as np
import math
import ipywidgets as widgets
from IPython.display import display, Math, Latex, HTML
from astropy.table import Table, Column
from ipywidgets import interact, interactive

py.offline.init_notebook_mode(connected=True)

%matplotlib inline

font = {'family' : 'sans-serif',
        'weight' : 'normal',
        'size'   : 14}

plt.rc('font', **font)



#code for User_Graph

class User_Graph:
    
    def __init__(self,num_points,x_dim,y_dim):
        
        #how many points do you want the user to input?
        self.num_points = num_points
        
        #dimensions of the graph
        self.x_dim = x_dim
        self.y_dim = y_dim
        
        #create lists for x and y
        self.x_list = []
        self.y_list = []
        
    
    def user_input(self):
        #ask users to input data points
        try:
            #get x values
            x_list = input("Enter the x-values as a list of the form [a,b,c,d,...]: " )
            x_list = x_list.strip(']')
            x_list = x_list.strip('[')
            x_list = x_list.split(',')
            for i in x_list:
                x_val = float(i)
                self.x_list.append(x_val)
            
            #get y values
            y_list = input("Enter the y-values as a list of the form [a,b,c,d,...]: " )
            y_list = y_list.strip(']')
            y_list = y_list.strip('[')
            y_list = y_list.split(',')
            for i in y_list:
                y_val = float(i)
                self.y_list.append(y_val)
            
        except:
            print("Entries must be real numbers!")
            self.user_input()
        
        if len(self.x_list) != len(self.y_list):
            print("Must have an equal number of x and y values! Try Again.")
            self.user_input()
    
    def make_graph(self, title):
        #make a graph out of the available data points
        
        layout = go.Layout(
            title = title,
            yaxis = dict(
                title="<i>Y</i> Values",
                range = [self.y_dim[0], self.y_dim[1]]
            ),
            xaxis = dict(
                title="<i>X</i> Values",
                range = [self.x_dim[0], self.x_dim[1]]
            ),
        )

        trace1 = go.Scatter(
            x = self.x_list,
            y = self.y_list,
            mode = "lines+markers",
            name = "extend plot",
            line=dict(
                color = ('rgb(22, 96, 167)'),
                shape = "spline",
                dash = "dot"
            )
    
        )

        fig = go.Figure(data = [trace1], layout = layout)
        py.offline.iplot(fig)
        
    def run_it(self):
        # provide a title
        title = input("Give the graph a title: ")
        self.user_input()
        self.make_graph(title)
            

#code for Double_Graph


class Double_Graph:
    
    def __init__(self,num_points):
        
        #how many points do you want the user to input?
        self.num_points = num_points
        
        #create lists for x and y
        self.x1_list = []
        self.y1_list = []
        self.x2_list = []
        self.y2_list = []
    
    def function_1(self):
        #ask users to input data points
        print("Enter data points for Quentin.")
        try:
            #get x values
            x1_list = input("Enter the x-values as a list of the form [a,b,c,d,...]: " )
            x1_list = x1_list.strip(']')
            x1_list = x1_list.strip('[')
            x1_list = x1_list.split(',')
            for i in x1_list:
                x1_val = float(i)
                self.x1_list.append(x1_val)
                
            #get y values
            y1_list = input("Enter the y-values as a list of the form [a,b,c,d,...]: " )
            y1_list = y1_list.strip(']')
            y1_list = y1_list.strip('[')
            y1_list = y1_list.split(',')
            for i in y1_list:
                y1_val = float(i)
                self.y1_list.append(y1_val)
            
        except:
            print("Entries must be real numbers!")
            self.function_1(data_num)
            
    def function_2(self):
        #ask users to input data points
        print("Now, enter data points for Isabelle")
        try:
            x2_list = input("Enter the x-values as a list of the form [a,b,c,d,...]: " )
            x2_list = x2_list.strip(']')
            x2_list = x2_list.strip('[')
            x2_list = x2_list.split(',')
            for i in x2_list:
                x2_val = float(i)
                self.x2_list.append(x2_val)
                
            #get y values
            y2_list = input("Enter the y-values as a list of the form [a,b,c,d,...]: " )
            y2_list = y2_list.strip(']')
            y2_list = y2_list.strip('[')
            y2_list = y2_list.split(',')
            for i in y2_list:
                y2_val = float(i)
                self.y2_list.append(y2_val)
            
        except:
            print("Entries must be real numbers!")
            self.function_2(data_num)
    
    def make_graph(self, title):
        #make a graph out of the available data points
        
        layout = go.Layout(
            title = title,
            yaxis = dict(
                title="<i>Y</i> Values",
                range = [0,15]
            ),
            xaxis = dict(
                title="<i>X</i> Values",
                range = [0,8]
            ),
        )

        trace1 = go.Scatter(
            x = self.x1_list,
            y = self.y1_list,
            mode = "lines+markers",
            name = "Quentin",
            line=dict(
                color = ('rgb(22, 96, 167)'),
                dash = "dot"
            )
    
        )
        
        trace2 = go.Scatter(
            x = self.x2_list,
            y = self.y2_list,
            mode = "lines+markers",
            name = "Isabelle",
            line=dict(
                color = ('rgb(205, 12, 24)'),
                dash = "dot"
            )
    
        )

        fig = go.Figure(data = [trace1,trace2], layout = layout)
        py.offline.iplot(fig)
        
    def run_it(self):
        title = input("Give the graph a title: ")
        self.function_1()
        self.function_2()
        self.make_graph(title)

'''Above, we are importing all the necessary modules in order to run the notebook. 
Numpy allows us to define arrays of values for our variables to plot them
matplotlib is what we use to create the figures
the display and widgets are to make the notebook look neat
'''

HTML('''
<form action="javascript:code_toggle()"><input type="submit" id="toggleButton" value="Show Code"></form>''')
        
    
```









<form action="javascript:code_toggle()"><input type="submit" id="toggleButton" value="Show Code"></form>



<h1><center>  Understanding the Relationship Between Two Variables Using Graphs </center></h1>
***





<center>*GIF taken from https://giphy.com/gifs/reaction-BmmfETghGOPrW, June 11th, 2018.*</center>

***

> ## **Introduction**

Humans are constantly interacting with other people, objects, and things. The outcomes of these interactions depend on the many **variables** that may exist in the given situation. A variable can be described as something that is free to change, and the way in which it changes can influence the outcome of a certain event. Consider the following example:

>### **Example**
Say you have a job that pays you an hourly wage of \$10 per hour. Your wage then determines how much money you will make if you were to work for 5 hours ($5$ hours $\times$ $\$10$ per hour = $\$50$). If your wage were to change to \$12 per hour, the amount of money you would make in 5 hours would change as well ($5$ hours $\times$ $\$12$ per hour = $\$60$). Your wage is therefore a *variable* that determines how much money you are going to make. 

Many of the choices we make every day require us to understand how certain variables will affect the results of our actions. The better our understanding, the better we are at reaching our desired outcome! A useful tool to help increase our understanding of how variables affect results is called a **graph**, which is simply a visual representation of the relationship between variables.

This lesson will focus on how we can use graphs to improve our understanding of how two variables interact with each other. While there are many different types of graphs, we will focus on line graphs in this lesson to demonstrate the relationship between variables within **linear functions**. We will go through:
- how to create a graph of a linear function 
- how to analyze the important characteristics of a graph, including slope, $x$ and $y$ intercepts

Take a look at the graph shown below. This is a line graph that shows the relationship between a persons wage and how much money they will earn if they were to work for 5 hours. You can use the toolbar at the top of the graph to manipulate it.


```python
wages = np.linspace(0,50,11)
money = []
for item in wages:
    y = item*5
    money.append(y)

layout = go.Layout(
    title = "Money Earned After Working 5 Hours",
    yaxis = dict(
        title="Money Earned (Dollars)"
    ),
    xaxis = dict(
        title="Wage (Dollars per hour)",
    ),
)

trace1 = go.Scatter(
    x = wages,
    y = money,
    mode = "lines+markers",
    name = "Plot 1",
    line=dict(
        color = ('rgb(205, 12, 24)'),
        shape = "spline",
        dash = "dot"
    )
    
)

fig = go.Figure(data = [trace1], layout = layout)
py.offline.iplot(fig)
```

<center> Figure 1: This graph shows the relationship between a persons wage, and the money they will make after working for 5 hours. </center>


***

> ## **Background**

### **Algebraic Equations**

When dealing with mathematics, the relationship between certain variables can be written as an **algebraic equation**. In an algebraic equation, letters are used to represent variables that may have any value. For example, the relationship between wage and the amount of money made after working for 5 hours, as described above, can be shown as follows:

\begin{equation} 
\label{1}
\rm MONEY\: MADE = 5\: HOURS \times WAGE
\end{equation}

here is the same equation, only now it is expressed algebraicly:

\begin{equation} 
\label{2}
M = 5W
\end{equation}

In the algebraic equation, each letter represents a variable. The $W$ corresponds to wage, and the $M$ corresponds to the resulting amount of money made.

When creating an algebraic expression, the letter that you assign to a variable really doesn't matter. However, in mathematics it is most common to select the letters $x$ and $y$ to represent equations with two different variables.
In most cases that you will see in class, an equation will be given to you, and there may not be any meaning behind what each variable represents. Take a look at some of the examples below to get a better grasp on how an algebraic equation may look.
***
>#### **Examples**

\begin{equation}
y = 3x + 9
\end{equation}

\begin{equation}
y = \frac{x}{4} + 3
\end{equation}

\begin{equation}
y = \frac{x + 7}{2}
\end{equation}
***

### **Linear Functions**

Algebraic equations are sometimes called **functions**, particularly when they are used to create a graph. A **linear function** has the form:

\begin{equation} 
y = mx + b
\end{equation}


where both the $x$ and $y$ variables in the equation are of degree 1 or 0, and $m$ and $b$ are *constants*. A *constant* is a number in an equation that does not change, or in other words, is not a variable. A constant can be positive, negative, a fraction or a whole number.

Take another look at the equations shown in the section above, these are all linear functions! When a linear function is used to create a graph, the graph will look like a straight line (hence the name *linear* function).

In this lesson we will only be looking at linear functions and their corresponding graphs.

Use the section below to better your understanding of the difference between a *linear* function, and a *non-linear* function.






```python
def lin_or_non(val):
    if val == "Linear":
        display(Latex("Correct!"))
        display(Latex("This equation has no exponent on either variable, and is therefore linear."))
    elif val == "Non-Linear":
        display(Latex("Try Again!"))

display(Latex("Is this function linear or non-linear?"))
display(Math('y = 12x -3'))
    
interact(lin_or_non, val = widgets.Dropdown(options=[' ', 'Linear', 'Non-Linear'],value = ' ',description = 'Choose One:',disabled = False));

```


```python
def lin_or_non(val):
    if val == "Linear":
        display(Latex("Try Again!"))
    elif val == "Non-Linear":
        display(Latex("Right on!"))
        display(Latex("Because $x$ is raised to the power of 2, this function is not linear."))

display(Latex("Is this function linear or non-linear?"))
display(Math('y = 2x^2 + 17'))
    
interact(lin_or_non, val = widgets.Dropdown(options=[' ', 'Linear', 'Non-Linear'],value = ' ',description = 'Choose One:',disabled = False));

```


```python
def lin_or_non(val):
    if val == "Linear":
        display(Latex("Try Again!"))
    elif val == "Non-Linear":
        display(Latex("Way to go!"))
        display(Latex("This graph does not resemble a straight line, therefore, it is not linear."))
        display(Latex("The equation for this function is:"))
        display(Math("y = x^2"))

display(Latex("Is this function linear or non-linear?"))

x = np.linspace(-10,10,21)
y = []
for num in x:
    y_val = num**2
    y.append(y_val)

layout = go.Layout(
    title = "<i>Y</i> as a Function of <i>X</i>",
    yaxis = dict(
        title="<i>Y</i> Values"
    ),
    xaxis = dict(
        title="<i>X</i> Values"
    ),
)

trace1 = go.Scatter(
    x = x,
    y = y,
    mode = "lines+markers",
    name = "Plot 1",
    line=dict(
        color = ('rgb(22, 96, 167)'),
        shape = "spline",
        dash = "dot"
    )
    
)

fig = go.Figure(data = [trace1], layout = layout)
py.offline.iplot(fig)

interact(lin_or_non, val = widgets.Dropdown(options=[' ', 'Linear', 'Non-Linear'],value = ' ',description = 'Choose One:',disabled = False));

```


```python
def lin_or_non(val):
    if val == "Linear":
        display(Latex("Good job!"))
        display(Latex("This graph is a straight line, and is therefore linear."))
        display(Latex("The equation for this function is:"))
        display(Math("y = -x - 6"))
    elif val == "Non-Linear":
        display(Latex("Try Again!"))

display(Latex("Is this function linear or non-linear?"))

x = np.linspace(-10,4,10)
y = []
for num in x:
    y_val = -num - 6
    y.append(y_val)
    
    
layout = go.Layout(
    title = "<i>Y</i> as a Function of <i>X</i>",
    yaxis = dict(
        title="<i>Y</i> Values"
    ),
    xaxis = dict(
        title="<i>X</i> Values"
    ),
)

trace1 = go.Scatter(
    x = x,
    y = y,
    mode = "lines+markers",
    name = "Plot 1",
    line=dict(
        color = ('rgb(100, 100, 100)'),
        shape = "spline",
        dash = "dot"
    )
    
)

fig = go.Figure(data = [trace1], layout = layout)
py.offline.iplot(fig)
    
    
interact(lin_or_non, val = widgets.Dropdown(options=[' ', 'Linear', 'Non-Linear'],value = ' ',description = 'Choose One:',disabled = False));

```

Now that we know how linear functions can look, lets talk about how we can represent them on a graph!

## **Creating a Graph**

### **Independent and Dependent Variables**

When you want to express a linear function with two variables on a graph, it is important to first establish the **independent variable** and the **dependent variable**. The *independent* variable can be thought of as the variable that is subject to change in order to produce different results, whereas the *dependent* variable can be thought of as the variable whose value *depends* on the independent variable. The difference between the two types of variables may be difficult to understand at first, so here is an example that may help.
***
>#### **Example**
Let's say you are conducting an experiment to measure how many push-ups you can do in a certain amount of time. There are two variables at play here: the amount of time you are given to do push-ups, and the amount of push-ups you actually do in that time. In this scenario, you are able to freely change how much time you have to do push-ups. You could give yourself 30 seconds, an hour, or a day! Therefore, *time is the independent variable*. 
On the other hand, the amount of push-ups you are actually able to do *depends* on the amount of time you have to do them. Therefore, the *dependent variable is the number of push-ups you do*. 
***
When you are given an equation with two variables ($x$ and $y$), it is common to establish $x$ as the independent variable, and $y$ as the dependent variable.
***



```python
def wage(val):
    if val == "W (Wage)":
        display(Latex("Correct!"))
        display(Latex("The amount of money you make depends on your wage, and your wage is free to change. It is therefore the independent variable."))
    elif val == "M (Money Made)":
        display(Latex("Try Again!"))

display(Latex("From the wage example shown in the section above, which variable do you think is the independent variable?"))
display(Math('M = 5W'))
    
interact(wage, val = widgets.Dropdown(options=[' ', 'W (Wage)', 'M (Money Made)'],value = ' ',description = 'Choose One:',disabled = False));

```

***
### **The Cartesian Plane**

Once we have established the independent and dependent variables in our linear function, we are ready to set up a graph of their relationship. When graphing two variables, we use what is called the **Cartesian Plane**. You have probably seen a graph on the Cartesian Plane before, but here is what it looks like:


```python
layout = go.Layout(
    title = "The Cartesian Plane",
    yaxis = dict(
        title="<i>Y</i> Axis",
        range = [-10,10],
        dtick = 1.00
    ),
    xaxis = dict(
        title="<i>X</i> Axis",
        range = [-10,10],
        dtick = 1.00
    ),
)

trace1 = go.Scatter(
    x = [5,-5,-5,5],
    y = [5,5,-5,-5],
    mode = "marker+text",
    name = "Plot 1",
    text=['Quadrant 1', 'Quadrant 2', 'Quadrant 3', 'Quadrant 4'],
    textposition='bottom'
)

fig = go.Figure(data = [trace1], layout = layout)
py.offline.iplot(fig)
```

<center> Figure 2: This is the standard layout for the Cartesian Plane, where you will plot all linear functions. The point where $x = 0$ and $y = 0$ is called the origin. </center>

On the Cartesian Plane, there are two **axes**: the $x$ axis (horizontal), and the $y$ axis (vertical). When we want to graph a certain relationship, we label the $y$ axis with the dependent variable, and the $x$ axis with the independent variable. When you aren't given a specific name for what the independent and dependent variable represent, it is okay to just label the $x$ axis "$X$" and the $y$ axis "$Y$". 

When giving the graph a title, it is customary to describe what is being shown on the graph. Your title should provide the observer with a concise understanding of what is being shown. In most cases, you will describe the dependent variable as a function of the independent variable, or "$Y$ as a Function of $X$". 

The Cartesian Plane consists of four **quadrants**, or sections. The top right section, where both $x$ and $y$ are positive (+,+), is quadrant 1. Then going counter-clockwise, quadrant 2 is the section where $x$ is negative and $y$ is positive (-,+), in quadrant 3 both $x$ and $y$ are negative (-,-), and in quadrant 4, $x$ is positive and $y$ is negative (+,-).

Now that we have established how a graph should look, lets talk about how we generate the values that we want to plot on the graph!

### **Finding the Data Points**

The data points that are shown on a graph don't just appear out of nowhere; they can come from scientific observations, measurements, or mathematical relationships. When graphing on the Cartesian Plane, each data point has an $x$ value and a $y$ value (sometimes called an $x$ coordinate and a $y$ coordinate), and these values are what determine where the point will appear on the graph.

The best way to describe how we find the $x$ value and $y$ value for each data point is through an example.

***
> #### **Example**

Suppose you are given a linear equation such as $y = 3x + 2$, and you are asked to plot a graph with ten data points, starting from $x = 1$ up to $x = 5$.

To begin generating data points, the first thing you need to do is pick ten unique $x$ values between $1$ and $5$. 

For simplicity, let's pick 1, 2, 3, 4, and 5.

\begin{array}{| c | c |}
\hline
  X\: Value & Y\: Value  \\
  \hline
  1 & ? \\\hline
  2 & ? \\\hline
  3 & ? \\\hline
  4 & ? \\\hline
  5 & ? \\\hline
\end{array}

Now we need to find the value of $y$ at each one of the chosen values for $x$. We can do this by plugging our chosen $x$ values into the equation that we have been given: $y = 3x + 2$.

Lets take our first $x$ value, 1, and plug it into the equation

\begin{equation} 
y = 3x + 2
\end{equation}
\begin{equation}
x = 1
\end{equation}

So, plugging in this value:

\begin{equation} 
y = 3(1) + 2
\end{equation}
\begin{equation}
y = 3 + 2
\end{equation}
\begin{equation}
y = 5
\end{equation}

We now have the value of our first data point! When $x = 1$, $y = 5$. We would write this data point down at (1,5). Lets fill in the table so we can keep track of the data points we find:

\begin{array}{| c | c |}
\hline
  X\: Value  & Y\: Value  \\
  \hline
  1 & 5 \\\hline
  2 & ? \\\hline
  3 & ? \\\hline
  4 & ? \\\hline
  5 & ? \\\hline
\end{array}


In the section below, use the above method to find the remaining $y$ values.


```python

###### Create input boxes to find remaining values

def find_y(xval):
    try:
        
        y = 3*(xval) + 2
    
        ans = input("When x = " + str(xval) + ", y = ")
        answer = int(ans)
        
        
        if answer == y:
            print("Correct!")
        
            #show updated table
            x_col = [1,2,3,4,5]
            y_col = []
            for i in range(1,xval + 1):
                yval = 3*(i) + 2
                y_col.append(yval)
            
            #fill in empty spots with question marks
            if len(y_col) < 5:
                extra = 5 - len(y_col)
                for j in range(1, extra + 1):
                    y_col.append("?")
                
            #show the table        
            t = Table([x_col,y_col], names = ("X Value","Y Value"))
            print(t)
        
        else:
            print("Incorrect, try again!")
            print("Remember, the equation is: y = 3x + 2")
            find_y(xval)
        
        
        
        
    except:
        print("Answer must be an integer! Try again.")
        find_y(xval)
        
    
                
for i in range(2,6):
    find_y(i)
                
```

Good job! Now that we have all of the data points, we are ready to plot them on the graph.


### **Plotting the Data Points on the Graph**

To plot our data points on a graph, we will need our Cartesian Plane, with the $x$ axis and $y$ axis labelled as follows:


```python
layout = go.Layout(
    title = "<i>Y</i> as a Function of <i>X</i>",
    yaxis = dict(
        title="<i>Y</i> Values",
        range = [-1,20],
        dtick = 1.00
    ),
    xaxis = dict(
        title="<i>X</i> Values",
        range = [-1,10],
        dtick = 1.00
    )
)

trace1 = go.Scatter(
    x = [],
    y = [],
    mode = "lines+markers",
    name = "Plot 1",
    line=dict(
        color = ('rgb(205, 12, 24)'),
        shape = "spline",
        dash = "dot"
    )
    
)

fig = go.Figure(data = [trace1], layout = layout)
py.offline.iplot(fig)

```


<div id="b454ea3d-7a8b-4f20-96e3-0bc6f0c5ad18" style="height: 525px; width: 100%;" class="plotly-graph-div"></div>


<center> Figure 3: The layout of our graph. As all of our data points have positive $x$ and $y$ values, we only need to focus on the first quadrant (top right corner).</center>

Now, using our chart of data points, lets plot our first point. The $x$ value of our first point is 1, and the $y$ value is 5. So lets find the point on the graph where $x=1$ and $y=5$ and mark this point



```python
layout = go.Layout(
    title = "<i>Y</i> as a Function of <i>X</i>",
    yaxis = dict(
        title="<i>Y</i> Values",
        range = [-1,20],
        dtick = 1.00
    ),
    xaxis = dict(
        title="<i>X</i> Values",
        range = [-1,10],
        dtick = 1.00
    )
)

trace1 = go.Scatter(
    x = [1],
    y = [5],
    mode = "lines+markers",
    name = "Plot 1",
    line=dict(
        color = ('rgb(205, 12, 24)'),
        shape = "spline",
        dash = "dot"
    )
    
)

fig = go.Figure(data = [trace1], layout = layout)
py.offline.iplot(fig)
```

<center> Figure 4: Here is the location of our first data point.</center>

That is your first data point!

In the section below, use the above methods to create a graph and plot all of the data points.

\begin{array}{| c | c |}
\hline
  X\: Value & Y\: Value \\
  \hline
  1 & 5 \\\hline
  2 & 8 \\\hline
  3 & 11 \\\hline
  4 & 14 \\\hline
  5 & 17 \\\hline
\end{array}


```python
g = User_Graph(5,[-1,10],[-1,20])
g.run_it()
```

    Give the graph a title: Y as a Function of X
    Enter the x-values as a list of the form [a,b,c,d,...]: [1,2,3,4,5]
    Enter the y-values as a list of the form [a,b,c,d,...]: [5,8,11,14,17]



<div id="d202420c-7c22-4d08-81e8-5b2705f68d1b" style="height: 525px; width: 100%;" class="plotly-graph-div"></div>


If done correctly, your graph should look something like this:


```python
layout = go.Layout(
    title = "<i>Y</i> as a Function of <i>X</i>",
    yaxis = dict(
        title="<i>Y</i> Values",
        range = [-1,20]
    ),
    xaxis = dict(
        title="<i>X</i> Values",
        range = [-1,10]
    )
)

trace1 = go.Scatter(
    x = [1,2,3,4,5],
    y = [5,8,11,14,17],
    mode = "lines+markers",
    name = "Plot 1",
    line=dict(
        color = ('rgb(205, 12, 24)'),
        shape = "spline",
        dash = "dot"
    )
    
)

fig = go.Figure(data = [trace1], layout = layout)
py.offline.iplot(fig)
```


<div id="e5fe49a6-50df-46ff-b035-4c43ce927cf8" style="height: 525px; width: 100%;" class="plotly-graph-div"></div>


If the graph you made looks significantly different, try running the code segment again to re-try!


Now that you know how to create a graph, let's talk about some of the important things that a graph can tell us.

***

**Features of a Linear Graph: Intercepts**

A graph can tell us some interesting details about how two variables relate to one another in a given scenario. Two such details are called the $x$ intercept and the $y$ intercept. The $x$ intercept is the point on the graph where the function crosses the $x$ axis, and similarily, the $y$ intercept is the point where the function crosses the $y$ axis. 
- At the $x$ intercept, $y = 0$
- At the $y$ intercept, $x = 0$.

This may seem like trivial information, but depending on what the graph is representing, the position of the $x$ and $y$ intercepts can prove to be very informative.

***
> ### **Example**

Let's say you have a job as a taxi driver and your wage is approximately \$15 per hour. However, before you start your shift you need to fill your car with \$30 worth of gas. Knowing this, how many hours do you have to work in order to start making money?

The equation for this scenario can be represented as:

\begin{equation} 
y = 15x - 30
\end{equation}

where $x$ represents how many hours you have worked, and $y$ represents the amount of money you have made.

Take a look at the graph below that shows the equation.


```python
hours = np.linspace(0,20,11)
money = []

for item in hours:
    y = item*15 - 30
    money.append(y)

    
layout = go.Layout(
    title = "Amount of Money Earned as a Function of Hours Worked",
    yaxis = dict(
        title="Money Earned (Dollars)"
    ),
    xaxis = dict(
        title="Hours Worked"
    ),
)

trace1 = go.Scatter(
    x = hours,
    y = money,
    mode = "lines+markers",
    name = "Plot 1",
    line=dict(
        color = ('rgb(22, 96, 167)'),
        shape = "spline",
        dash = "dot"
    )
    
)

fig = go.Figure(data = [trace1], layout = layout)
py.offline.iplot(fig)

```


<div id="4cf6c7a2-9258-4a49-8c13-a0c5bfca72f2" style="height: 525px; width: 100%;" class="plotly-graph-div"></div>


<center> Figure 5: The amount of money a taxi driver will earn after having to pay for $30 worth of gas.

To make money, your value for *Money Earned* needs to be greater than zero, and the point where this begins is the $x$ intercept! By identifying the $x$ intercept (the data point on the graph at (2,0)), you now know the minimum amount of time you need to work (2 Hours) in order for you to start making money after spending \$30 to fill up with gas.

While this particular example shows the significance of the $x$ intercept, there are many other scenarios where it may be useful to identify the $y$ intercept on a graph as well.

***

To find the value of the $x$ intercept without using a graph, we can use the linear equation:

\begin{equation} 
y = 15x - 30
\end{equation}

and set the value of $y$ to zero, then solve for $x$. It would look something like this:

\begin{equation} 
(0) = 15x - 30
\end{equation}

\begin{equation} 
30 = 15x
\end{equation}

\begin{equation} 
\frac{30}{15} = x
\end{equation}

\begin{equation} 
2 = x
\end{equation}

As before, the x$$ intercept is found to be at (2,0). The same process can be used to find the value of the $y$ intercept, only instead of setting $y$ to zero, we set $x$ so zero, as follows:

\begin{equation} 
y = 15(0) - 30
\end{equation}

\begin{equation} 
y = -30
\end{equation}

So now we see that the $y$ intecept for this particular equation is (0,-30). 

You may have noticed that the $y$ coordinate of the $y$ intercept is actually just the $b$ value in an equation of the form $y = mx + b$!

> ## **Note**
If a graph is a perfectly straight horizontal line (slope = 0), the function may not have an $x$ intercept. If the function is a perfectly vertical line (slope is undefined), the graph may not have a $y$ intercept. In all other cases, a graph will have both an $x$ and $y$ intercept. However, the range in which you have plotted may not show them.

***



**Features of a Linear Graph: Slope**

Another important characteristic of a linear function is **slope**. The *slope* of a function can be thought of as the steepness or the angle of the function as it appears on a graph. What it really represents is the rate at which the dependent variable changes with respect to the independent variable. 

That may sound complicated at first, but here is an example that might make it easier to understand what slope is.

***

> ### **Example**

Say you have been given a set of data points describing how far a car has travelled in a given amount of time:


\begin{array}{| c | c |}
\hline
  Time(seconds) & Distance Travelled(meters) \\
  \hline
  0 & 0 \\\hline
  20 & 400 \\\hline
  40 & 800 \\\hline
  60 & 1200 \\\hline
  80 & 1600 \\\hline
  100 & 2000 \\\hline
  120 & 2400 \\\hline
  140 & 2800 \\\hline
  160 & 3200 \\\hline
  180 & 3600 \\\hline
  200 & 4000 \\\hline
\end{array}


You are then asked to find out how fast the car was travelling. Let's set up a graph of this data.






```python
time = np.linspace(0,200,11)
distance = np.linspace(0,4000,11)

layout = go.Layout(
    title = "Distance Travelled as a Function of Time",
    yaxis = dict(
        title="Distance Travelled (Meters)"
    ),
    xaxis = dict(
        title="Time (Seconds)"
    ),
)

trace1 = go.Scatter(
    x = time,
    y = distance,
    mode = "lines+markers",
    name = "Plot 1",
    line=dict(
        color = ('rgb(22, 96, 167)'),
        shape = "spline",
        dash = "dot"
    )
    
)

fig = go.Figure(data = [trace1], layout = layout)
py.offline.iplot(fig)
```


<div id="fe6b9d73-b801-443a-8507-05f26b6ccac6" style="height: 525px; width: 100%;" class="plotly-graph-div"></div>


<center> Figure 6: This graph shows the distance a car travels after a given amount of time.</center>

We want to find how fast the car is travelling. Knowing that speed is a measurement of distance over time, let's use the total distance travelled and the total amount of time to find the average speed of the car.
From the graph, we can see that the car initially started at position 0 meters, and travelled a total of 4000 meters. We also know that the car started moving at time 0 seconds, and stopped moving after 200 seconds. Using this information, we can set up the following equations to find the speed of the car:

\begin{equation}
Speed = \frac{Total\: Distance}{Total\: Time}
\end{equation}

and we know that:

\begin{equation}
Total\: Distance = Final\: Distance - Initial\: Distance
\end{equation}
\begin{equation}
Total\: Time = Final\: Time - Initial\: Time
\end{equation}

so:

\begin{equation} 
Speed = \frac{Final\: Distance - Initial\: Distance}{Final\: Time - Initial\: Time}
\end{equation}

\begin{equation} 
Speed = \frac{4000m - 0m}{200s - 0s}
\end{equation}

\begin{equation}
Speed = \frac{4000m}{200s}
\end{equation}

\begin{equation} 
 Speed = 20 m/s
\end{equation}



From the equations above, we can see that the car travels 20 meters every second. While it may not be obvious, we just found the slope of the graph! By finding the average speed of the car, we identified the rate at which the dependent variable (Distance Travelled) changes with respect to the independent variable (Time).

***


When looking at a graph, the general equation used to find the slope of a linear function from point ($x_i$,$y_i$) to ($x_f$,$y_f$) is given as follows:

\begin{equation} 
Slope = \frac{y_f - y_i}{x_f - x_i}
\end{equation}

where the subscript $f$ stands for *final*, and the $i$ stands for *initial*.

When looking at a linear function that has the form:
\begin{equation} 
y = mx + b
\end{equation}
the value of the slope is the same as the value of the coefficient $m$.

Use the section below to see the effect that slope can have on a function.


```python
def slider(slope):
    
    x_list = [0,1,2,3,4,5,6,7,8,9,10]
    y_list = []
    for i in x_list:
        y_val = slope*(i)
        y_list.append(y_val)
    
    
    layout = go.Layout(
        showlegend = True,
        title ='y = ' + str(slope) + 'x',
        yaxis = dict(
            title="<i>X</i> Values",
            range = [-1,20]
        ),
        xaxis = dict(
            title="<i>Y</i> Values",
            range = [-1,20]
        ),
    )

    trace1 = go.Scatter(
        x = x_list,
        y = y_list,
        mode = "lines+markers",
        name = str('$y = ' + str(slope) + 'x$'),
        line=dict(
            color = ('rgb(22, 96, 167)'),
            shape = "spline",
            dash = "dot"
        )
    
    )

    fig = go.Figure(data = [trace1], layout = layout)
    py.offline.iplot(fig, filename = 'show-legend')
    
interactive(slider, slope=(0,5,0.5))


```


<p>Failed to display Jupyter Widget of type <code>interactive</code>.</p>
<p>
  If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean
  that the widgets JavaScript is still loading. If this message persists, it
  likely means that the widgets JavaScript library is either not installed or
  not enabled. See the <a href="https://ipywidgets.readthedocs.io/en/stable/user_install.html">Jupyter
  Widgets Documentation</a> for setup instructions.
</p>
<p>
  If you're reading this message in another frontend (for example, a static
  rendering on GitHub or <a href="https://nbviewer.jupyter.org/">NBViewer</a>),
  it may mean that your frontend doesn't currently support widgets.
</p>



<center> Figure 7: This graph shows the way in which slope can affect the shape of a function. The larger the slope, the "steeper" the graph is.</center>

When dealing with a single linear function, the slope will be constant across the entire graph. However, if a graph consists of more than one linear function, the slope may change at some point. 
Take a look at the following graph to see what a graph with more than one linear function may look like:


```python
x1 = [0,1,2,3,4,5]
y1 = []

for num in x1:
    y1_val = num + 1
    y1.append(y1_val)
    
x2 = [6,7,8,9,10]
y2 = []

for num in x2:
    y2_val = 3*(num) - 9
    y2.append(y2_val)

x_list = x1 + x2
y_list = y1 + y2
    
layout = go.Layout(
    title = "<i>Y</i> as a Function of <i>X</i>",
    yaxis = dict(
        title="<i>Y</i> Values"
    ),
    xaxis = dict(
        title="<i>X</i> Values"
    ),
)

trace1 = go.Scatter(
    x = x_list,
    y = y_list,
    mode = "lines+markers",
    name = "Plot 1",
    line=dict(
        color = ('rgb(22, 96, 167)'),
        dash = "dot"
    )
    
)

fig = go.Figure(data = [trace1], layout = layout)
py.offline.iplot(fig)

    
```


<div id="e522e20a-83b8-4718-b251-ac32dc996297" style="height: 525px; width: 100%;" class="plotly-graph-div"></div>


<center> Figure 8: On this plot, notice the distinct change in steepness at $x = 5$. The slope increases at this point.</center>

As you can see above, the graph has two distinct sections: one from $x = 0$ to $x = 5$, and the other from $x = 5$ to $x = 10$. As we have seen from the equations of linear functions, we know that the slope of one function has a constant value. Because these sections each have a different slope, they must represent two distinct linear functions.

You may be wondering how one graph can show two distinct functions at the same time, but consider our example of the car shown above. What if halfway through the trip, the car began to move at a faster speed? If this were the case, the slope of the graph would increase at the halfway point, much like the graph shown above.

## **Questions**

The following section is meant to test your understanding and improve your familiarity with the concepts we have gone over in this lesson.


### **Question 1.**

Use the following table of data points describing the motion of a car driving to a destination and complete the following tasks:
- Plot a graph (be sure to label the axes, and to give the graph a title!)
- Find the slope of the graph
- Find the $x$ and $y$ intercept
- Derive the equation for the linear function

\begin{array}{| c | c |}
\hline
  Time\: (seconds) & Distance\: Away\: from\: Destination\: (meters) \\
  \hline
  0 & 10 \\\hline
  1 & 8 \\\hline
  2 & 6 \\\hline
  3 & 4 \\\hline
  4 & 2 \\\hline
  5 & 0 \\\hline
  6 & -2 \\\hline
  7 & -4 \\\hline
  8 & -6 \\\hline
  9 & -8 \\\hline
\end{array}


Use the section below to create the graph:


```python
q1 = User_Graph(10, [-1,10], [-10,12])
q1.run_it()

```

    Label the X-Axis: Time (seconds)
    Label the Y-Axis: Distance From Destination (meters)
    Give the graph a title: Driving Somewhere
    Enter the x-values as a list of the form [a,b,c,d,...]: [0,1,2,3,4,5,6,7,8,9]
    Enter the y-values as a list of the form [a,b,c,d,...]: [10,8,6,4,2,0,-2,-4,-6,-8]



<div id="9f29ab44-700e-4899-a52b-17d0fd9d3623" style="height: 525px; width: 100%;" class="plotly-graph-div"></div>


Use the following sections to answer some questions about the graph you just made:


```python
def ask_slope():
    slope = input("What is the value of the slope of the above graph? (Enter your answer as an integer) ")
    
    if slope in ["-2"]:
        display(Latex("Good stuff!"))
    else:
        display(Latex("Try Again!"))
        display(Latex("Remember, the equation for the slope of a line is:"))
        display(Math("Slope = (y_f - y_i)/(x_f - x_i)"))
        ask_slope()
        
ask_slope()
```


```python
def ask_y_int():
    y_int = input("What are the coordinates for the y intercept in the above graph? (Enter your answer as (x,y)) ")
    
    if y_int in ["(0,10)"]:
        display(Latex("Correct!"))
    else:
        display(Latex("Try Again!"))
        display(Latex("Hint: Look at where the graph crosses the $y$ axis."))
        ask_y_int()
        
ask_y_int()
```

    What are the coordinates for the y intercept in the above graph? (Enter your answer as (x,y)) (0,10)



Good Job!



```python
def ask_x_int():
    x_int = input("What are the coordinates for the x intercept in the above graph? (Enter your answer as (x,y)) ")
    
    if x_int in ["(5,0)"]:
        display(Latex("Well done!"))
    else:
        display(Latex("Try Again!"))
        display(Latex("Hint: Look at where the graph crosses the $x$ axis."))
        ask_x_int()
        
ask_x_int()
```

    What are the coordinates for the x intercept in the above graph? (Enter your answer as (x,y)) (5,0)



Good Job!



```python
def ask_equation():
    eq = input("What is the equation of the linear function shown on the graph? (Enter your answer as: y = mx + b)")
    
    if eq in ["y = -2x + 10", "y = -(2)x + 10", "y = (-2)x + 10", "y=-2x+10"]:
        display(Latex("Good Job!"))
    else:
        display(Latex("Try Again!"))
        display(Latex("Hint: Remeber that slope = $m$, and the $y$ intercept = $b$"))
        ask_equation()
        
ask_equation()
```

    What is the equation of the linear function shown on the graph? (Enter your answer as: y = mx + b)y=-2x+10



Good Job!



```python
def ask_q():
    q = input("At what point did the person walk past their destination? (Enter your answer as (x,y)) ")
    
    if q in ["(5,0)"]:
        display(Latex("Way to go!"))
    else:
        display(Latex("Try Again!"))
        display(Latex("Hint: At what point is the person 0 meters away from their destination, but then keeps going?"))
        ask_q()
        
ask_q()
```

    At what point did the person walk past their destination? (Enter your answer as (x,y)) (5,0)



Good Job!


###### Question 2.

Quentin and Isabelle both earn their allowance by doing exercise. Quentins parents pay him \$8.00 each time he goes for a run,  plus an additional \$0.50 for every kilometer he runs.

Isabelle gets paid \$5.00 every time she goes for a run, and an additional \$1.00 for every kilometer she runs.

At what distance do Quentin and Isabelle make the same amount of money?
Who makes the most money after running for 10 kilometers?


To help answer these questions, here are the linear equations that describe Quentin and Isabelle's allowance:

Quentin: 
\begin{equation}
y = \frac{1}{2}x + 8
\end{equation}

Isabelle:
\begin{equation}
y = x + 5
\end{equation}

Create a graph that shows both of these functions at the points: 

\begin{array}{| c | c |}
\hline
  Distance\: (km) & Money\: Made\: (Dollars)\\
  \hline
  0 & ? \\\hline
  1 & ? \\\hline
  2 & ? \\\hline
  3 & ? \\\hline
  4 & ? \\\hline
  5 & ? \\\hline
  6 & ? \\\hline
  7 & ? \\\hline
\end{array}

*Hint*: Create one table for each of the equations and find the corresponding y values, then use the section below to enter the points.


```python
D = Double_Graph(5)
D.run_it()
```

    Label the X-Axis: Distance (km)
    Label the Y-Axis: Money Made (dollars)
    Give the graph a title: Quentin and Isabelle
    Enter data points for Quentin.
    Enter the x-values as a list of the form [a,b,c,d,...]: [0,1,2,3,4,5,6,7]
    Enter the y-values as a list of the form [a,b,c,d,...]: [8,8.5,9,9.5,10,10.5,11,11.5]
    Now, enter data points for Isabelle
    Enter the x-values as a list of the form [a,b,c,d,...]: [0,1,2,3,4,5,6,7]
    Enter the y-values as a list of the form [a,b,c,d,...]: [5,6,7,8,9,10,11,12]



<div id="da2b1250-a082-4b45-9875-cad0853abfae" style="height: 525px; width: 100%;" class="plotly-graph-div"></div>



```python
def ask_q2():
    q = input("At what distance do Quentin and Isabelle make the same amount of money? (Enter your answer as a whole number) ")
    
    if q in ["6", "6km", "6 km", "6 kilometers"]:
        display(Latex("You got it!"))
    else:
        display(Latex("Try Again!"))
        display(Latex("Hint: What is the $x$ coordinate where the two functions cross?"))
        ask_q2()
        
ask_q2()
```

    At what distance do Quentin and Isabelle make the same amount of money? (Enter your answer as a whole number) 6



Good Job!



```python
def ask_q3():
    q = input("Who makes the most money after running 10 kilometers? (Enter: Quentin, or Isabelle) ")
    
    if q in ["Isabelle"]:
        display(Latex("Nice work!"))
    else:
        display(Latex("Try Again!"))
        display(Latex("Hint: Who has the highest $y$ value when $x = 10$?"))
        ask_q3()
        
ask_q3()
```

    Who makes the most money after running 10 kilometers? (Enter: Quentin, or Isabelle) Q



Try Again!



Hint: Who has the highest $y$ value when $x = 10$?


    Who makes the most money after running 10 kilometers? (Enter: Quentin, or Isabelle) I



Try Again!



Hint: Who has the highest $y$ value when $x = 10$?


    Who makes the most money after running 10 kilometers? (Enter: Quentin, or Isabelle) Isabelle



Good Job!


## **Conclusion**

In this lesson, we have highlighted some of the many ways that a graph can help us understand the relationship between two variables in a linear function. Some of the important methods that you should remember include:

- Defining a Linear Function
- Identifying the Independent and Dependent Variables
- Setting up a Graph on the Cartesian Plane
- Creating and Plotting Data Points
- Features of a Graph ($x$ intercept, $y$ intercept, and slope)

Throughout your life, you will be presented with all sorts of statistics, data, charts and graphs. It is important that you have the skills required to analyze what a graph is trying to tell you, not just so you can pass your math class, but to enable you to make informed decisions when you need to consider how variables can interact and affect results.



32f22806eccc11245ec9ca4864925d24dcd6a5db
ipynb
Jupyter Notebook
Mathematics/graphing-two-variables.ipynb
jazwinkicks/jaz-callysto
f9414e275c3cc38bc7266f105e0dcb57ad483ae0
CC-BY-4.0
Mathematics/graphing-two-variables.ipynb
jazwinkicks/jaz-callysto
f9414e275c3cc38bc7266f105e0dcb57ad483ae0
CC-BY-4.0
Mathematics/graphing-two-variables.ipynb
jazwinkicks/jaz-callysto
f9414e275c3cc38bc7266f105e0dcb57ad483ae0
CC-BY-4.0
Qwen/Qwen-72B
1. YES
2. YES