如何确定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
不包含 projection键,并且没有Google的快速搜索出现了任何有用的东西。
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屋!