I have data that looks like this:
[
[1, 2, 3, 4, 5], #x
[6, 7, 8, 9, 0], #y
[[1, 2, 3], [0, 2, 3], [1, 7, 3], [1, 2, 9], [1, 1, 3]] #z is 3 values for each one value in x and y
]
How can I plot/visualize such data in matplotlib?
I tried the following but without success:
ax.scatter3D(data[0], data[1], data[2], c=data[2], cmap='Greens');
# and
ax.plot3D(data[0], data[1], data[2], 'gray')
it gives me errors:
# first line ==> ValueError: shape mismatch: objects cannot be broadcast to a single shape
# second line ==> ValueError: input operand has more dimensions than allowed by the axis remapping
CodePudding user response:
You need to reshape your data to expand x and y to match z.
Using pure python:
from itertools import product
a,b,c = zip(*(e for x,y,z in zip(*data) for e in product([x],[y],z)))
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.scatter3D(a, b, c, c=c, cmap='Greens');
Using numpy:
import numpy as np
x, y = np.repeat(np.array(data[:2]), 3, axis=1)
z = data[2]
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.scatter3D(x, y, z, c=z, cmap='Greens')
output:

