Graphs#

Plotting data on a graph has the major advantage, it is often clear to see trends and patterns that are present. In experiments, we have:

  • a control variable that we can change,

  • an observed variable that we measure,

  • and all other quantities we try to keep constant.

When plotting a graph of data, we always plot the control variable along the horizontal (or \(x\)-) axis and the observed variable along the vertical (or \(y\)-) axis.

Hide code cell source

import matplotlib.pyplot as plt
import numpy as np 

x = np.linspace(-1, 6, 10)
y = 0.5 * x + 1

plt.plot(x, y)
plt.xlabel('Control variable, $x$')
plt.ylabel('Observed variable, $y$')
plt.show()
../_images/ec1a1babc60b102949ecb0500895ed15c3fec9ccee879dfb42fd7c0b95d2bb08.png

Straight Line Graphs#

Straight line graphs come in the form:

\[ y = mx + c \]

where \(x\) is the control variable, \(y\) is the observed variable and \(m\) and \(c\) are constants.

  • \(c\) is known as the intercept and is where the graph passes through the \(y\)-axis. Hence to find \(c\), we find the value of \(y\) when \(x=0\).

  • \(m\) is known as the gradient and describes how steep the line is. It can be found by drawing a “triangle” on the straight line and using it to calculate the equation:

\[ m = \frac{\Delta y}{\Delta x} \]

This can all be visualised on the graph below:

Hide code cell source

x = np.linspace(-0.2, 3.3, 100)
y = 2 * x - 1

plt.plot(x, y)
plt.axvline(x=0, color='gray', linestyle='-')
plt.axhline(y=0, color='gray', linestyle='-')
plt.axvline(x=1, color='red', linestyle='--', ymin=0.2, ymax=0.38)
plt.axvline(x=3, color='red', linestyle='--', ymin=0.2, ymax=0.38)
plt.axvline(x=3, color='red', linestyle='-', ymin=0.38, ymax=0.87)
plt.axhline(y=1, color='red', linestyle='--', xmin=0.21, xmax=0.38)
plt.axhline(y=5, color='red', linestyle='--', xmin=0.21, xmax=0.625)
plt.axhline(y=1, color='red', linestyle='-', xmin=0.38, xmax=0.625)
plt.text(-0.5, 1, '$y_1$', fontsize=12, ha='center')
plt.text(-0.5, 5, '$y_2$', fontsize=12, ha='center')
plt.text(1, -0.7, '$x_1$', fontsize=12, ha='center')
plt.text(3, -0.7, '$x_2$', fontsize=12, ha='center')
plt.text(2, 0.6, '$\Delta x$', fontsize=12, ha='center')
plt.text(3.3, 3, '$\Delta y$', fontsize=12, ha='center')
plt.text(3.4, 5.4, '$f(x)=2x-1$', fontsize=12, ha='left')
plt.xlabel('Control variable, $x$')
plt.ylabel('Observed variable, $y$')
plt.xlim(-2, 6)
plt.ylim(-2, 6)
plt.show()
../_images/cb05dd450f9cec87e020b7490280382858f74fd2c03929e232c962708d24531e.png

Example

What is the equation of the straight line shown in the diagram below?

A plot with a straight line with the equation y=2x + 1.

Solution: We know that since this is a straight line, it will be in the form \(y=mx+c\). The values of \(c\) is the value of \(y\) when \(x=0\). At \(x=0\), \(y=1\) from the graph. Therefore, \(c=1\).

To find \(m\), we need to calculate \(\frac{\Delta y}{\Delta x}\). Using the triangle method with the triangle in the above graph gives:

\[ \textrm{gradient} = \frac{\Delta y}{\Delta x} = \frac{y_2-y_1}{x_2-x_1} = \frac{3 - (-1)}{1 - (-1)} = \frac{4}{2} = 2 \]

Since \(m=2\), the equation of the line is \(y=2x+1\).

Example

What is the equation of the straight line shown in the diagram below?

A plot with a straight line with the equation y=-0.5x.

Solution: Since this is a straight line, it will be in the form \(y=mx+c\). The values of \(c\) can be seen to be \(0\), because when \(x=0\), \(y\) is also \(0\).

Using the triangle method, we can find the gradient of the line. with the triangles on the graph, we have

\[ \textrm{gradient} = \frac{\Delta y}{\Delta x} = \frac{y_2-y_1}{x_2-x_1} = \frac{-1 - 1}{2 - (-2)} = \frac{-2}{4} = \frac{-1}{2} \]

Since \(m=\frac{-1}{2}\), the equation of the line is \(y=\frac{-1}{2}x\).


Python, specifically the matplotlib library can be used to plot these straight lines using their equations.

import matplotlib.pyplot as plt
import numpy as np 

x = np.linspace(-3, 3, 100)
line_1 = 2 * x +1 
line_2 = -0.5 * x

plt.plot(x, line_1, label='$y=2x+1$')
plt.plot(x, line_2, label='$y=-0.5x$')
plt.axhline(y=0, color='gray', linestyle='-')
plt.axvline(x=0, color='gray', linestyle='-')
plt.legend()
plt.show()
../_images/4d1ab8c98e2059b50525291f4fdb1b4a77cacb0aa6d899fb30b936c139493a2c.png

Graphs with Units#

Often graphs won’t be those of just numbers and a line, but instead will have some physical significance and hence include units. Consider the graph of a particles velocity as a function of time.

Hide code cell source

t = np.linspace(0, 10, 100)
v = 8 * t + 25

plt.plot(t, v)
plt.xlabel('Time, $t$ (s)')
plt.ylabel('Velocity, $v$ (m/s)')
plt.xlim(0, None)
plt.ylim(0, None)
plt.show()
../_images/0d3c2536323e9a4f85df3ab3973060bdb65be916ef8d4f02a8aba845a1533347.png

The equation of the line is \(v = 8 t + 25\). \(v\) has units of metres per second and to make the equation work so everything on the right hand side must also have units of metres per second as well. That means that the \(c=25\) has units of m/s. This 25 m/s is the starting velocity of the particle.

The units of \(m\) can be worked out fro the equation:

\[ \frac{\Delta y}{\Delta x} \textrm{ or in this case }\frac{\Delta v}{\Delta t} \]

\(\Delta v\) has the same units as \(v\) (m/s) and similarly \(t\) has the units (s). This means \(m\), the gradient, has the units of m/s ÷ s or m/s2. Since the gradient has units of distance per time squared, then it means that it is an acceleration. The particle will accelerate at 8 m/s2.


The dimensional analysis we perform above can be achieved with the Python library pint.

import pint

ureg = pint.UnitRegistry()
(ureg.meter / ureg.second) / (ureg.second)
meter/second2