From 981a026a9aa99c3628e342f9909d1f94f9609e1b Mon Sep 17 00:00:00 2001 From: David Rotermund <54365609+davrot@users.noreply.github.com> Date: Tue, 19 Dec 2023 12:05:56 +0100 Subject: [PATCH] Update README.md Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com> --- numpy/mesh_grid/README.md | 40 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/numpy/mesh_grid/README.md b/numpy/mesh_grid/README.md index 6024a8d..bf3de26 100644 --- a/numpy/mesh_grid/README.md +++ b/numpy/mesh_grid/README.md @@ -47,3 +47,43 @@ plt.show() ![image0](image0.png) ![image1](image1.png) + +An example: + +```python +import numpy as np +import matplotlib.pyplot as plt + +x = np.linspace(0, 1, 100) +y = np.linspace(0, 1, 100) + +xv, yv = np.meshgrid(x, y) + +a = np.sin(xv * 2 * np.pi) * np.sin(yv * 8 * np.pi) + +plt.imshow(a, cmap="hot") +plt.xlabel("x axis") +plt.ylabel("y axis") +plt.show() +``` + +![image2](image2.png) + +**The question is if you really need a mesh or if just using broadcasting can do the job too.** I guess this depends on your need. + +```python +import numpy as np +import matplotlib.pyplot as plt + +x = np.linspace(0, 1, 100)[np.newaxis, :] +y = np.linspace(0, 1, 100)[:, np.newaxis] + +a = np.sin(x * 2 * np.pi) * np.sin(y * 8 * np.pi) + +plt.imshow(a, cmap="hot") +plt.xlabel("x axis") +plt.ylabel("y axis") +plt.show() +``` + +![image3](image3.png)