简体   繁体   English

有一个类 matplotlib.axes.AxesSubplot,但是模块 matplotlib.axes 没有属性 AxesSubplot

[英]There is a class matplotlib.axes.AxesSubplot, but the module matplotlib.axes has no attribute AxesSubplot

The code编码

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
print type(ax)

gives the output给出输出

<class 'matplotlib.axes.AxesSubplot'>

Then the code然后代码

import matplotlib.axes
matplotlib.axes.AxesSubplot

raises the exception引发异常

AttributeError: 'module' object has no attribute 'AxesSubplot'

To summarize, there is a class matplotlib.axes.AxesSubplot , but the module matplotlib.axes has no attribute AxesSubplot .总而言之,有一个类matplotlib.axes.AxesSubplot ,但模块matplotlib.axes没有属性AxesSubplot What on earth is going on?这到底是怎么回事?

I'm using Matplotlib 1.1.0 and Python 2.7.3.我使用的是 Matplotlib 1.1.0 和 Python 2.7.3。

Heh.呵呵。 That's because there is no AxesSubplot class.. until one is needed, when one is built from SubplotBase .这是因为没有AxesSubplot类..直到一个是需要的,当一个人从建SubplotBase This is done by some magic in axes.py :这是通过axes.py的一些魔法完成的:

def subplot_class_factory(axes_class=None):
    # This makes a new class that inherits from SubplotBase and the
    # given axes_class (which is assumed to be a subclass of Axes).
    # This is perhaps a little bit roundabout to make a new class on
    # the fly like this, but it means that a new Subplot class does
    # not have to be created for every type of Axes.
    if axes_class is None:
        axes_class = Axes

    new_class = _subplot_classes.get(axes_class)
    if new_class is None:
        new_class = new.classobj("%sSubplot" % (axes_class.__name__),
                                 (SubplotBase, axes_class),
                                 {'_axes_class': axes_class})
        _subplot_classes[axes_class] = new_class

    return new_class

So it's made on the fly, but it's a subclass of SubplotBase :所以它是即时制作的,但它是SubplotBase的子类:

>>> import matplotlib.pyplot as plt
>>> fig = plt.figure()
>>> ax = fig.add_subplot(111)
>>> print type(ax)
<class 'matplotlib.axes.AxesSubplot'>
>>> b = type(ax)
>>> import matplotlib.axes
>>> issubclass(b, matplotlib.axes.SubplotBase)
True

Another way to see what DSM said:了解 DSM 内容的另一种方式:

In [1]: from matplotlib import pyplot as plt                                                                                                                                                                       

In [2]: type(plt.gca()).__mro__                                                                                                                                                                                    
Out[2]: 
(matplotlib.axes._subplots.AxesSubplot,
 matplotlib.axes._subplots.SubplotBase,
 matplotlib.axes._axes.Axes,
 matplotlib.axes._base._AxesBase,
 matplotlib.artist.Artist,
 object)

with the dunder method resolution order you can find all the inheritances of some class.使用 dunder方法解析顺序,您可以找到某个类的所有继承。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM