Created
October 4, 2017 00:47
-
-
Save apoorvalal/eaff80babfc93f01003e1b7315c44efd to your computer and use it in GitHub Desktop.
visualise functions in R2 and R3 in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In[1]: | |
import os,sys,glob | |
import pandas as pd | |
import numpy as np | |
import matplotlib.pyplot as plt | |
# run for jupyter notebook | |
# get_ipython().magic('matplotlib notebook') | |
# from IPython.core.interactiveshell import InteractiveShell | |
# InteractiveShell.ast_node_interactivity = "all" | |
# ## Plotter functions | |
# In[2]: | |
def plot_2d(function, x): | |
y = eval(function) | |
plt.clf() | |
plt.plot(x, y) | |
plt.show() | |
#%% | |
# In[3]: | |
def plot_3d(Z, X, Y): | |
from mpl_toolkits.mplot3d import Axes3D | |
from matplotlib import cm | |
from matplotlib.ticker import LinearLocator, FormatStrFormatter | |
fig = plt.figure() | |
ax = fig.gca(projection='3d') | |
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, | |
cmap=cm.RdBu, linewidth=0, antialiased=False) | |
ax.zaxis.set_major_locator(LinearLocator(10)) | |
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) | |
fig.colorbar(surf, shrink=0.5, aspect=5) | |
plt.show() | |
# In[4]: | |
x = np.arange(-20, 20, 0.5) | |
plot_2d('-x**2 + 2 * x - 2', x) | |
# In[5]: | |
x = np.arange(-20, 20, 0.5) | |
plot_2d('-x**3+ 2 * x**2 - 2', x) | |
# In[6]: | |
x = np.arange(-3.0, 3.0, 0.1) | |
y = np.arange(-3.0, 3.0, 0.1) | |
X, Y = np.meshgrid(x, y) # grid of points | |
def z_func(x, y): | |
return (1 - (x**2 + y**3)) * np.exp(-(x**2 + y**2) / 2) | |
Z = z_func(X, Y) | |
plot_3d(Z, X, Y) | |
# In[7]: | |
y = np.arange(-10, 10, 0.1) | |
x = np.arange(-10, 10, 0.1) | |
X, Y = np.meshgrid(x, y) # grid of points | |
def z_func(x, y): | |
return x**2 + y**2 - 2*x - 2*y | |
Z = z_func(X, Y) | |
plot_3d(Z, X, Y) | |
# In[78]: | |
y = np.linspace(-20, 20, 50) | |
x = np.linspace(-20, 20, 50) | |
X, Y = np.meshgrid(x, y) # grid of points | |
def z_func(x, y): | |
return x**2 + y**2 - 2*x - 2*y | |
Z = z_func(X, Y) | |
plot_3d(Z, X, Y) | |
# In[81]: | |
y = np.arange(-10, 10, 0.1) | |
x = np.arange(-10, 10, 0.1) | |
X, Y = np.meshgrid(x, y) # grid of points | |
def z_func(x, y): | |
return -x**2 +x*y - y**2 +x - y | |
Z = z_func(X, Y) | |
plot_3d(Z, X, Y) | |
# In[ ]: | |
y = np.arange(-30, 30, 0.1) | |
x = np.arange(-30, 30, 0.1) | |
X, Y = np.meshgrid(x, y) # grid of points | |
def z_func(x, y): | |
return (x-y)**2 | |
Z = z_func(X, Y) | |
plot_3d(Z, X, Y) | |
# In[82]: | |
y = np.arange(-10, 10, 0.1) | |
x = np.arange(-10, 10, 0.1) | |
X, Y = np.meshgrid(x, y) # grid of points | |
def z_func(x, y): | |
return -x**2 + y**4 | |
Z = z_func(X, Y) | |
plot_3d(Z, X, Y) | |
# In[10]: | |
y = np.arange(0, 3, 0.1) | |
x = np.arange(0, 3, 0.1) | |
X, Y = np.meshgrid(x, y) # grid of points | |
def z_func(x, y): | |
return 2-3*x*y+ x**2*y**2 | |
Z = z_func(X, Y) | |
plot_3d(Z, X, Y) | |
# In[4]: | |
y = np.arange(0, 10, 0.1) | |
x = np.arange(0, 10, 0.1) | |
X, Y = np.meshgrid(x, y) # grid of points | |
def z_func(x, y): | |
return 1/(x*y+1) | |
Z = z_func(X, Y) | |
plot_3d(Z, X, Y) | |
# In[ ]: | |
# In[4]: | |
y = np.arange(0, 10, 0.1) | |
x = np.arange(0, 10, 0.1) | |
X, Y = np.meshgrid(x, y) # grid of points | |
def z_func(x, y): | |
return (x-2)**2 + (y-2)**2 | |
Z = z_func(X, Y) | |
plot_3d(Z, X, Y) | |
# In[ ]: | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment