在matplotlib 3d图中移动刺?

时间:2023-01-07 23:36:51

I'm trying to move the spines in a 3D matplotlib axes object.

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

This seems like a really simple issue, but I have not found any questions/answers that address this directly. I've included a list of my research on this topic at the bottom of this question.

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

I can set the position of the spines in matplotlib 2D plots. The following code:

我可以在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))

produces: 在matplotlib 3d图中移动刺?

However, when I try the same thing with a 3D axis...

但是,当我用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))

the above code gives me:

上面的代码给了我:

在matplotlib 3d图中移动刺?

I.e. no effect, even though the code still runs. Also, for the 3D axes, ax.spines looks like:

即没有效果,即使代码仍然运行。此外,对于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>)])

I'm not sure what "left", "right", "bottom", "top" refer to in the context of a 3D axis. I've tried changing other properties like colour of the spines; no luck there either. How can I get hold of the actual x, y, z spines on the axes?

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

Research:

  • searching "matplotlib spines 3d" in * gives 5 results (including this question) at the time of writing.
  • 在*中搜索“matplotlib spines 3d”在编写时给出了5个结果(包括这个问题)。

  • The mplot3d documentation doesn't mention spines at all.
  • mplot3d文档根本没有提到刺。

  • This question shows how to set the pane colour with ax.w_xaxis.set_pane_color(), but there is no similar ax.w_zaxis.set_spine... method.
  • 此问题显示如何使用ax.w_xaxis.set_pane_color()设置窗格颜色,但没有类似的ax.w_zaxis.set_spine ...方法。

  • This question shows how to set the spine colour using ax.w_zaxis.line.set_color(). I thought about making a horrible workaround to set ax.w_zaxis.line.set_data manually, but it only has x and y data; no z! Even the x and y axes don't have z data.
  • 此问题显示如何使用ax.w_zaxis.line.set_color()设置脊椎颜色。我想过制作一个可怕的解决方法来手动设置ax.w_zaxis.line.set_data,但它只有x和y数据;没有z!即使x轴和y轴也没有z数据。

1 个解决方案

#1


3  

There seems to be no obvious way to do this at the moment. Setting the spines when the axis projection is 3D is not implemented. However, there is a small hack here.

目前似乎没有明显的方法可以做到这一点。未实现轴投影为3D时设置刺。但是,这里有一个小小的黑客。

The ax.spines setting is for 2D rendering. When you set projection=3d in the initialization of the figure, certain 2D properties (like ax.spines, etc.) are ignored. It's why you don't get any response when you set the 2D spines.

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

The 3D figure axis line (the thick black line for each axis) locations are determined by the parameter ax.xaxis._axinfo['juggled'] (and similarly for y and z axes). This specifies which of the six outer boundaries of a 3D plot bounding box are plotted as thick black lines.

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

You can shift the position of the axis line for each of x,y,z axis by overwriting the juggled value, which specifies which axis lines are the main ones, as the following example for the x axis, 在matplotlib 3d图中移动刺? the default setting, ax.xaxis._axinfo['juggled'] = (1,0,2) 在matplotlib 3d图中移动刺? new setting, ax.xaxis._axinfo['juggled'] = (2,0,1) The parameters for all the six outer boundaries are, 在matplotlib 3d图中移动刺?

您可以通过覆盖杂项值来移动x,y,z轴每个轴线的位置,该值指定哪些轴线是主轴线,如下面的x轴示例,默认设置ax.xaxis ._axinfo ['juggled'] =(1,0,2)新设置,ax.xaxis._axinfo ['juggled'] =(2,0,1)所有六个外边界的参数是,

#1


3  

There seems to be no obvious way to do this at the moment. Setting the spines when the axis projection is 3D is not implemented. However, there is a small hack here.

目前似乎没有明显的方法可以做到这一点。未实现轴投影为3D时设置刺。但是,这里有一个小小的黑客。

The ax.spines setting is for 2D rendering. When you set projection=3d in the initialization of the figure, certain 2D properties (like ax.spines, etc.) are ignored. It's why you don't get any response when you set the 2D spines.

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

The 3D figure axis line (the thick black line for each axis) locations are determined by the parameter ax.xaxis._axinfo['juggled'] (and similarly for y and z axes). This specifies which of the six outer boundaries of a 3D plot bounding box are plotted as thick black lines.

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

You can shift the position of the axis line for each of x,y,z axis by overwriting the juggled value, which specifies which axis lines are the main ones, as the following example for the x axis, 在matplotlib 3d图中移动刺? the default setting, ax.xaxis._axinfo['juggled'] = (1,0,2) 在matplotlib 3d图中移动刺? new setting, ax.xaxis._axinfo['juggled'] = (2,0,1) The parameters for all the six outer boundaries are, 在matplotlib 3d图中移动刺?

您可以通过覆盖杂项值来移动x,y,z轴每个轴线的位置,该值指定哪些轴线是主轴线,如下面的x轴示例,默认设置ax.xaxis ._axinfo ['juggled'] =(1,0,2)新设置,ax.xaxis._axinfo ['juggled'] =(2,0,1)所有六个外边界的参数是,