Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: plot wireframe using matplotlib #2324

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
298 changes: 298 additions & 0 deletions Matplotlib/Matplotlib_Plot_Wireframe.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,298 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "b5f17b41-90cd-431c-a773-7803b60a2c12",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"<img width=\"10%\" alt=\"Naas\" src=\"https://landen.imgix.net/jtci2pxwjczr/assets/5ice39g4.png?w=160\"/>"
]
},
{
"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:** [Akshat Katiyar](https://www.linkedin.com/in/akshat-katiyar/)"
]
},
{
"cell_type": "markdown",
"id": "6f001af0-17f9-4c7b-a717-5252ad4a25aa",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Last update:** 2023-10-17 (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": "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,
"id": "f3bf5ca1-d6be-4570-9cb2-b9aea748fb47",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"from mpl_toolkits.mplot3d import Axes3D\n",
"import numpy as np"
]
},
{
"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 ( 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."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1ea98763-a8da-410d-be15-02308b748d5a",
"metadata": {
"papermill": {},
"tags": []
},
"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",
"id": "a534cc4e-f1f1-4ca1-93c1-f00e47b489e4",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Model"
]
},
{
"cell_type": "markdown",
"id": "ea762969-7411-468a-8518-166916798016",
"metadata": {},
"source": [
"### Plot wireframe"
]
},
{
"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": "code",
"execution_count": null,
"id": "da36b0d8-67bc-4e73-bc92-d08a89ec9402",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"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": "4c6b6611-fe35-4248-8b3f-fdff54e2505e",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Output"
]
},
{
"cell_type": "markdown",
"id": "9e6d69af-0dc3-4357-9044-231b8ed2a868",
"metadata": {},
"source": [
"### Save figure"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6234eb28-281a-4d70-bee6-e7a876780109",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"fig_path = \"wireframe.png\"\n",
"fig.savefig(fig_path)"
]
},
{
"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
}
Loading