gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 20:47:38 29 4
gpt4 key购买 nike

代码

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

给出输出

<class 'matplotlib.axes.AxesSubplot'>

然后是代码

import matplotlib.axes
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 类.. 直到需要一个,从 SubplotBase 构建一个。这是通过 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

所以它是即时制作的,但它是 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

关于python - 有一个类matplotlib.axes.AxesSubplot,但是模块matplotlib.axes没有属性AxesSubplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11690597/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com