5

我正在使用 matplotlib 构建一个 3D 散点图,但无法使生成的图形具有所有 3 个轴的共同原点。我怎样才能做到这一点?

我的代码(到目前为止),我还没有为轴规范实现任何定义,因为我对 Python 很陌生:

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


  # imports specific to the plots in this example
  import numpy as np
  from matplotlib import cm
  from mpl_toolkits.mplot3d.axes3d import get_test_data

  def randrange(n, vmin, vmax):
  return (vmax-vmin)*np.random.rand(n) + vmin

  # Same as wide as it is tall.
  fig = plt.figure(figsize=plt.figaspect(1.0))


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

  n = 512

  xs = np.random.randint(0, 10, size=n)
  ys = np.random.randint(0, 10, size=n)
  zs = np.random.randint(0, 10, size=n)
  colors = np.random.randint(0, 10, size=n)
  scat = ax.scatter(xs, ys, zs, c=colors, marker='o')

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


  fig.colorbar(scat, shrink=0.5, aspect=10)

  plt.show()
4

2 回答 2

1

Probably you mean something as follows (from Mathematica):

Mathematica

At the present time I can only plot some lines centered at (0,0,0):

xline=((min(data[:,0]),max(data[:,0])),(0,0),(0,0))
ax.plot(xline[0],xline[1],xline[2],'grey')
yline=((0,0),(min(data[:,1]),max(data[:,1])),(0,0))
ax.plot(yline[0],yline[1],yline[2],'grey')
zline=((0,0),(0,0),(min(data[:,2]),max(data[:,2])))
ax.plot(zline[0],zline[1],zline[2],'grey')

This is the result:

Matplotlib

于 2013-10-29T15:11:12.713 回答
0

如果你把线

ax.view_init(30,220)

就在之前

plt.show()

你应该得到你想要的 xy 平面。不知道如何将该平面移动到 z=0。

于 2014-11-25T17:39:27.070 回答