首页 > 解决方案 > Matplotlib 中的三维绘图,点之间有一条线

问题描述

我在处理三个变量的第三维图时遇到问题。

我有三个矩阵:X、Y 和 Z,参数为这种类型:

(x_1, y_1, z_1)

(x_2, y_2, z_2)

.

(x_n, y_n, z_n)

我可以通过 scatter3D 绘图将它们显示在不同的点上。打击是我的代码,objective[0] 是 x,objective[1] 是 y,objective[2] 表示 z:

  from mpl_toolkits import mplot3d
  import matplotlib.pyplot as plt

  fig = plt.figure()
  ax = plt.axes(projection='3d')

  ax.scatter3D([s.objectives[0] for s in algorithm.result],
        [s.objectives[1] for s in algorithm.result],
        [s.objectives[2] for s in algorithm.result])

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

但是,我的问题是当我想在点之间显示一条线时。我想在其中一个矩阵的变化方向上的点之间有一条线,例如 x。

我的意思是,如果我的目标是基于 x 并且假设 x 不断增加,我希望在具有 x_1 的点和具有 x_2 的点之间有一条线,然后在具有 x_2 的点和具有 x_3 的点之间有一条线。 ...

实际上,这条线显示了当 x 变化时(当 x 增加时)我的三个参数如何变化。

为此,我使用了 plot3D,blow 是我的代码:

  ax.plot3D([s.objectives[0] for s in algorithm.result],
        [s.objectives[1] for s in algorithm.result],
        [s.objectives[2] for s in algorithm.result])

但在图中,我在每个点与所有其他点之间都有线。我的意思是 ((n*n-1)/2) 行而不是 n-1 行。

如何使用 mplot3d 实现我的目标?或其他方式。

标签: python-3.xmatplotlib

解决方案


推荐阅读