Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
826 views
in Technique[技术] by (71.8m points)

matplotlib - python check if figure is 2d or 3d

In matlab with a figure, to check if it is 3D figure or 2D figure I use:

V=axis;

and check the number of components of V (4 for 2d figure, 6 for 3d figure). How can i implement this with python and matplotlib?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...