Yusuf Falola
Yusuf Falola

Reputation: 35

Viewing 3D array

I have 3D array normalized (between 0 and 1) data, I would like view it 3D format in python like this, with the colors matching the values in the array.

Upvotes: 1

Views: 1200

Answers (1)

mozway
mozway

Reputation: 260300

3D array

You could use matplotlib.pyplot.scatter with a 3D projection:

import numpy as np
import matplotlib.pyplot as plt

a = np.random.random(size=(20, 10, 5))

ax = plt.subplot(projection='3d')

grid = np.meshgrid(np.arange(a.shape[2]),
                   np.arange(a.shape[1]),
                   np.arange(a.shape[0]))

x = ax.scatter(*grid, c=a)
plt.colorbar(x)

3D scatter

2D array

You could use matplotlib.pyplot.imshow:

import numpy as np
import matplotlib.pyplot as plt

a = np.random.random(size=(50,100))

plt.imshow(a, vmin=0, vmax=1)
plt.colorbar()

output:

matplotlib imshow

Upvotes: 1

Related Questions