如何确定 matplotlib 轴对象的投影(2D 或 3D)? [英] How to determine the projection (2D or 3D) of a matplotlib axes object?
问题描述
在 Python 的 matplotlib 库中,很容易指定坐标轴对象在创建时的投影:
In Python's matplotlib library it is easy to specify the projection of an axes object on creation:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
ax = plt.axes(projection='3d')
但是如何确定现有坐标区对象的投影?没有 ax.get_projection
,ax.properties
不包含投影"键,并且快速的谷歌搜索没有找到任何有用的东西.
But how do I determine the projection of an existing axes object? There's no ax.get_projection
, ax.properties
contains no "projection" key, and a quick google search hasn't turned up anything useful.
推荐答案
我不认为有自动化的方式,但显然有一些属性只有 3D 投影才有(例如 zlim
).
I don't think there is an automated way, but there are obviously some properties that only the 3D projection has (e.g. zlim
).
所以你可以写一个小辅助函数来测试它是否是 3D 的:
So you could write a little helper function to test if its 3D or not:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
def axesDimensions(ax):
if hasattr(ax, 'get_zlim'):
return 3
else:
return 2
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212, projection='3d')
print "ax1: ", axesDimensions(ax1)
print "ax2: ", axesDimensions(ax2)
打印:
ax1: 2
ax2: 3
这篇关于如何确定 matplotlib 轴对象的投影(2D 或 3D)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!