python check if figure is 2d or 3d

不问归期 提交于 2019-11-29 16:43:35
ImportanceOfBeingErnest

You can use the name of the axes.

plt.gca().name   or   ax.name

if ax is the axes.

A 3D axes' name will be "3d". A 2D axes' name will be "rectilinear", "polar" or some other name depending on the type of plot.

You can therefore check

if  ax.name == "3d":
    # axes is 3D, do something
else:
    # axes is not 3D, do something else


You can also check for the limits, as proposed in an answer to the question this is a duplicate of. In this way you would get the limits
def get_limits(ax):
    xlim = ax.get_xlim()
    ylim = ax.get_ylim()
    if hasattr(ax, 'get_zlim'): 
        zlim = ax.get_zlim()
        return xlim, ylim, zlim
    else:
        return xlim, ylim
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!