0

I have the following code of 3D plot and would like to present a slice of it

import matplotlib.pyplot as plt
import numpy as np

ax = plt.figure().add_subplot(projection='3d')

# Make the grid
x, y, z = np.meshgrid(np.arange(-0.8, 1, 0.2),
                     np.arange(-0.8, 1, 0.2),
                     np.arange(-0.8, 1, 0.8))

# Make the direction data for the arrows
u = np.sin(np.pi * x) * np.cos(np.pi * y) * np.cos(np.pi * z)
v = -np.cos(np.pi * x) * np.sin(np.pi * y) * np.cos(np.pi * z)
w = (np.sqrt(2.0 / 3.0) * np.cos(np.pi * x) * np.cos(np.pi * y) *
    np.sin(np.pi * z))

ax.quiver(x, y, z, u, v, w, length=0.1, normalize=True)

plt.show()

How to plot a slice of these arrows when z is zero for example

How to rewrite this part so that I get a 2D slice of the plot?

ax.quiver(x, y, z, u, v, w, length=0.1, normalize=True)

plt.show()

How to plot a slice of these arrows when z is zero for example

Hani
  • 3
  • 2

1 Answers1

0

Evaluates the vector field on the interested slice, like this:

n = 10j # number of discretization points
# discretize a plane
x, y = np.mgrid[-0.8:0.8:n, -0.8:0.8:n]
z = 0 * np.ones_like(x)

u = np.sin(np.pi * x) * np.cos(np.pi * y) * np.cos(np.pi * z)
v = -np.cos(np.pi * x) * np.sin(np.pi * y) * np.cos(np.pi * z)
w = (np.sqrt(2.0 / 3.0) * np.cos(np.pi * x) * np.cos(np.pi * y) *
    np.sin(np.pi * z))

ax = plt.figure().add_subplot(projection='3d')
ax.quiver(x, y, z, u, v, w, length=0.1, normalize=True)
ax.set_zlim(-1, 1)
plt.show()

EDIT to satisfy comment:

Since you are interested at a slice with z=0, it means that w will be zero. Hence, you can plot that slice on a 2D plot:

fig, ax = plt.subplots()
ax.quiver(x, y, u, v)
plt.show()
Davide_sd
  • 10,578
  • 3
  • 18
  • 30
  • Thank you so much but how can I rotate over this slice or at least have it as 2D so I can visualize the arrows clearly. – Hani Dec 13 '22 at 11:22
  • Many Thanks. I tried to modify the code a little bit and I got the following error. ValueError: too many values to unpack (expected 2) for the code line --> 124 ax.quiver(x, y, u, v) – Hani Dec 14 '22 at 01:32