intro = "This is a dummy module!!"
def print_func(name):
print("Hello : ", name)
return
import mymodule
mymodule.intro
'This is a dummy module!!'
mymodule.print_func("Sachin")
Hello : Sachin
from mymodule import print_func
print_func("Sachin")
Hello : Sachin
from mymodule import intro
print(intro)
This is a dummy module!!
from mymodule import *
print(intro)
print_func("Sachin")
This is a dummy module!! Hello : Sachin
import mymodule as my
my.intro
'This is a dummy module!!'
import numpy as np
a = np.array([2,3,4])
print(a, a.dtype)
[2 3 4] int32
b = np.array([(1.2, 3.5, 5.1),(4.1,6.1,0.5)])
print(b, b.dtype)
[[1.2 3.5 5.1] [4.1 6.1 0.5]] float64
b[0]
array([1.2, 3.5, 5.1])
b[1]
array([4.1, 6.1, 0.5])
b[0][0]
1.2
c = np.array( [ [1,2], [3,4] ], dtype=complex )
c
array([[1.+0.j, 2.+0.j], [3.+0.j, 4.+0.j]])
print('Zeros: ',np.zeros( (3,4) ))
print('Ones', np.ones( (2,4), dtype=np.float64 ))
print('Empty', np.empty( (2,3) ))
Zeros: [[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]] Ones [[1. 1. 1. 1.] [1. 1. 1. 1.]] Empty [[1.2 3.5 5.1] [4.1 6.1 0.5]]
a = np.arange(15).reshape(3, 5)
a
array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14]])
print("array dimensions: ", a.shape)
print("number of dimensions: ", a.ndim)
print("element types: ", a.dtype.name)
print("size of elements: ", a.itemsize)
print("total number of elements: ",a.size)
print("type of object:",type(a))
array dimensions: (3, 5) number of dimensions: 2 element types: int32 size of elements: 4 total number of elements: 15 type of object: <class 'numpy.ndarray'>
import matplotlib.pyplot as plt
from matplotlib import pyplot as plt
import matplotlib.pyplot as plt
val = [1, 2, 3, 4, 5]
sq = [1, 4, 9, 16, 25]
cu = [1, 8, 27, 64, 125]
plt.plot(val, sq, label = 'Square Values', linewidth = 2, linestyle = '--')
plt.plot(val, cu, label = 'Cube Values', linewidth = 4)
plt.xlabel('Values')
plt.ylabel('Square and Cube')
plt.title('Simple Graph')
plt.legend()
plt.show()
import matplotlib.pyplot as plt
boys = ['John', 'Ric', 'Tim', 'Tom', 'Brat']
height = [7, 6.5, 5.7, 5.10, 6.1]
girls = ['Beth', 'Rose', 'Jill', 'Becky', 'Tina', 'Lizz']
ht_g = [5, 5.5, 6.3, 5.11, 6, 5.7]
plt.bar(boys, height, width = 0.5)
plt.bar(girls, ht_g, color = 'c', width = 0.5)
plt.xlabel('Boys & Girls')
plt.ylabel('Height')
plt.title('Height Distribution')
plt.legend(['Boys', 'Girls'])
plt.show()
Constructing a Histogram
from matplotlib import pyplot as py
population_ages = [5, 6, 7, 8, 8, 9, 10, 15, 12, 16, 19, 14, 11, 15, 22, 25, 28, 29, 45, 41, 44, 32, 36, 39, 38, 55, 56, 50, 51, 54, 57, 55, 61, 64, 65, 61, 69, 66, 70, 71, 74, 74, 78, 79]
bins = [0, 20, 40, 60, 80]
plt.hist(population_ages, bins, histtype='bar', rwidth=0.8)
plt.xlabel('Age')
plt.ylabel('No. of People')
plt.title('Histogram showing number of people within a specific age group')
plt.legend(['Population Density'])
plt.show()
from matplotlib import pyplot as py
countries = ['USA', 'India', 'England', 'Australia', 'China']
pop = [20, 60, 25, 30, 70]
plt.scatter(countries, pop, s = 150, color = 'k', marker = '*')
plt.xlabel('Countries')
plt.ylabel('Pop Size')
plt.legend(['Size'])
plt.show();
from matplotlib import pyplot as plt
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
sleeping = [7, 6, 5, 5, 8]
eating = [4, 3, 4, 2, 3]
working = [8, 7, 8, 7, 6]
playing = [5, 8, 7, 10, 7]
plt.stackplot(days, sleeping, eating, working, playing,
colors = ['r', 'b', 'c', 'm'],
labels = ['Sleeping', 'Eating', 'Working', 'Playing'])
plt.legend()
plt.title('My Weekly Routine')
plt.xlabel('Days')
plt.ylabel('Activities')
plt.show()
from matplotlib import pyplot as py
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
sleeping = [7, 6, 5, 5, 8]
eating = [4, 3, 4, 2, 3]
working = [8, 7, 8, 7, 6]
playing = [5, 8, 7, 10, 7]
friday = [8, 3, 6, 7]
activities = ['sleeping', 'eating', 'working', 'playing']
plt.pie(friday, labels = activities,
colors = ['r', 'b', 'c', 'm'],
startangle = 90,
shadow = True,
explode = (0.1, 0, 0, 0), # pulls out the specific slice of pie
autopct = '%1.1f%%') # adds % to the pie
plt.title("Friday's Routine", color = 'red')
plt.show()
from matplotlib import pyplot as py
import numpy as np
col1, col2 = np.loadtxt('C:/Users/saj415/Documents/example.txt', delimiter = ',', unpack = True)
plt.plot(col1, col2, label = 'Loaded from Text File!')
plt.legend()
plt.xlabel('Col1')
plt.ylabel('Col2')
plt.title('Loading Data from Text File')
plt.show()
from matplotlib import pyplot as plt
import numpy as np
fig = plt.figure();
ax1 = plt.subplot2grid((1, 1), (0, 0)) # (1, 1) -> shape of grid, (0, 0) -> starting point
names = np.array(['Tony Stark', 'Bruce Banner', 'Peter Parker', 'Doctor Strange', 'Steve Rogers', 'Bucky Barnes'])
rating = np.array([98, 95, 90, 88, 94, 92])
ax1.plot(names, rating)
plt.xlabel('Avenger')
plt.ylabel('Rating')
plt.title('Avengers Rating')
plt.show()
from matplotlib import pyplot as plt
import numpy as np
fig = plt.figure();
ax1 = plt.subplot2grid((1, 1), (0, 0))
names = np.array(['Tony Stark', 'Bruce Banner', 'Peter Parker', 'Doctor Strange', 'Steve Rogers', 'Bucky Barnes'])
rating = np.array([98, 95, 90, 88, 94, 92])
ax1.plot(names, rating)
#rotate the labels
for label in ax1.xaxis.get_ticklabels():
label.set_rotation(45)
plt.xlabel('Avenger')
plt.ylabel('Rating')
plt.title('Avengers Rating')
plt.subplots_adjust(left = 0.09, bottom = 0.20, right = 0.94, top = 0.95, wspace = 0.2, hspace = 0) #wspace & hspace -> padding b/w plots
plt.show()
from matplotlib import pyplot as plt
import numpy as np
fig = plt.figure();
ax1 = plt.subplot2grid((1, 1), (0, 0))
names = np.array(['Tony Stark', 'Bruce Banner', 'Peter Parker', 'Doctor Strange', 'Steve Rogers', 'Bucky Barnes'])
rating = np.array([98, 95, 90, 88, 94, 92])
ax1.plot(names, rating)
# rotate the labels
for label in ax1.xaxis.get_ticklabels():
label.set_rotation(45)
# add grid to the plot
ax1.grid(True, color = 'r', linestyle = '-', linewidth = 0.5)
plt.xlabel('Avenger')
plt.ylabel('Rating')
plt.title('Avengers Rating')
plt.subplots_adjust(left = 0.09, bottom = 0.20, right = 0.94, top = 0.95, wspace = 0.2, hspace = 0)
plt.show()
from matplotlib import pyplot as plt
import numpy as np
fig = plt.figure();
ax1 = plt.subplot2grid((1, 1), (0, 0))
names = np.array(['Tony Stark', 'Bruce Banner', 'Peter Parker', 'Doctor Strange', 'Steve Rogers', 'Bucky Barnes'])
rating = np.array([98, 95, 90, 88, 94, 92])
ax1.plot(names, rating)
# fills
ax1.fill_between(names, rating, 92, where = (rating >= rating[5]), facecolor = 'g', alpha = 0.3)
ax1.fill_between(names, rating, 92, where = (rating < rating[5]), facecolor = 'r', alpha = 0.3)
# rotate the labels
for label in ax1.xaxis.get_ticklabels():
label.set_rotation(45)
# add grid to the plot
ax1.grid(True, color = 'r', linestyle = '-', linewidth = 0.5)
# more customization
ax1.xaxis.label.set_color('r')
ax1.yaxis.label.set_color('b')
ax1.set_yticks([88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100])
plt.xlabel('Avenger')
plt.ylabel('Rating')
plt.title('Avengers Rating')
plt.subplots_adjust(left = 0.09, bottom = 0.20, right = 0.94, top = 0.95, wspace = 0.2, hspace = 0)
plt.show()
from matplotlib import style
style.use('fivethirtyeight')
print(plt.style.available)
print(plt.__file__)
ax1 = plt.subplot2grid((1, 1), (0, 0))
no = [1, 2, 3, 4, 5, 6]
sq = [1, 4, 9, 16, 25, 36]
cu = [1, 8, 27, 64, 125, 216]
mu2 = [2, 4, 6, 8, 10, 12]
mu10 = [10, 20, 30, 40, 50, 60]
ax1.xaxis.label.set_color('r')
ax1.yaxis.label.set_color('b')
plt.plot(no, sq, label = 'Square')
plt.plot(no, cu, label = 'Cube')
plt.plot(no, mu2, label = 'Multiple of 2')
plt.plot(no, mu10, label = 'Multiple of 10')
plt.xlabel('Number')
plt.ylabel('Square, Cube, 2X, 10X')
plt.legend()
plt.title('Adding Style to Graph')
plt.show()
['bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark-palette', 'seaborn-dark', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'seaborn', 'Solarize_Light2', 'tableau-colorblind10', '_classic_test'] C:\Users\saj415\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\pyplot.py
from matplotlib import animation as animat
from matplotlib import style
style.use('fivethirtyeight')
fig = plt.figure();
ax1 = fig.add_subplot(1, 1, 1) # 1*1 and plot no. 1
def animate(i):
xs, ys = np.loadtxt('C:/Users/saj415/Documents/example.txt', delimiter = ',', unpack = True)
ax1.clear()
ax1.plot(xs, ys)
ani = animat.FuncAnimation(fig, animate, interval = 1000) #1000 ms -> every sec. update
plt.show()
import random
from matplotlib import pyplot as plt
fig = plt.figure()
def create_plots():
xs = []
ys = []
for i in range(10):
x = i
y = random.randrange(10)
xs.append(x)
ys.append(y)
return xs, ys
# add_subplot syntax:
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(212)
x, y = create_plots()
ax1.plot(x, y)
x, y = create_plots()
ax2.plot(x, y)
x, y = create_plots()
ax3.plot(x, y)
plt.show()
import random
from matplotlib import pyplot as plt
from matplotlib import style
import matplotlib.ticker as mticker
style.use('fivethirtyeight')
fig = plt.figure()
def create_plots():
xs = []
ys = []
for i in range(10):
x = i
y = random.randrange(10)
xs.append(x)
ys.append(y)
return xs, ys
ax1 = plt.subplot2grid((6, 1), (0, 0), rowspan = 1, colspan = 1)
plt.title('SubPlots', color = 'c')
plt.ylabel('Plot 1')
ax2 = plt.subplot2grid((6, 1), (1, 0), rowspan = 4, colspan = 1, sharex = ax1)
plt.ylabel('Plot 2', color = 'b')
ax3 = plt.subplot2grid((6, 1), (5, 0), rowspan = 1, colspan = 1, sharex = ax1)
plt.xlabel('X', color = 'm')
plt.ylabel('Plot 3')
# add_subplot syntax:
# ax1 = fig.add_subplot(221)
# ax2 = fig.add_subplot(222)
# ax3 = fig.add_subplot(212)
x, y = create_plots()
ax1.plot(x, y, color = 'r', label = 'Plot 1')
ax1.yaxis.set_major_locator(mticker.MaxNLocator(nbins = 3, prune = 'lower'))
x, y = create_plots()
ax2.plot(x, y, color = 'b', label = 'Plot 2')
x, y = create_plots()
ax3.plot(x, y, color = 'c', label = 'Plot 3')
plt.setp(ax1.get_xticklabels(), visible = False)
plt.setp(ax2.get_xticklabels(), visible = False)
ax1.legend()
leg = ax1.legend(loc = 9, ncol = 2, prop = {'size': 11})
leg.get_frame().set_alpha(0.4)
ax2.legend()
leg = ax2.legend(loc = 9, ncol = 2, prop = {'size': 11})
leg.get_frame().set_alpha(0.4)
ax3.legend()
leg = ax3.legend(loc = 9, ncol = 1, prop = {'size': 11})
leg.get_frame().set_alpha(0.4)
plt.show()
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111, projection = '3d')
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [2, 1, 4, 5, 7, 8, 9, 3, 1, 5]
z = [1, 2, 6, 3, 2, 7, 3, 3, 7, 2]
ax1.plot(x, y, z)
ax1.set_xlabel('X Axis')
ax1.set_ylabel('Y Axis')
ax1.set_zlabel('Z Axis')
plt.show()
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111, projection = '3d')
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [2, 1, 4, 5, 7, 8, 9, 3, 1, 5]
z = [1, 2, 6, 3, 2, 7, 3, 3, 7, 2]
# ax1.plot(x, y, z)
x1 = [-1, -2, -3, -4, -5, -6, -7, -8, -9, -10]
y1 = [-2, -1, -4, -5, -7, -8, -9, -3, -1, -5]
z1 = [-1, -2, -6, -3, -2, -7, -3, -3, -7, -2]
ax1.scatter(x1, y1, z1, marker = 'v')
ax1.scatter(x, y, z, marker = '*')
ax1.set_xlabel('X Axis')
ax1.set_ylabel('Y Axis')
ax1.set_zlabel('Z Axis')
plt.show()
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax1 = fig.add_subplot(111, projection = '3d')
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [2, 1, 4, 5, 7, 8, 9, 3, 1, 5]
z = [1, 2, 6, 3, 2, 7, 3, 3, 7, 2]
# ax1.plot(x, y, z)
x1 = [-1, -2, -3, -4, -5, -6, -7, -8, -9, -10]
y1 = [-2, -1, -4, -5, -7, -8, -9, -3, -1, -5]
z1 = [-1, -2, -6, -3, -2, -7, -3, -3, -7, -2]
# ax1.scatter(x1, y1, z1, marker = 'v')
# ax1.scatter(x, y, z, marker = '*')
x3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y3 = [2, 1, 4, 5, 7, 8, 9, 3, 1, 5]
z3 = np.zeros(10) # do not want bars floating in air, z axis is the one that goes up
dx = np.ones(10) # how far back is the x gonna go
dy = np.ones(10)
dz = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
ax1.bar3d(x3, y3, z3, dx, dy, dz)
ax1.set_xlabel('X Axis')
ax1.set_ylabel('Y Axis')
ax1.set_zlabel('Z Axis')
plt.show()