Python 存在matplotlib.axes.AxesSubplot类,但模块matplotlib.axes没有属性AxesSubplot

Python 存在matplotlib.axes.AxesSubplot类,但模块matplotlib.axes没有属性AxesSubplot,python,matplotlib,Python,Matplotlib,代码 import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111) print type(ax) 给出输出 <class 'matplotlib.axes.AxesSubplot'> 引发了异常 AttributeError: 'module' object has no attribute 'AxesSubplot' 总之,有一个类matplotlib.axes.AxesSubplot

代码

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

<class 'matplotlib.axes.AxesSubplot'>
引发了异常

AttributeError: 'module' object has no attribute 'AxesSubplot'
总之,有一个类
matplotlib.axes.AxesSubplot
,但是模块
matplotlib.axes
没有属性
AxesSubplot
。到底发生了什么事


我正在使用Matplotlib 1.1.0和Python 2.7.3。

嘿。这是因为没有
AxesSubplot
class。。直到需要一个时,从
子lotbase
构建一个。这是通过
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
所以它是动态生成的,但它是
子lotbase
的一个子类:

>>> 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
>>将matplotlib.pyplot作为plt导入
>>>图=plt.图()
>>>ax=图添加_子批次(111)
>>>打印类型(ax)
>>>b=类型(ax)
>>>导入matplotlib.axes
>>>issubclass(b,matplotlib.axes.SubplotBase)
真的

另一种了解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)

有了dunder,你可以找到某个类的所有继承项。

你想用这个来解决一个实际问题,还是这个问题只是好奇?@Julian:这就是“好奇”。我相信好奇会让你成为一个更好的开发人员。那么,当我运行第一个代码段时,该类不应该被创建,而当我运行第二个代码段时,该类不应该作为matplotlib.axes的属性出现吗?它是创建的,但不是存储在模块级。查看
matplotlib.axes.\u子绘图\u类
:您应该看到
{matplotlib.axes.axes:matplotlib.axes.AxesSubplot}
。注意,在工厂函数中,
新类
被添加到
\u子批
字典中。
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)