Last active
January 19, 2019 11:41
-
-
Save czonios/555105a7fa6c100ddc7fc72cbf17bd99 to your computer and use it in GitHub Desktop.
A simple Jupyter notebook for visualizing scalar-valued functions of two variables.
This file contains hidden or 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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Plotting multivariate functions $\\rightarrow f(x,y) = z$\n", | |
"\n", | |
"### Step 1: Import libraries" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"%matplotlib notebook\n", | |
"import numpy as np\n", | |
"from mpl_toolkits.mplot3d import Axes3D\n", | |
"from matplotlib import cm, colors\n", | |
"from matplotlib import pyplot as plt" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Step 2: Define functions\n", | |
"\n", | |
"Both $f: \\mathbb{R}^2 \\rightarrow \\mathbb{R}$\n", | |
"\n", | |
"$f_1(x,y) = x^2 - y^2$\n", | |
"\n", | |
"$f_2(x,y) = 1 - (x^2 + y^2)$" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"f_1 = lambda x,y: x ** 2 - y ** 2\n", | |
"f_2 = lambda x,y: 1 - (x ** 2 + y ** 2)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Step 3: Create sample inputs and get outputs\n", | |
"\n", | |
"$x,y \\in [-1, 1]$" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"x = np.arange(-1, 1, 0.025)\n", | |
"y = np.arange(-1, 1, 0.025)\n", | |
"X, Y = np.meshgrid(x, y)\n", | |
"\n", | |
"Z = f_1(X, Y)\n", | |
"W = f_2(X, Y)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Step 4: Plot the surfaces" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"scrolled": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# Create colors from normalized output range\n", | |
"norm = colors.Normalize(Z.min(), Z.max())\n", | |
"z_color = cm.jet(norm(Z))\n", | |
"norm_2 = colors.Normalize(W.min(), W.max())\n", | |
"w_color = cm.jet(norm_2(W))\n", | |
"\n", | |
"# Create a figure with enough space for 2 rows\n", | |
"fig = plt.figure(figsize=(8, 12))\n", | |
"\n", | |
"# plot f_1 on first row\n", | |
"ax = fig.add_subplot(2,1,1,projection='3d')\n", | |
"ax.plot_surface(X, Y, Z,\n", | |
" rstride=1, cstride=1, alpha=1, facecolors=z_color)\n", | |
"\n", | |
"# plot f_2 on second row\n", | |
"ax_imag = fig.add_subplot(2,1,2,projection='3d')\n", | |
"ax_imag.plot_surface(X, Y, W,\n", | |
" rstride=1, cstride=1, alpha=0.5, facecolors=w_color)\n", | |
"\n", | |
"plt.savefig('sample.png', transparent=True)\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Exporting & Importing Figures using pickle " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Save object as pickle\n", | |
"import pickle\n", | |
"pickle.dump(fig, open('FigureObject.fig.pickle', 'wb')) # This is for Python 3 - py2 may need `file` instead of `open`\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Read figure from pickle\n", | |
"\n", | |
"import pickle\n", | |
"figx = pickle.load(open('FigureObject.fig.pickle', 'rb'))\n", | |
"\n", | |
"figx.show()\n", | |
"\n", | |
"# Can also view data\n", | |
"data = figx.axes[0]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python (thesis)", | |
"language": "python", | |
"name": "thesis" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample figure of two plots created with this notebook: