Example for Numpy and Matplotlib



In [1]:
#This is how you import libraries and assign them an alias
import random as r
import numpy as np
import matplotlib.pyplot as plt

In [2]:
#this is a random number generator using the random library
r.random()


Out[2]:
0.6269334312573364

In [3]:
#this is an array of random numbers using numpy
np.random.random((10,10))


Out[3]:
array([[ 0.64263641,  0.97591004,  0.61430508,  0.25310312,  0.31624835,
         0.85357146,  0.26981644,  0.81468061,  0.79108859,  0.53553102],
       [ 0.71262421,  0.71196937,  0.38626785,  0.20054045,  0.85827306,
         0.14279195,  0.52889098,  0.39362371,  0.5166977 ,  0.27583037],
       [ 0.01068437,  0.75702911,  0.37314562,  0.98087086,  0.41996665,
         0.85274189,  0.54722681,  0.75399316,  0.63508343,  0.34881343],
       [ 0.46522499,  0.05913477,  0.04518021,  0.32142027,  0.55499534,
         0.82310796,  0.87631252,  0.58930669,  0.45050414,  0.79009125],
       [ 0.02643714,  0.81582025,  0.83921821,  0.61739175,  0.48048697,
         0.15042726,  0.56900372,  0.70441641,  0.61254813,  0.92715421],
       [ 0.10921399,  0.31094297,  0.71395566,  0.03634797,  0.54393866,
         0.73170129,  0.05372304,  0.38559158,  0.88319438,  0.96073187],
       [ 0.42983847,  0.43938497,  0.96599876,  0.62907283,  0.5330632 ,
         0.14436102,  0.49882662,  0.88108017,  0.87527759,  0.45663573],
       [ 0.14193523,  0.00601961,  0.17453667,  0.35759541,  0.94731742,
         0.9843624 ,  0.54655132,  0.82310032,  0.10265093,  0.63655118],
       [ 0.91026084,  0.68390351,  0.9009292 ,  0.94262408,  0.14674898,
         0.6708454 ,  0.08623509,  0.00580094,  0.74838966,  0.97181376],
       [ 0.71931066,  0.20854164,  0.01565089,  0.69565195,  0.56782492,
         0.91511351,  0.76219113,  0.27424522,  0.12200269,  0.14903244]])

In [2]:
#this is a scatter plot using matplotlib and numpy
mu = 1        # mean
sigma = 0.25  # standard deviation
n = 10000     #sample size

#can also be written as :
#   mu, sigma, n = 1, 0.25, 10000 # mean, standard deviation, sample size

#notice that the random numbers are generated in a normal/Gaussian distribution
plt.scatter(np.random.normal(mu, sigma, n),np.random.normal(mu, sigma, n))

plt.show()



In [5]:
#this forces the plot to start at the origin
mu, sigma, n = 1, 0.25, 10000 # mean and standard deviation
plt.scatter(np.random.normal(mu, sigma, n),np.random.normal(mu, sigma, n),alpha=.05,c='orange')
# alpha=.05 modifies the transparency/opacity
# c='orange' changes the color of the points

plt.ylim([0,2])
plt.xlim([0,2])

plt.show()



In [ ]: