-4

So I have obtained a 4-column dataframe with a structure: X, Y, Z, C, where each of the columns contains real values, from very diverse not normalized ranges. I need to plot the data in the following manner:

The first 3 columns, namely 'X', 'Y', and 'Z' should be my x, y, z axis to create a standardized grid (cube). The forth column "C" must be used for coloring the plot.

Could someone provide leads on how this can be done please? Hope my description is understandable.

mcsoini
  • 6,280
  • 2
  • 15
  • 38
Gayane
  • 1
  • 2

1 Answers1

0

you can try this:(ref)

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np


def randrange(n, vmin, vmax):
    return (vmax - vmin)*np.random.rand(n) + vmin

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

n = 100

# For each set of style and range settings, plot n random points in the box
# defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh].
for c, m, zlow, zhigh in [('r', 'o', -50, -25), ('b', '^', -30, -5), 
                         ('g', ',', -10, -5)]:
    xs = randrange(n, 23, 32)
    ys = randrange(n, 0, 100)
    zs = randrange(n, zlow, zhigh)
    ax.scatter(xs, ys, zs, c=c, marker=m)

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

plt.show()

output:

enter image description here

EDIT: add new code block by request:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np


fig = plt.figure()
ax = fig.gca(projection='3d')

# Make data.
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False)

# Customize the z axis.
ax.set_zlim(-1.01, 1.01)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

output:

enter image description here

I'mahdi
  • 23,382
  • 5
  • 22
  • 30
  • Thanks a lot for your response. The problem is that is supposed to be more or less like a heatmap , given that the color needs to be as many as the rows... – Gayane Aug 17 '21 at 14:26
  • @Gayane, i edit answer, see this link : https://matplotlib.org/2.0.2/mpl_toolkits/mplot3d/tutorial.html – I'mahdi Aug 17 '21 at 14:31
  • Thanks a lot, this would probably work. – Gayane Aug 17 '21 at 14:44