From 1509655ed93e082d8c902323f3a278c53631b2a2 Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Sun, 1 Feb 2026 10:02:20 -0500 Subject: [PATCH] Remove pylab examples --- README.md | 4 +++ ch2_graphing/graphing_functions.py | 2 +- ch2_graphing/simple_graphs.py | 17 +++++----- ch2_graphing/temp_comparision_v2.py | 52 ++++++++++++++++++----------- ch2_graphing/temp_comparison.py | 29 ++++++++++++---- ch2_graphing/temp_decimal.py | 20 +++++++---- ch2_graphing/temp_graph.py | 15 ++++++--- 7 files changed, 92 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index 442d1ac..99332b3 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,10 @@ Math is fun, but we generally learn applying it's operations with pencil and pap ## Installation +```bash +sudo apt install python3-tk +``` + ```bash uv venv -p 3.12 uv pip install -r requirements.in diff --git a/ch2_graphing/graphing_functions.py b/ch2_graphing/graphing_functions.py index 18d6443..cfcef2e 100644 --- a/ch2_graphing/graphing_functions.py +++ b/ch2_graphing/graphing_functions.py @@ -28,7 +28,7 @@ def generate_F_r(): m2 = 1.5 for dist in r: - force = G * (m1 / m2) / (dist ** 2) + force = G * (m1 * m2) / (dist ** 2) F.append(force) draw_graph(r, F) diff --git a/ch2_graphing/simple_graphs.py b/ch2_graphing/simple_graphs.py index da14455..3ecfb7a 100644 --- a/ch2_graphing/simple_graphs.py +++ b/ch2_graphing/simple_graphs.py @@ -1,17 +1,16 @@ """ -Simple pylab example +Simple matplotlib example -Use pylab when you are in an interactive shell. This example really should be copied into a RPEL. """ -from pylab import plot, show +import numpy as np +import matplotlib.pyplot as plt -x_numbers = [1, 2, 3] -y_numbers = [2, 4, 6] - -my_graph = plot(x_numbers, y_numbers, marker='o') -# my_graph = plot(x_numbers, y_numbers, 'o') +x_numbers = np.array([1, 2, 3]) +y_numbers = np.array([2, 4, 6]) +fig, ax = plt.subplots() +ax.plot(x_numbers, y_numbers, marker="o") if __name__ == "__main__": - show() + plt.show() diff --git a/ch2_graphing/temp_comparision_v2.py b/ch2_graphing/temp_comparision_v2.py index 08de9d5..6fcb4c8 100644 --- a/ch2_graphing/temp_comparision_v2.py +++ b/ch2_graphing/temp_comparision_v2.py @@ -1,20 +1,32 @@ -from pylab import legend, plot, show, title, xlabel, ylabel, axis - -nyc_temp_2000 = [31.3, 37.3, 47.2, 51.0, 63.5, 71.3, 72.3, 72.7, 66.0, 57.0, 45.3, 31.1] -nyc_temp_2006 = [40.9, 35.7, 43.1, 55.7, 63.1, 71.0, 77.9, 75.8, 66.6, 56.2, 51.9, 43.6] -nyc_temp_2012 = [37.3, 40.9, 50.9, 54.8, 65.1, 71.0, 78.8, 76.7, 68.8, 58.0, 43.9, 41.5] - -months = range(1, 13) - -plot(months, nyc_temp_2000, months, nyc_temp_2006, months, nyc_temp_2012) - -title('Average monthly temperature in NYC') - -xlabel("Month") -ylabel("Temperature") - -legend([2000, 2006, 2012]) - -axis(ymin=0) - -show() \ No newline at end of file +import numpy as np +import matplotlib.pyplot as plt + +nyc_temp_2000 = np.array([ + 31.3, 37.3, 47.2, 51.0, 63.5, 71.3, + 72.3, 72.7, 66.0, 57.0, 45.3, 31.1 +]) +nyc_temp_2006 = np.array([ + 40.9, 35.7, 43.1, 55.7, 63.1, 71.0, + 77.9, 75.8, 66.6, 56.2, 51.9, 43.6 +]) +nyc_temp_2012 = np.array([ + 37.3, 40.9, 50.9, 54.8, 65.1, 71.0, + 78.8, 76.7, 68.8, 58.0, 43.9, 41.5 +]) + +months = np.arange(1, 13) + +fig, ax = plt.subplots() + +ax.plot(months, nyc_temp_2000, marker="o", label="2000") +ax.plot(months, nyc_temp_2006, marker="o", label="2006") +ax.plot(months, nyc_temp_2012, marker="o", label="2012") + +ax.set_title("Average monthly temperature in NYC") +ax.set_xlabel("Month") +ax.set_ylabel("Temperature") + +ax.set_ylim(bottom=0) # equivalent to axis(ymin=0) +ax.legend() + +plt.show() diff --git a/ch2_graphing/temp_comparison.py b/ch2_graphing/temp_comparison.py index 1be6488..c5dfda1 100644 --- a/ch2_graphing/temp_comparison.py +++ b/ch2_graphing/temp_comparison.py @@ -1,13 +1,28 @@ -from pylab import legend, plot, show +import numpy as np +import matplotlib.pyplot as plt -nyc_temp_2000 = [31.3, 37.3, 47.2, 51.0, 63.5, 71.3, 72.3, 72.7, 66.0, 57.0, 45.3, 31.1] -nyc_temp_2006 = [40.9, 35.7, 43.1, 55.7, 63.1, 71.0, 77.9, 75.8, 66.6, 56.2, 51.9, 43.6] -nyc_temp_2012 = [37.3, 40.9, 50.9, 54.8, 65.1, 71.0, 78.8, 76.7, 68.8, 58.0, 43.9, 41.5] +nyc_temp_2000 = [ + 31.3, 37.3, 47.2, 51.0, 63.5, 71.3, + 72.3, 72.7, 66.0, 57.0, 45.3, 31.1 +] +nyc_temp_2006 = [ + 40.9, 35.7, 43.1, 55.7, 63.1, 71.0, + 77.9, 75.8, 66.6, 56.2, 51.9, 43.6 +] +nyc_temp_2012 = [ + 37.3, 40.9, 50.9, 54.8, 65.1, 71.0, + 78.8, 76.7, 68.8, 58.0, 43.9, 41.5 +] months = range(1, 13) -plot(months, nyc_temp_2000, months, nyc_temp_2006, months, nyc_temp_2012) +plt.plot(months, nyc_temp_2000) +plt.plot(months, nyc_temp_2006) +plt.plot(months, nyc_temp_2012) -legend([2000, 2006, 2012]) +plt.legend([2000, 2006, 2012]) +plt.xlabel("Month") +plt.ylabel("Temperature") +plt.title("NYC Monthly Temperatures") -show() \ No newline at end of file +plt.show() diff --git a/ch2_graphing/temp_decimal.py b/ch2_graphing/temp_decimal.py index 5817e8e..ca7d855 100644 --- a/ch2_graphing/temp_decimal.py +++ b/ch2_graphing/temp_decimal.py @@ -1,12 +1,20 @@ -from pylab import plot, show +import numpy as np +import matplotlib.pyplot as plt from decimal import Decimal -nyc_temp = [50.9, 60.3, 54.4, 52.4, 51.5, 52.8, 56.8, 55.0, 55.3, 54.0, 56.7, 56.4, 57.3] +nyc_temp = [ + 50.9, 60.3, 54.4, 52.4, 51.5, 52.8, + 56.8, 55.0, 55.3, 54.0, 56.7, 56.4, 57.3 +] -dec_nyc_temp = list(map(lambda x: Decimal(str(x)), nyc_temp)) +# Preserve Decimal behavior +dec_nyc_temp = [Decimal(str(x)) for x in nyc_temp] -years = range(2000, 2013) +years = list(range(2000, 2013)) -plot(years, dec_nyc_temp, marker='o') +plt.plot(years, dec_nyc_temp, marker="o") +plt.xlabel("Year") +plt.ylabel("Temperature") +plt.title("NYC Temperature by Year") -show() \ No newline at end of file +plt.show() diff --git a/ch2_graphing/temp_graph.py b/ch2_graphing/temp_graph.py index 263b5ef..21e5539 100644 --- a/ch2_graphing/temp_graph.py +++ b/ch2_graphing/temp_graph.py @@ -1,7 +1,14 @@ -from pylab import plot, show +import numpy as np +import matplotlib.pyplot as plt -nyc_temp = [53.9, 56.3, 56.4, 53.4, 54.5, 55.8, 56.8, 55.0, 55.3, 54.0, 56.7, 56.4, 57.3] +nyc_temp = np.array([ + 53.9, 56.3, 56.4, 53.4, 54.5, 55.8, + 56.8, 55.0, 55.3, 54.0, 56.7, 56.4, 57.3 +]) -plot(nyc_temp, marker="o") +plt.plot(nyc_temp, marker="o") +plt.xlabel("Day") +plt.ylabel("Temperature (°F)") +plt.title("NYC Temperatures") -show() \ No newline at end of file +plt.show()