Skip to content

Commit

Permalink
feat: plot wireframe using matplotlib
Browse files Browse the repository at this point in the history
  • Loading branch information
YellowFoxH4XOR committed Oct 17, 2023
1 parent 7b9493e commit c6660e0
Showing 1 changed file with 74 additions and 39 deletions.
113 changes: 74 additions & 39 deletions Matplotlib/Matplotlib_Plot_Wireframe.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"tags": []
},
"source": [
"**Author:** [Firstname Lastname](https://www.linkedin.com/in/xxxx)"
"**Author:** [Akshat Katiyar](https://www.linkedin.com/in/akshat-katiyar/)"
]
},
{
Expand All @@ -52,7 +52,7 @@
"tags": []
},
"source": [
"**Last update:** 2023-10-09 (Created: 2023-10-09)"
"**Last update:** 2023-10-17 (Created: 2023-10-09)"
]
},
{
Expand All @@ -74,7 +74,9 @@
"tags": []
},
"source": [
"**References:**\n- [Matplotlib - Plot Wireframe](https://matplotlib.org/stable/plot_types/3D/wire3d_simple.html)\n- [Matplotlib - 3D Plotting](https://matplotlib.org/3.2.1/tutorials/toolkits/mplot3d.html)"
"**References:**\n",
"- [Matplotlib - Plot Wireframe](https://matplotlib.org/stable/plot_types/3D/wire3d_simple.html)\n",
"- [Matplotlib - 3D Plotting](https://matplotlib.org/3.2.1/tutorials/toolkits/mplot3d.html)"
]
},
{
Expand All @@ -99,6 +101,16 @@
"### Import libraries"
]
},
{
"cell_type": "markdown",
"id": "9e952cad-9b11-42ed-acf7-9e032902a917",
"metadata": {},
"source": [
"- numpy (np): A powerful library for numerical operations in Python, particularly for handling arrays and mathematical functions.\n",
"- matplotlib.pyplot (plt): The plotting library used to create visualizations like plots and charts.\n",
"- mpl_toolkits.mplot3d.Axes3D: A toolkit in Matplotlib to create 3D plots."
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -107,8 +119,12 @@
"papermill": {},
"tags": []
},
"source": "import matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d import Axes3D\nimport numpy as np",
"outputs": []
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"from mpl_toolkits.mplot3d import Axes3D\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
Expand All @@ -118,7 +134,13 @@
"tags": []
},
"source": [
"### Setup variables\n- `x`: x-axis values\n- `y`: y-axis values\n- `z`: z-axis values"
"### Setup variables\n",
"- `x`: x-axis values\n",
"- `y`: y-axis values\n",
"- `Z`: z-axis values ( We use the X and Y values to compute Z based on a combination of sine and cosine functions.)\n",
"- np.linspace: Creates an array of evenly spaced values over a specified range.\n",
"- np.meshgrid: Creates a mesh grid for the given 1D arrays x and y.\n",
"- np.sin and np.cos: Compute the sine and cosine of each element in the input arrays."
]
},
{
Expand All @@ -129,8 +151,13 @@
"papermill": {},
"tags": []
},
"source": "x = np.linspace(-5, 5, 50)\ny = np.linspace(-5, 5, 50)\nX, Y = np.meshgrid(x, y)\nR = np.sqrt(X**2 + Y**2)\nZ = np.sin(R)",
"outputs": []
"outputs": [],
"source": [
"x = np.linspace(-5, 5, 50)\n",
"y = np.linspace(-5, 5, 50)\n",
"X, Y = np.meshgrid(x, y)\n",
"Z = np.sin(np.sqrt(X**2 + Y**2)) + np.cos(X + Y)"
]
},
{
"cell_type": "markdown",
Expand All @@ -145,68 +172,76 @@
},
{
"cell_type": "markdown",
"id": "ea57cfa9-7955-46b3-997c-87b4f507b7e8",
"metadata": {
"papermill": {},
"tags": []
},
"id": "ea762969-7411-468a-8518-166916798016",
"metadata": {},
"source": [
"### Plot wireframe"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "84592261-012a-4f88-884b-cab344972042",
"metadata": {
"papermill": {},
"tags": []
},
"source": "fig = plt.figure()\nax = Axes3D(fig)\nax.plot_wireframe(X, Y, Z, color=\"green\")",
"outputs": []
"cell_type": "markdown",
"id": "1e3a1c52-4f5f-4b9e-b615-e079a63d4708",
"metadata": {},
"source": [
"- ax.plot_wireframe: Plots a 3D wireframe plot.\n",
"- X, Y, Z: The data for the plot (coordinates and values).\n",
"- rstride and cstride: The row and column stride (step size) for sampling the data to create the wireframe.\n",
"- linewidth: The width of the lines in the wireframe.\n",
"- cmap: The colormap for coloring the wireframe."
]
},
{
"cell_type": "markdown",
"id": "4c6b6611-fe35-4248-8b3f-fdff54e2505e",
"cell_type": "code",
"execution_count": null,
"id": "da36b0d8-67bc-4e73-bc92-d08a89ec9402",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"## Output"
"fig = plt.figure(figsize=(10, 8))\n",
"# In this code, we're creating a new figure (fig) and adding a 3D subplot to it using fig.add_subplot.\n",
"# The projection='3d' argument specifies that the subplot should be a 3D plot, and Axes3D is used internally to handle the 3D plotting capabilities.\n",
"ax = fig.add_subplot(111, projection='3d')\n",
"ax.plot_wireframe(X, Y, Z, rstride=5, cstride=5, linewidth=1, cmap='viridis')\n",
"ax.set_xlabel('X')\n",
"ax.set_ylabel('Y')\n",
"ax.set_zlabel('Z')\n",
"ax.set_title('Wireframe Plot')\n",
"ax.view_init(elev=30, azim=30)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "a86759fc-94b0-4657-96a8-036fc5e6f18a",
"id": "4c6b6611-fe35-4248-8b3f-fdff54e2505e",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Display result"
"## Output"
]
},
{
"cell_type": "markdown",
"id": "a86c6553-8e42-4d5e-9cff-026b0a5bc035",
"metadata": {
"papermill": {},
"tags": []
},
"id": "9e6d69af-0dc3-4357-9044-231b8ed2a868",
"metadata": {},
"source": [
"The result is a 3D wireframe plot of the sinusoidal function."
"### Save figure"
]
},
{
"cell_type": "markdown",
"id": "1f3da44b-6265-4dcc-9679-5f51f78a40a9",
"cell_type": "code",
"execution_count": null,
"id": "6234eb28-281a-4d70-bee6-e7a876780109",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"![alt text](https://github.com/callysto/curriculum-notebooks/blob/master/notebooks/images/Matplotlib_Wireframe.png?raw=true)"
"fig_path = \"wireframe.png\"\n",
"fig.savefig(fig_path)"
]
},
{
Expand Down Expand Up @@ -260,4 +295,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
}
}

0 comments on commit c6660e0

Please sign in to comment.