【问题标题】:Move spines in matplotlib 3d plot?在matplotlib 3d图中移动刺?
【发布时间】:2018-07-04 16:37:24
【问题描述】:

我正在尝试移动 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))

产生:

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

上面的代码给了我:

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

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

研究:

  • 在撰写本文时,在 stackoverflow 中搜索“matplotlib spines 3d”会得到 5 个结果(包括这个问题)。
  • mplot3d 文档根本没有提到脊椎。
  • This question 展示了如何使用ax.w_xaxis.set_pane_color() 设置窗格颜色,但没有类似的ax.w_zaxis.set_spine... 方法。
  • This question 展示了如何使用 ax.w_zaxis.line.set_color() 设置书脊颜色。我想过做一个可怕的解决方法来手动设置ax.w_zaxis.line.set_data,但它只有 x 和 y 数据;没有z!甚至 x 和 y 轴也没有 z 数据。

【问题讨论】:

  • 这肯定不是第一次被问到。请务必查看有关此问题的先前问题。我的猜测是实际上不可能以预期的方式移动轴,但我不能肯定地说。
  • @ImportanceOfBeingErnest 是的,我很感激。这是我在问这个问题之前已经搜索了很多次的东西。老实说,很难找到任何甚至提到 3d 脊椎的东西(包括 matplotlib 文档)。针对您的评论,我添加了其他问题的简短摘要,以表明我无法找到现有答案。

标签: python matplotlib plot 3d


【解决方案1】:

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

ax.spines 设置用于 2D 渲染。在图的初始化中设置projection=3d 时,某些二维属性(如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) 所有六个外部边界的参数是,

【讨论】:

  • 有没有办法设置刺线宽?
  • 2d可以单独设置书脊宽度,使用ax.spines['left'].set_linewidth(x),其中x是你想要的线宽,left可以改成其他('right', 'top', 'bottom')。对于3d,我认为你不能单独设置(axis3d的源代码在matplotlib.org/3.1.3/_modules/mpl_toolkits/mplot3d/axis3d.html),但是你可以使用rcParams['axes.linewidth'] = x将线宽全局设置为x
猜你喜欢
  • 2012-07-07
  • 1970-01-01
  • 2012-12-09
  • 1970-01-01
  • 1970-01-01
  • 2016-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多