移动 3D 图以避免边距裁剪

Move 3D plot to avoid clipping by margins

我想弄清楚如何让下面的 3D matplotlib 图像在 canvas 上绘制得更高,这样它就不会被剪裁。这是我用来创建情节的代码。我找不到附加包含 Z 高程的文本文件的方法(在下面的代码中引用),但它只是一个二维数组,包含一个由 0 到 1 之间的值组成的表面。

import os
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from mpl_toolkits.mplot3d import Axes3D

nrow=30
ncol=100

f = open(r'C:\temp\fracEvapCume_200.txt','r')
fracEvapTS = np.loadtxt(f)
f.close()

X, Y = np.meshgrid(ncol, nrow)
Y3d, X3d = np.mgrid[0:Y, 0:X]

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.auto_scale_xyz([0, 100], [0, 30], [0, 0.2])
Y3d, X3d = np.mgrid[0:Y, 0:X]
Z = fracEvapTS

surf = ax.plot_surface(X3d, Y3d, Z, cmap='autumn', cstride=2, rstride=2)
ax.set_xlabel("X-Label")
ax.set_ylabel("Y-Label")
ax.set_zlabel("Z-Label")
ax.pbaspect = [1., .33, 0.25]
ax.dist  = 7
plt.tight_layout()
plt.savefig('clipped.png')

为了使 ax.pbaspect=[1., .33, 0.25] 行正常工作,按照 this post. In order to get the figure to draw larger, I added ax.dist = 7 based on this post. Lastly, based on this post 中的建议对 site-packages\mpl_toolkits\mplot3d\axes3d.py 中的 get_proj 函数进行了更改 我希望plt.tight_layout() 会回滚边距并防止下面显示的 red/yellow 表面被剪裁,但这也不起作用。我找不到将图像向上移动到 canvas 的命令,从而避免了图顶部所有不必要的白色 space 并防止 red/yellow 表面被剪掉了。是否有一行 Python 可以完成此操作?

添加行 plt.tight_layout() 后,情况变得更糟:

问题是你对site-packages\mpl_toolkits\mplot3d\axes3d.py的修改改变了投影矩阵,没有改变视图的中心,一次搞乱了场景的位置在相机坐标中转换。

因此当视图缩放(使用 ax.dist)然后移动时,情节有时会超出 canvas。

您需要将以下行替换为 axes3d.py 中的 get_proj 函数:

    # look into the middle of the new coordinates
    R = np.array([0.5, 0.5, 0.5])

作者:

    # look into the middle of the new coordinates
    try:
        R = np.array(self.pbaspect)/2
    except AttributeError:
        R = np.array([0.5, 0.5, 0.5])

这应该有效:

PS : 用于制作数字的代码 :

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from mpl_toolkits.mplot3d import Axes3D

nrow=30
ncol=100

X, Y = np.meshgrid(ncol, nrow)
Y3d, X3d = np.mgrid[0:Y, 0:X]
Z = np.sin(Y3d/Y)*np.sin(X3d/X)

fig = plt.figure()
for i in range(4):
    ax = fig.add_subplot(2,2,i,projection='3d')
    ax.auto_scale_xyz([0, 100], [0, 30], [0, 0.2])

    surf = ax.plot_surface(X3d, Y3d, Z, cmap='autumn', cstride=2, rstride=2)
    ax.set_xlabel("X-Label")
    ax.set_ylabel("Y-Label")
    ax.set_zlabel("Z-Label")
    ax.pbaspect = [1., .33, 0.25]
    ax.dist  = 7