<jupyter_start><jupyter_text>Visualizing Two Variable FunctionsWe wil make the surface plots of two variable functions of the form $z = f(x,y)$ and generate animation with rotating azimuth angle in the 3D surface plot using python, matplotlib.pyplot, and matplotlib.animation.FuncAnimation.<jupyter_code>#%matplotlib inline  # This displayes static plots in jupytrer environment.
%matplotlib notebook 
                     # Use this to enable 3D plot interactive.  
                     # Don't combine this with %matplotlib inline
import numpy as np

import matplotlib.pyplot as plt
from matplotlib import animation
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D<jupyter_output><empty_output><jupyter_text>Basic Surface Plot**Example 1.** Plot $f(x,y) = \sqrt{x}+\sqrt{y}$.<jupyter_code># Make datax 
x = y = np.linspace(0., 5., 41)
xv, yv = np.meshgrid(x, y, indexing='ij', sparse=False)

fv = np.sqrt(xv)+np.sqrt(yv)  
# Get instance of Axis3D
fig = plt.figure(figsize=(8,5))
ax = fig.gca(projection='3d')

# Plot the surface
ax.plot_surface(xv, yv, fv, cmap=cm.coolwarm, rstride=1, cstride=1)
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
#ax.set_zlabel('Z axis')<jupyter_output><empty_output><jupyter_text>Animate a rotating 3D graph in matplotlib* [Source](https://pythonmatplotlibtips.blogspot.com/2018/01/rotate-azimuth-angle-animation-3d-python-matplotlib-pyplot.html)<jupyter_code>X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

# Create a figure and a 3D Axes
fig = plt.figure()
ax = Axes3D(fig)

def init():
    # Plot the surface.
    ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False)
    return fig,

def animate(i):
    # azimuth angle : 0 deg to 360 deg
    ax.view_init(elev=10, azim=i*4)
    return fig,

# Animate
ani = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=90, interval=50, blit=True)

fn = 'rotate_azimuth_angle_3d_surf'
#filename_mp4='animation/'+fn+'.mp4'
filename_gif='animation/'+fn+'.gif'
#ani.save(filename_mp4,writer='ffmpeg',fps=1000/50)
ani.save(filename_gif,writer='imagemagick',fps=1000/50)
## This doesn't work on jupyeter 
#import subprocess
#cmd = 'magick convert %s.gif -fuzz 5%% -layers Optimize %s_r.gif'%(fn,fn)
#subprocess.check_output(cmd)

## This works on command line only
##convert rotate.gif -fuzz 5% -layers Optimize rotater.gif
##magick "rotate.gif" -fuzz 5% -layers Optimize "rotater.gif"
#plt.rcParams['animation.html'] = 'html5'
#ani<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13453430433116137, 'fraction_numerical': 0.026446914526638558, 'mean_word_length': 4.403726708074534, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '287115', 'license': 'MIT', 'n_tokens_mistral': 1033, 'n_tokens_neox': 964, 'n_words': 236, 'path': 'T1_3_visualizing_two_variable_functions.ipynb', 'repo_name': 'bkimo/Multivariable_Calculus_with_Python', 'size': '2609'}