matplotlib 我如何编辑我的代码来绘制类似于Python中这个例子的4D轮廓?

jdgnovmf  于 2023-10-24  发布在  Python
关注(0)|答案(1)|浏览(162)

类似于stackoverflow上的许多其他研究人员,他们试图用4D数据绘制等高线图(即X,Y,Z和它们对应的值C),我试图用我的数据绘制一个4D等值线图。我已经尝试了堆栈流中的许多建议的解决方案。从所有建议的this图中,和this最接近我想要的,但在数据解释方面还不完全是我需要的。
Here是数据的一个子集。我把它放在dropbox上。一旦这些数据下载到python文件的目录中,下面的代码就会工作。我已经从this post修改了这个脚本。

  1. import numpy as np
  2. from mpl_toolkits.mplot3d import Axes3D
  3. import matplotlib.pyplot as plt
  4. import matplotlib.tri as mtri
  5. #####Importing the data
  6. df = pd.read_csv('Data_4D_plot.csv')
  7. do_random_pt_example = False;
  8. index_x = 0; index_y = 1; index_z = 2; index_c = 3;
  9. list_name_variables = ['x', 'y', 'z', 'c'];
  10. name_color_map = 'seismic';
  11. if do_random_pt_example:
  12. number_of_points = 200;
  13. x = np.random.rand(number_of_points);
  14. y = np.random.rand(number_of_points);
  15. z = np.random.rand(number_of_points);
  16. c = np.random.rand(number_of_points);
  17. else:
  18. x = df['X'].to_numpy();
  19. y = df['Y'].to_numpy();
  20. z = df['Z'].to_numpy();
  21. c = df['C'].to_numpy();
  22. #end
  23. #-----
  24. # We create triangles that join 3 pt at a time and where their colors will be
  25. # determined by the values of their 4th dimension. Each triangle contains 3
  26. # indexes corresponding to the line number of the points to be grouped.
  27. # Therefore, different methods can be used to define the value that
  28. # will represent the 3 grouped points and I put some examples.
  29. triangles = mtri.Triangulation(x, y).triangles;
  30. choice_calcuation_colors = 2;
  31. if choice_calcuation_colors == 1: # Mean of the "c" values of the 3 pt of the triangle
  32. colors = np.mean( [c[triangles[:,0]], c[triangles[:,1]], c[triangles[:,2]]], axis = 0);
  33. elif choice_calcuation_colors == 2: # Mediane of the "c" values of the 3 pt of the triangle
  34. colors = np.median( [c[triangles[:,0]], c[triangles[:,1]], c[triangles[:,2]]], axis = 0);
  35. elif choice_calcuation_colors == 3: # Max of the "c" values of the 3 pt of the triangle
  36. colors = np.max( [c[triangles[:,0]], c[triangles[:,1]], c[triangles[:,2]]], axis = 0);
  37. #end
  38. #----------
  39. ###=====adjust this part for the labeling of the graph
  40. list_name_variables[index_x] = 'X (m)'
  41. list_name_variables[index_y] = 'Y (m)'
  42. list_name_variables[index_z] = 'Z (m)'
  43. list_name_variables[index_c] = 'C values'
  44. # Displays the 4D graphic.
  45. fig = plt.figure(figsize = (15,15));
  46. ax = fig.gca(projection='3d');
  47. triang = mtri.Triangulation(x, y, triangles);
  48. surf = ax.plot_trisurf(triang, z, cmap = name_color_map, shade=False, linewidth=0.2);
  49. surf.set_array(colors); surf.autoscale();
  50. #Add a color bar with a title to explain which variable is represented by the color.
  51. cbar = fig.colorbar(surf, shrink=0.5, aspect=5);
  52. cbar.ax.get_yaxis().labelpad = 15; cbar.ax.set_ylabel(list_name_variables[index_c], rotation = 270);
  53. # Add titles to the axes and a title in the figure.
  54. ax.set_xlabel(list_name_variables[index_x]); ax.set_ylabel(list_name_variables[index_y]);
  55. ax.set_zlabel(list_name_variables[index_z]);
  56. ax.view_init(elev=15., azim=45)
  57. plt.show()

输出如下:

虽然它看起来很棒,但它并不是我想要的(上面的等高线图示例)。我已经从this post修改了下面的脚本,希望达到所需的图表,但是,图表看起来与我所期望的完全不同(类似于前面的输出图表)。警告:以下代码可能需要一些时间才能运行。

  1. import matplotlib
  2. import matplotlib.pyplot as plt
  3. from mpl_toolkits.mplot3d import Axes3D
  4. import numpy as np
  5. df = pd.read_csv('Data_4D_plot.csv')
  6. x = df['X'].to_numpy();
  7. y = df['Y'].to_numpy();
  8. z = df['Z'].to_numpy();
  9. cc = df['C'].to_numpy();
  10. # convert to 2d matrices
  11. Z = np.outer(z.T, z)
  12. X, Y = np.meshgrid(x, y)
  13. C = np.outer(cc.T,cc)
  14. # fourth dimention - colormap
  15. # create colormap according to cc-value
  16. color_dimension = C # change to desired fourth dimension
  17. minn, maxx = color_dimension.min(), color_dimension.max()
  18. norm = matplotlib.colors.Normalize(minn, maxx)
  19. m = plt.cm.ScalarMappable(norm=norm, cmap='jet')
  20. m.set_array([])
  21. fcolors = m.to_rgba(color_dimension)
  22. # plot
  23. fig = plt.figure()
  24. ax = fig.gca(projection='3d')
  25. ax.plot_surface(X,Y,Z, rstride=1, cstride=1, facecolors=fcolors, vmin=minn, vmax=maxx, shade=False)
  26. ax.set_xlabel('x')
  27. ax.set_ylabel('y')
  28. ax.set_zlabel('z')
  29. plt.show()

现在,我想知道我们的社区和Maven是否可以帮助我绘制一个类似于示例图(本文中的图一)的等值线图,其中等值线基于C范围内的值?

a5g8bdjr

a5g8bdjr1#

碰巧的是(左边的子图),你的(xyz)位于一个平面上,减去测量误差(?),如果你适当地旋转散点图(右边的子图),你可以清楚地看到你的数据的结构。
我建议找到最佳拟合平面,然后你可以绘制两个散点图,一个是 C,一个是残差,第一个是你最终想要的,第二个 * 可能 * 显示残差和 C 之间的一些相关性。

相关问题