commit 4e15d8f6f5c89ce854f7765d6a886bc3c1071e2c Author: androiddrew Date: Wed Jan 9 16:20:58 2019 -0500 Initial commit. completed exercises up to chapter 2. Graphing equations. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..680a09b --- /dev/null +++ b/.gitignore @@ -0,0 +1,63 @@ +# ---> Python +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*,cover + +# Translations +*.mo +*.pot + +# Django stuff: +*.log + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Pycharm +.idea + diff --git a/ch2_graphing/graphing_functions.py b/ch2_graphing/graphing_functions.py new file mode 100644 index 0000000..18d6443 --- /dev/null +++ b/ch2_graphing/graphing_functions.py @@ -0,0 +1,38 @@ +""" +Simple example demonstrating graphing with functions + +Topic: Newton's law of Universal Gravitation + +Models the relationship between gravitational force and distance between two bodies. +""" + +import matplotlib.pyplot as plt + +G = 6.674 * (10 ** -11) + + +def draw_graph(x, y): + plt.plot(x, y, marker="o") + plt.xlabel("Distance in Meters") + plt.ylabel("Gravitational Force in Newtons") + plt.title("Gravitational Force and Distance") + plt.show() + + +def generate_F_r(): + """Generates values for r in the New""" + r = range(100, 1001, 50) + F = [] + + m1 = 0.5 + m2 = 1.5 + + for dist in r: + force = G * (m1 / m2) / (dist ** 2) + F.append(force) + + draw_graph(r, F) + + +if __name__ == "__main__": + generate_F_r() diff --git a/ch2_graphing/mygraph.png b/ch2_graphing/mygraph.png new file mode 100644 index 0000000..349959c Binary files /dev/null and b/ch2_graphing/mygraph.png differ diff --git a/ch2_graphing/pyplot_example.py b/ch2_graphing/pyplot_example.py new file mode 100644 index 0000000..2c3855d --- /dev/null +++ b/ch2_graphing/pyplot_example.py @@ -0,0 +1,19 @@ +""" +Simple Pyplot example + +PyPlot is the module you should be using for non-interactive scripts. +""" + +import matplotlib.pyplot + + +def create_graph(): + x_nums = [1, 2, 3] + y_nums = [2, 4, 6] + + matplotlib.pyplot.plot(x_nums, y_nums) + matplotlib.pyplot.show() + + +if __name__ == "__main__": + create_graph() diff --git a/ch2_graphing/saving_a_plot.py b/ch2_graphing/saving_a_plot.py new file mode 100644 index 0000000..3a69484 --- /dev/null +++ b/ch2_graphing/saving_a_plot.py @@ -0,0 +1,19 @@ +""" +Example saving a figure +""" +import matplotlib.pyplot as plt +import pathlib + +HERE = pathlib.Path(__file__).parent + + +def create_graph(): + x = [1, 2, 3] + y = [2, 4, 6] + plt.plot(x, y) + plt.savefig(HERE.joinpath("mygraph.png")) + + +if __name__ == "__main__": + print(HERE) + create_graph() \ No newline at end of file diff --git a/ch2_graphing/simple_graphs.py b/ch2_graphing/simple_graphs.py new file mode 100644 index 0000000..da14455 --- /dev/null +++ b/ch2_graphing/simple_graphs.py @@ -0,0 +1,17 @@ +""" +Simple pylab example + +Use pylab when you are in an interactive shell. This example really should be copied into a RPEL. +""" + +from pylab import plot, show + +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') + + +if __name__ == "__main__": + show() diff --git a/ch2_graphing/temp_comparision_v2.py b/ch2_graphing/temp_comparision_v2.py new file mode 100644 index 0000000..08de9d5 --- /dev/null +++ b/ch2_graphing/temp_comparision_v2.py @@ -0,0 +1,20 @@ +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 diff --git a/ch2_graphing/temp_comparison.py b/ch2_graphing/temp_comparison.py new file mode 100644 index 0000000..1be6488 --- /dev/null +++ b/ch2_graphing/temp_comparison.py @@ -0,0 +1,13 @@ +from pylab import legend, plot, show + +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) + +legend([2000, 2006, 2012]) + +show() \ No newline at end of file diff --git a/ch2_graphing/temp_decimal.py b/ch2_graphing/temp_decimal.py new file mode 100644 index 0000000..5817e8e --- /dev/null +++ b/ch2_graphing/temp_decimal.py @@ -0,0 +1,12 @@ +from pylab import plot, show +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] + +dec_nyc_temp = list(map(lambda x: Decimal(str(x)), nyc_temp)) + +years = range(2000, 2013) + +plot(years, dec_nyc_temp, marker='o') + +show() \ No newline at end of file diff --git a/ch2_graphing/temp_graph.py b/ch2_graphing/temp_graph.py new file mode 100644 index 0000000..263b5ef --- /dev/null +++ b/ch2_graphing/temp_graph.py @@ -0,0 +1,7 @@ +from pylab import plot, show + +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] + +plot(nyc_temp, marker="o") + +show() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4b43f7e --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +matplotlib \ No newline at end of file