- 
      
 - 
        
Save aspineon/75690f29ab906a43e3c80d7ccbce1e0b to your computer and use it in GitHub Desktop.  
    Create a Surface Plot from a Matrix #numpy #matplotlib #python
  
        
        
  
    
      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
    
  
  
    
  | import numpy as np | |
| import matplotlib.pyplot as plt | |
| from mpl_toolkits.mplot3d import Axes3D | |
| def surface_plot (matrix, **kwargs): | |
| # acquire the cartesian coordinate matrices from the matrix | |
| # x is cols, y is rows | |
| (x, y) = np.meshgrid(np.arange(matrix.shape[0]), np.arange(matrix.shape[1])) | |
| fig = plt.figure() | |
| ax = fig.add_subplot(111, projection='3d') | |
| surf = ax.plot_surface(x, y, matrix, **kwargs) | |
| return (fig, ax, surf) | |
| # m.shape must be (10,10) | |
| m = np.fromfunction(lambda x, y: np.sin(np.sqrt(x**2 + y**2)), (10, 10)) | |
| (fig, ax, surf) = surface_plot(m, cmap=plt.cm.coolwarm) | |
| fig.colorbar(surf) | |
| ax.set_xlabel('X (cols)') | |
| ax.set_ylabel('Y (rows)') | |
| ax.set_zlabel('Z (values)') | |
| plt.show() | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            