Python Forum

Full Version: 3D Plotting in Matplotlib for Python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone, I'm very new with Python. I found this code and I would like to change the color of a specific position in this 3d-matrix.

Thanks in advance,

Regards,


[Image: e1DQ6kA.png]

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



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

z = [8,7,6,7,9,10,9,8,10,8,9,10,9,8,10,8,9,10,9,8,10,8,9,10]
x = [1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,4,4,4,4,4,4]
y = [1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6]


ax.scatter(x, y, z, c='r', marker='o')
    

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

plt.show()
As it could be seen from the official docs, c parameter in the scatter function could be a vector, consisting of
corresponding colors to each point.

# changes in your code
colored_inds = [10, 12, 15]  # points #10, 12 and 15 will be blue.
colors = ['r' if j not in colored_inds else 'b' for j in range(len(x))]
ax.scatter(x, y, z, c=colors, marker='o')