0

I am new to constructing 3D plots on Python. I am working on a code in which i am given the data of a ship, in terms of values of different sections. I have successfully been able to arrange the data in a form in which it gives a complete 3D plot of the ship. Here is the section of my code which gives the 3D plot.

fig = plt.figure()
ax = plt.axes(projection="3d")
X = np.array(new_list)
Y= np.array(YQ)
Z = np.array(ZQ)
ax.plot_surface(Y, Z, X, rstride=1, cstride=1,cmap='Purples', edgecolor='none')
ax.set_xlim(-80, 80)
ax.set_ylim(-80, 80)
ax.set_xlabel('Y')
ax.set_ylabel('Z')
ax.set_zlabel('X')
plt.show()

Please ignore me assigning different variables to the conventional ordering of axis. I understand it makes it a bit confusing, but the data was arranged in such a way and I had to work with it. Now I want to assign a different color to the part of the ship submerged in water. In my code, the variable Z represents the height of the ship, so basically I want to assign a different color to all the values less than Z = 0, as all values of Z less than 0 are submerged in water. What changes can I make in my code to do this task?

Zeeshan Rizvi
  • 66
  • 1
  • 2

1 Answers1

0

My idea would be to generate an np.array for the facecolors input variable of ax.plot_surface(). As an example look at this.

So since you want everything with a z-value below 0 in a different color you can create your facecolors thus:

color_above_0 = [1, 1, 1]
color_below_0 = [0, 0, 0]
f_c = np.array([[color_below_0 if val < 0 else color_above_0 for val in z_row] for z_row in Z])

This you can then use as described in the previous link as a facecolors input.

LeoE
  • 2,054
  • 1
  • 11
  • 29