在matplotlib 3d图中移动刺?

bin*_*nev 3 python 3d plot matplotlib

我正在尝试在3D matplotlib轴对象中移动刺。

这似乎是一个非常简单的问题,但是我没有找到直接解决此问题的任何问题/答案。我在此问题的底部列出了我对该主题的研究列表。

我可以在matplotlib 2D图中设置棘刺的位置。如下代码:

import matplotlib.pyplot as plt, numpy as np

fig, axes = plt.subplots(1, 2)
r, theta = 1, np.linspace(0, 2*np.pi, 100)
x, y = r*np.cos(theta), r*np.sin(theta)
for ax in axes:  # plot the same data on both axes
    ax.plot(x, y)
    ax.set_aspect("equal")
for spine in ax.spines.values():  # adjust spines on last active axis 
    spine.set_position(("data", 0))
Run Code Online (Sandbox Code Playgroud)

产生: 超级病态的2D情节

但是,当我尝试使用3D轴进行相同操作时...

z = np.zeros(x.shape)  # exciting stuff
fig = plt.figure()
for i in range(2):  # create two 3D subplots
    ax = plt.subplot(1,2,i+1, projection="3d", aspect="equal")
    plt.plot(x, y, z)
for spine in ax.spines.values():  # adjust spines on last active axis
    spine.set_position(("data", 0))
Run Code Online (Sandbox Code Playgroud)

上面的代码给了我:

真的很me脚的3D情节

也就是说,即使代码仍在运行,也没有任何效果。另外,对于3D轴,ax.spines看起来像:

OrderedDict([('left', <matplotlib.spines.Spine at 0x120857b8>),
             ('right', <matplotlib.spines.Spine at 0xfd648d0>),
             ('bottom', <matplotlib.spines.Spine at 0xe89e4e0>),
             ('top', <matplotlib.spines.Spine at 0xe89eef0>)])
Run Code Online (Sandbox Code Playgroud)

我不确定在3D轴的上下文中,“左”,“右”,“底部”,“顶部”是指什么。我试过改变其他属性,例如刺的颜色。那里也没有运气。我如何掌握轴上实际的x,y,z刺?

研究:

  • 在撰写本文时,在stackoverflow中搜索“ matplotlib棘3d”会给出5个结果(包括该问题)。
  • mplot3d文档根本没有提到棘刺。
  • 这个问题显示了如何使用设置颜色ax.w_xaxis.set_pane_color(),但是没有类似的ax.w_zaxis.set_spine...方法。
  • 这个问题显示了如何使用设置书脊颜色ax.w_zaxis.line.set_color()。我曾想过要ax.w_zaxis.line.set_data手动设置一个可怕的解决方法,但是它只包含x和y数据。不z!甚至x和y轴也没有z数据。

小智 5

目前似乎没有明显的方法可以执行此操作。未实现轴投影为3D时设置棘刺的操作。然而,有一个小黑客在这里

ax.spines设置用于2D渲染。在projection=3d图形的初始化中进行设置时,某些2D属性(如ax.spines等)将被忽略。这就是为什么在设置2D脊柱时没有任何响应的原因。

3D图形轴线(每个轴的粗黑线)的位置由参数ax.xaxis._axinfo['juggled'](以及y和z轴)确定。这指定将3D绘图边界框的六个外部边界中的哪一个绘制为粗黑线。

您可以通过覆盖juggled值来移动x,y,z轴中每个轴的轴线位置,该值指定哪些轴是主要的,如以下x轴示例所示, 在此处输入图片说明 默认设置 ax.xaxis._axinfo['juggled'] = (1,0,2) 在此处输入图片说明 新设置,ax.xaxis._axinfo['juggled'] = (2,0,1) 所有六个外部边界的参数是 在此处输入图片说明