Last active
August 25, 2025 18:49
-
-
Save CMCDragonkai/dd420c0800cba33142505eff5a7d2589 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() |
Change:
(x, y) = np.meshgrid(np.arange(matrix.shape[0]), np.arange(matrix.shape[1]))
to:
(x, y) = np.meshgrid(np.arange(matrix.shape[1]), np.arange(matrix.shape[0]))
in order to remove the restriction that m.shape must be square.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

how to defend result a (matrix line * matrix clone) in python as function. help me