Matplotlib subplots vs axes vs axis (singular / plural)

后端 未结 2 747
别跟我提以往
别跟我提以往 2021-02-14 16:36

Can you please clarify some Matplotlib terminology:

  • is the word \"subplots\" (or \"subplot\"?) synonymous to \"axes\"?
  • what are singulars / plurals of \"a
相关标签:
2条回答
  • 2021-02-14 16:45

    This is indeed a confusing matter.

    In English language the singular is axis and the plural is axes. Two of the kind axis form two axes.

    In matplotlib, a matplotlib.axes._axes.Axes object is often simply called "axes". This object incorporates an xaxis and a yaxis, thus the name. But speaking of that object, one would call it axes in singular. Several of those are still called axes.

    Every subplot is an Axes object, but there are Axes objects, which are no AxesSubplot object. E.g. an axes, which is created through the subplot mechanism is a matplotlib.axes._subplots.AxesSubplot. This class derives from matplotlib.axes._axes.Axes, thus this subplot is an axes. You can however also create axes via different mechanisms, e.g. by adding an axes to the figure, fig.add_axes(). This would then not be a subplot, but an axes, matplotlib.axes._axes.Axes.

    import matplotlib.pyplot as plt
    
    fig, ax = plt.subplots()
    
    print(ax)         # Axes(0.125,0.11;0.775x0.77)
    print(type(ax))   # <class 'matplotlib.axes._subplots.AxesSubplot'>
    
    ax2 = fig.add_axes([0.8,0.1,0.05,0.8])
    
    print(ax2)       # Axes(0.8,0.1;0.05x0.8)
    print(type(ax2)) # <class 'matplotlib.axes._axes.Axes'>
    

    There are also other axes, like e.g. inset axes, mpl_toolkits.axes_grid1.parasite_axes.AxesHostAxes. This object would also be called axes.

    from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes
    axins = zoomed_inset_axes(ax, 0.2, loc=3) 
    
    print(axins)       # Axes(0.125,0.11;0.775x0.77)     
    print(type(axins)) # <class 'mpl_toolkits.axes_grid1.parasite_axes.AxesHostAxes'>
    
    0 讨论(0)
  • 2021-02-14 16:59

    Axes is the plural of axis. A subplot usually has an x-axis and a y-axis, which together form the two axes of the subplot.

    Let's talk in terms of function/class names:

    Figure.add_subplot or pyplot.subplot return a AxesSubplot object. This in turn contains a XAxis and a YAxis object.

    fig = plt.figure()
    ax = fig.add_subplot(111)
    x = ax.xaxis
    
    print(type(ax))   # matplotlib.axes._subplots.AxesSubplot
    print(type(x))    # matplotlib.axis.XAxis
    

    XAxis is derived from base class Axis. AxesSubplot is derived from SubplotBase and Axes.

    0 讨论(0)
提交回复
热议问题