From 7b9493e54b5e3acaf2e1e9cd4b08187128d377ae Mon Sep 17 00:00:00 2001 From: Florent Ravenel Date: Mon, 9 Oct 2023 09:32:52 +0200 Subject: [PATCH 1/2] feat(Matplotlib): Add Plot Wireframe --- Matplotlib/Matplotlib_Plot_Wireframe.ipynb | 263 +++++++++++++++++++++ 1 file changed, 263 insertions(+) create mode 100644 Matplotlib/Matplotlib_Plot_Wireframe.ipynb diff --git a/Matplotlib/Matplotlib_Plot_Wireframe.ipynb b/Matplotlib/Matplotlib_Plot_Wireframe.ipynb new file mode 100644 index 0000000000..e797fbef5d --- /dev/null +++ b/Matplotlib/Matplotlib_Plot_Wireframe.ipynb @@ -0,0 +1,263 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "b5f17b41-90cd-431c-a773-7803b60a2c12", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "\"Naas\"" + ] + }, + { + "cell_type": "markdown", + "id": "324ca40c-7bbf-45fc-8ca9-c72c76a4b1cb", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "# Matplotlib - Plot Wireframe" + ] + }, + { + "cell_type": "markdown", + "id": "98f14a42-397f-45df-a4f9-017c853fc650", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Tags:** #matplotlib #plot #3d #wireframe #visualization #data" + ] + }, + { + "cell_type": "markdown", + "id": "97696608-6737-44ea-bc52-357d2484c4e4", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Author:** [Firstname Lastname](https://www.linkedin.com/in/xxxx)" + ] + }, + { + "cell_type": "markdown", + "id": "6f001af0-17f9-4c7b-a717-5252ad4a25aa", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Last update:** 2023-10-09 (Created: 2023-10-09)" + ] + }, + { + "cell_type": "markdown", + "id": "ddb43910-ae2b-41b8-a13f-f0da8ce1a67c", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Description:** This notebook will demonstrate how to plot a wireframe using Matplotlib. It is usefull for data visualization." + ] + }, + { + "cell_type": "markdown", + "id": "50f8a14a-669a-49ad-b00d-ddfa2b03f7b6", + "metadata": { + "papermill": {}, + "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)" + ] + }, + { + "cell_type": "markdown", + "id": "671abfca-e7e4-46e8-9011-c510869d24da", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Input" + ] + }, + { + "cell_type": "markdown", + "id": "bb0f5e4f-fc6e-4fe1-871a-866c13021a3a", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Import libraries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f3bf5ca1-d6be-4570-9cb2-b9aea748fb47", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": "import matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d import Axes3D\nimport numpy as np", + "outputs": [] + }, + { + "cell_type": "markdown", + "id": "fe06db29-912f-4fdf-98af-bc134a9bb6b6", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Setup variables\n- `x`: x-axis values\n- `y`: y-axis values\n- `z`: z-axis values" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1ea98763-a8da-410d-be15-02308b748d5a", + "metadata": { + "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": [] + }, + { + "cell_type": "markdown", + "id": "a534cc4e-f1f1-4ca1-93c1-f00e47b489e4", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Model" + ] + }, + { + "cell_type": "markdown", + "id": "ea57cfa9-7955-46b3-997c-87b4f507b7e8", + "metadata": { + "papermill": {}, + "tags": [] + }, + "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": "4c6b6611-fe35-4248-8b3f-fdff54e2505e", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Output" + ] + }, + { + "cell_type": "markdown", + "id": "a86759fc-94b0-4657-96a8-036fc5e6f18a", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Display result" + ] + }, + { + "cell_type": "markdown", + "id": "a86c6553-8e42-4d5e-9cff-026b0a5bc035", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "The result is a 3D wireframe plot of the sinusoidal function." + ] + }, + { + "cell_type": "markdown", + "id": "1f3da44b-6265-4dcc-9679-5f51f78a40a9", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "![alt text](https://github.com/callysto/curriculum-notebooks/blob/master/notebooks/images/Matplotlib_Wireframe.png?raw=true)" + ] + }, + { + "cell_type": "markdown", + "id": "143fbdee-b935-481f-b6c5-8493253e0ddc", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + " " + ] + }, + { + "cell_type": "markdown", + "id": "4e213a63-4f1a-4997-adc3-936e34be36ee", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + " " + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "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.9.6" + }, + "widgets": { + "application/vnd.jupyter.widget-state+json": { + "state": {}, + "version_major": 2, + "version_minor": 0 + } + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} \ No newline at end of file From c6660e0cf07193050343e2f547fc32bdca63b89d Mon Sep 17 00:00:00 2001 From: YellowFoxH4XOR Date: Tue, 17 Oct 2023 11:04:11 +0200 Subject: [PATCH 2/2] feat: plot wireframe using matplotlib --- Matplotlib/Matplotlib_Plot_Wireframe.ipynb | 113 ++++++++++++++------- 1 file changed, 74 insertions(+), 39 deletions(-) diff --git a/Matplotlib/Matplotlib_Plot_Wireframe.ipynb b/Matplotlib/Matplotlib_Plot_Wireframe.ipynb index e797fbef5d..e3671195c0 100644 --- a/Matplotlib/Matplotlib_Plot_Wireframe.ipynb +++ b/Matplotlib/Matplotlib_Plot_Wireframe.ipynb @@ -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/)" ] }, { @@ -52,7 +52,7 @@ "tags": [] }, "source": [ - "**Last update:** 2023-10-09 (Created: 2023-10-09)" + "**Last update:** 2023-10-17 (Created: 2023-10-09)" ] }, { @@ -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)" ] }, { @@ -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, @@ -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", @@ -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." ] }, { @@ -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", @@ -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)" ] }, { @@ -260,4 +295,4 @@ }, "nbformat": 4, "nbformat_minor": 5 -} \ No newline at end of file +}