jmd*_*_dk 5 python matplotlib scatter-plot python-3.x
我有一些代码使用 matplotlibscatter
和 结合生成 3D 散点图tight_layout
,请参阅下面的简化代码:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import proj3d
fig = plt.figure()
ax = fig.gca(projection='3d')
N = 100
x = np.random.random(N)
y = np.random.random(N)
z = np.random.random(N)
ax.scatter(x, y, z)
plt.tight_layout() # <-- Without this, everything is fine
plt.savefig('scatter.png')
Run Code Online (Sandbox Code Playgroud)
旧版本会生成类似的输出,至少可以追溯到 1.5.1。使用新版本 3.0.0 时,出现问题,plt.tight_layout()
我得到以下输出:
伴随着这是警告
.../matplotlib/tight_layout.py:177: UserWarning: 左右边距不能足够大以容纳所有轴装饰
有人可能会争辩说,tight_layout
像这里那样不带参数使用并不会(在较旧的 matplotlib 上)始终导致预期的边距收紧,因此首先应该避免使用tight_layout
3D 绘图。但是,通过手动调整tight_layout
它的参数(曾经是)一种即使在 3D 绘图上也可以修剪边距的体面方法。
我的猜测是这是 matplotlib 中的一个错误,但也许他们做了一些我没有注意到的故意更改。任何有关修复的指针表示赞赏。
感谢 ImportanceOfBeingErnest 的评论,它现在可以工作了:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import proj3d
fig = plt.figure()
ax = fig.gca(projection='3d')
N = 100
x = np.random.random(N)
y = np.random.random(N)
z = np.random.random(N)
ax.scatter(x, y, z)
# The fix
for spine in ax.spines.values():
spine.set_visible(False)
plt.tight_layout()
plt.savefig('scatter.png')
Run Code Online (Sandbox Code Playgroud)
从评论中的链接来看,这似乎将在 matplotlib 3.0.x 中修复。目前,可以使用上面的方法。
归档时间: |
|
查看次数: |
4833 次 |
最近记录: |