# CS 237 Spring 19
# Author: CS 237 course staff from past offerings
# Used in D1
We will make extensive use of the numpy library in this course. Numpy provides very efficient implementation of lists as arrays, as well as functions which operate point-wise on such arrays. This is a powerful and elegant way to write code! The basic ideas should be clear from the examples below; read a extensive tutorial here: https://docs.scipy.org/doc/numpy-dev/user/quickstart.html
# Here are some imports which will be used in the code in the rest of the notebook
# Jupyter notebook specific
from IPython.display import Image
from IPython.core.display import HTML
from IPython.display import display_html
from IPython.display import display
from IPython.display import Math
from IPython.display import Latex
from IPython.display import HTML
import numpy as np # arrays and functions which operate on array
import matplotlib.pyplot as plt # normal plotting
from numpy.random import seed, randint, uniform
from collections import Counter
%matplotlib inline
# To plot the points (1,2), (2,3), (3,6), (4,8) we would list the x values and the corresponding y values:
plt.scatter([1,2,3,4], [2,3,6,8])
plt.title('Some Points')
plt.xlabel("The X Values")
plt.ylabel("The Y Values")
plt.show()
# To plot the points (1,2), (2,3), (3,6), (4,8) we would list the x values and the corresponding y values:
plt.bar([1,2,3,4], [2,3,6,8])
plt.title('A Bar Chart')
plt.xlabel("The X Values")
plt.ylabel("The Y Values")
plt.show()
# Show the distribution of probabilities for a coin flip:
x = [0,1]
y = [0.5, 0.5]
labels = ['Heads', 'Tails']
plt.xticks(x, labels)
plt.bar(x,y)
plt.title('Probability Distribution for Coin Flips')
plt.ylabel("Probability")
plt.xlabel("Outcomes")
plt.show()
# To plot a curve through the points (1,2), (2,3), (3,6), (4,8) we would use:
plt.plot([1,2,3,4], [2,3,6,8])
plt.title('A Curve Through Some Points')
plt.xlabel("The X Values")
plt.ylabel("The Y Values")
plt.show()
plt.scatter([1,2,3,4], [2,3,6,8])
plt.plot([1,2,3,4], [2,3,6,8])
plt.title('A Curve Through Some Points, Showing the Points')
plt.xlabel("The X Values")
plt.ylabel("The Y Values")
plt.show()
# EXAMPLE: Plotting a square via lines
plt.figure(num=None, figsize=(8, 8), dpi=89)
plt.plot([0,1],[0,0],color='red') # Line connecting (0,0) to (1,0)
plt.plot([0,0],[0,1],color='green') # Line connecting (0,0) to (0,1)
plt.plot([0,1],[1,1],color='orange') # Line connecting (0,1) to (1,1)
plt.plot([1,1],[0,1],color='black') # Line connecting (1,0) to (1,1)
[<matplotlib.lines.Line2D at 0x11f0822b0>]
# Plotting a smooth curve for the function x^2
x = [i for i in range(10)]
y = [i**2 for i in x]
plt.plot(x,y)
plt.show()
x=np.arange(0,2*np.pi,.25)
y=np.sin(x)
plt.plot(x,y)
plt.plot([0,6.28],[0,0],color='black')
[<matplotlib.lines.Line2D at 0x11fd9bcc0>]
plt.hist([1,2,4,2,6,2,4,5,6,4,6,3,4,3],bins=[0.5,1.5,2.5,3.5,4.5,5.6,6.5],edgecolor='black')
plt.title('Sample Histogram')
plt.xlabel("Outcomes")
plt.ylabel("Frequency")
plt.show()