点制作的3D表面的颜色

huangapple go评论95阅读模式
英文:

Color of a point-made 3D surface

问题

你想要将第一个代码段中的表面颜色从Z值改为C值,对吧?

英文:

I have a set of 4D data (3D points + 1D value). Let's call them X,Y,Z and C. I would like to generate a surface from the X,Y,Z points and then colour it according to the C values.<br> I think I'm asking the same thing that <i>diffracteD</i> did in this question, but nobody seems to have understood what he was asking and the answers and comments aren't helpful.

I was able to create a surface with XYZ data, following this this answer, but then the surface is coloured according to the Z value, not the C value as I want. <br>

On the other end, the <i>original answer</i> to this question manages to colour the surface using the C value, but in this case Z is a function of X and Y, not a free variable as in my case.

My goal is to somewhat merge the two things, creating a surface from XYZ data and colouring it according to C

Here is what I've done so far:

  1. XYZ independent but surface coloured with Z values:
  1. import matplotlib.pyplot as plt
  2. from matplotlib.ticker import MaxNLocator
  3. from matplotlib import cm
  4. import numpy as np
  5. from numpy.random import randn
  6. from scipy import array, newaxis
  7. # ======
  8. ## data:
  9. DATA = np.array([
  10. [-0.807237702464, 0.904373229492, 111.428744443],
  11. [-0.802470821517, 0.832159465335, 98.572957317],
  12. [-0.801052795982, 0.744231916692, 86.485869328],
  13. [-0.802505546206, 0.642324228721, 75.279804677],
  14. [-0.804158144115, 0.52882485495, 65.112895758],
  15. [-0.806418040943, 0.405733109371, 56.1627277595],
  16. [-0.808515314192, 0.275100227689, 48.508994388],
  17. [-0.809879521648, 0.139140394575, 42.1027499025],
  18. [-0.810645106092, -7.48279012695e-06, 36.8668106345],
  19. [-0.810676720161, -0.139773175337, 32.714580273],
  20. [-0.811308686707, -0.277276065449, 29.5977405865],
  21. [-0.812331692291, -0.40975978382, 27.6210856615],
  22. [-0.816075037319, -0.535615685086, 27.2420699235],
  23. [-0.823691366944, -0.654350489595, 29.1823292975],
  24. [-0.836688691603, -0.765630198427, 34.2275056775],
  25. [-0.854984518665, -0.86845932028, 43.029581434],
  26. [-0.879261949054, -0.961799684483, 55.9594146815],
  27. [-0.740499820944, 0.901631050387, 97.0261463995],
  28. [-0.735011699497, 0.82881933383, 84.971061395],
  29. [-0.733021568161, 0.740454485354, 73.733621269],
  30. [-0.732821755233, 0.638770044767, 63.3815970475],
  31. [-0.733876941678, 0.525818698874, 54.0655910105],
  32. [-0.735055978521, 0.403303715698, 45.90859502],
  33. [-0.736448900325, 0.273425879041, 38.935709456],
  34. [-0.737556181137, 0.13826504904, 33.096106049],
  35. [-0.738278724065, -9.73058423274e-06, 28.359664343],
  36. [-0.738507612286, -0.138781586244, 24.627237837],
  37. [-0.738539663773, -0.275090412979, 21.857410904],
  38. [-0.739099040189, -0.406068448513, 20.1110519655],
  39. [-0.741152200369, -0.529726022182, 19.7019157715],
  40. ])
  41. Xs = DATA[:,0]
  42. Ys = DATA[:,1]
  43. Zs = DATA[:,2]
  44. # ======
  45. ## plot:
  46. fig = plt.figure()
  47. ax = fig.add_subplot(111, projection=&#39;3d&#39;)
  48. #cmap=&quot;hot&quot; colours w.r.t. Z values
  49. surf = ax.plot_trisurf(Xs, Ys, Zs, cmap=&quot;hot&quot;, linewidth=0)
  50. fig.colorbar(surf)
  51. ax.xaxis.set_major_locator(MaxNLocator(5))
  52. ax.yaxis.set_major_locator(MaxNLocator(6))
  53. ax.zaxis.set_major_locator(MaxNLocator(5))
  54. fig.tight_layout()
  55. plt.show()

点制作的3D表面的颜色

  1. Surface coloured with C values, but Z is a function of X,Y
  1. from mpl_toolkits.mplot3d import Axes3D
  2. from matplotlib import cm
  3. import matplotlib.pyplot as plt
  4. import numpy as np
  5. fig = plt.figure()
  6. ax = fig.add_subplot(projection=&#39;3d&#39;)
  7. # as plot_surface needs 2D arrays as input
  8. x = np.arange(10)
  9. y = np.array(range(10,15))
  10. # we make a meshgrid from the x,y data
  11. X, Y = np.meshgrid(x, y)
  12. Z = np.sin(np.sqrt(X**2 + Y**2))
  13. # data_value shall be represented by color
  14. data_value = np.random.rand(len(y), len(x))
  15. # map the data to rgba values from a colormap
  16. colors = cm.ScalarMappable(cmap = &quot;hot&quot;).to_rgba(data_value)
  17. # plot_surface with points X,Y,Z and data_value as colors
  18. surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=colors,
  19. linewidth=0, antialiased=True)
  20. fig.colorbar(surf)
  21. plt.show()

点制作的3D表面的颜色

Is there a way to modify the first code to make it use C values instead of Z values to colour the surface?
Thanks.

答案1

得分: 3

这是一种使用trisurf将三角形的面颜色设置为替代方法,只需确保C值与三角形的数量相同:

  1. Xs = DATA[:,0]
  2. Ys = DATA[:,1]
  3. Zs = DATA[:,2]
  4. fig = plt.figure()
  5. ax = fig.add_subplot(111, projection='3d')
  6. surf = ax.plot_trisurf(Xs, Ys, Zs, linewidth=0)

我们可以清楚地在图中找到这些三角形:
点制作的3D表面的颜色

然后只需要为这些三角形生成颜色:

  1. # 找到三角形的切片
  2. slices = surf._segslices
  3. # 数据值将由颜色表示
  4. data_value = np.random.rand(len(slices))
  5. # 将数据映射到来自热度图的rgba值
  6. colors = cm.ScalarMappable(cmap="hot").to_rgba(data_value)
  7. # 设置面颜色
  8. surf.set_fc(colors)
  9. cbar = fig.colorbar(cm.ScalarMappable(cmap="hot"), ax=ax)
  10. ax.xaxis.set_major_locator(MaxNLocator(5))
  11. ax.yaxis.set_major_locator(MaxNLocator(6))
  12. ax.zaxis.set_major_locator(MaxNLocator(5))
  13. fig.tight_layout()
  14. plt.show()

获取图形:
点制作的3D表面的颜色

英文:

Here is an alternative method with setting the face colors to the triangles in the trisurf, just need to make the C values have the same number of the triangles:

  1. Xs = DATA[:,0]
  2. Ys = DATA[:,1]
  3. Zs = DATA[:,2]
  4. fig = plt.figure()
  5. ax = fig.add_subplot(111, projection=&#39;3d&#39;)
  6. surf = ax.plot_trisurf(Xs, Ys, Zs, linewidth=0)

We can find the triangles in the figure clearly :
点制作的3D表面的颜色

Then just need to generate the colors for the triangles:

  1. # Find the slices of the triangles
  2. slices = surf._segslices
  3. # data_value shall be represented by color
  4. data_value = np.random.rand(len(slices))
  5. # map the data to rgba values from a colormap
  6. colors = cm.ScalarMappable(cmap = &quot;hot&quot;).to_rgba(data_value)
  7. # set the face colors
  8. surf.set_fc(colors)
  9. cbar = fig.colorbar(cm.ScalarMappable(cmap=&quot;hot&quot;), ax=ax)
  10. ax.xaxis.set_major_locator(MaxNLocator(5))
  11. ax.yaxis.set_major_locator(MaxNLocator(6))
  12. ax.zaxis.set_major_locator(MaxNLocator(5))
  13. fig.tight_layout()
  14. plt.show()

Get the figure :
点制作的3D表面的颜色

答案2

得分: 0

I ended up following HMH1013 suggestion and use plotly.
Here is the code:

  1. import plotly.graph_objects as go
  2. import numpy as np
  3. #data.dat has 4 cols of numbers: X, Y, Z, C
  4. DATA = np.loadtxt(open("data.dat", "rb"))
  5. Xs = DATA[:,0]
  6. Ys = DATA[:,1]
  7. Zs = DATA[:,2]
  8. values = DATA[:,3]
  9. fig = go.Figure(data=[
  10. go.Mesh3d(
  11. x=Xs,
  12. y=Ys,
  13. z=Zs,
  14. colorbar_title='value',
  15. colorscale="jet",
  16. intensity=values,
  17. showscale=True
  18. )
  19. ])
  20. fig.show()

点制作的3D表面的颜色

In this way C needs to have the same dimension of X, Y, and Z and not the one of the triangles.

Many thanks to HMH1013 for helping me.

英文:

I ended up following <i>HMH1013</i> suggestion and use plotly.
Here is the code:

  1. import plotly.graph_objects as go
  2. import numpy as np
  3. #data.dat has 4 cols of numbers: X, Y, Z, C
  4. DATA = np.loadtxt(open(&quot;data.dat&quot;, &quot;rb&quot;))
  5. Xs = DATA[:,0]
  6. Ys = DATA[:,1]
  7. Zs = DATA[:,2]
  8. values = DATA[:,3]
  9. fig = go.Figure(data=[
  10. go.Mesh3d(
  11. x=Xs,
  12. y=Ys,
  13. z=Zs,
  14. colorbar_title=&#39;value&#39;,
  15. colorscale=&quot;jet&quot;,
  16. intensity=values,
  17. showscale=True
  18. )
  19. ])
  20. fig.show()

点制作的3D表面的颜色

In this way C needs to have the same dimension of X,Y and Z and not the one of the triangles.

Many thanks to <i>HMH1013</i> for helping me.

huangapple
  • 本文由 发表于 2023年5月10日 20:38:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218532.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定