python的结构层次_matplotlib中的层次结构

根据this文章,matplotlib中的所有内容都是按层次结构组织的。层次结构的顶部是matplotlib“状态机环境”,它由matplotlib.pyplot模块提供。在这个层次上,简单的函数用于将绘图元素(线、图像、文本等)添加到当前图形的当前轴上。层次结构中的下一级是面向对象接口的第一级,其中pyplot仅用于图形创建等少数功能,用户显式地创建并跟踪图形和轴对象。在这个级别,用户使用pyplot创建图形,通过这些图形,可以创建一个或多个axis对象。这些轴对象随后用于大多数打印操作。

也有其他的术语,如图形,轴,轴,艺术家(有一个很好的图片,解释了所有这些,在提到的页面)。总而言之:所有内容都属于matplotlib.pyplot模块

图形-跟踪所有的子轴,少量的“特殊”艺术家(标题、人物传说等)

轴-具有数据空间的图像区域-属于图形

轴-标记记号(x、y、z坐标等)的字符串属于轴

艺术家-你在人物身上看到的一切(甚至是人物、轴和轴对象)都属于图形

创建新地物的最简单方法是使用pyplot:fig = plt.figure() # an empty figure with no axes

fig, ax_lst = plt.subplots(2, 2) # a figure with a 2x2 grid of Axes

我经常看到这两种方法可以互换使用,我希望它们基本上是等价的。但是使用fig, ax = plt.subplots()和使用fig = plt.figure()和{}无法获得相同的结果

以下是我的实验,针对plt.figure():

^{pr2}$

以下是我的实验,针对plt.subplots():In [1]: from mpl_toolkits.mplot3d import Axes3D

In [2]: import matplotlib.pyplot as plt

In [3]: fig, ax = plt.subplots()

In [4]: fig

Out[4]:

In [5]: ax

Out[5]:

In [6]:

如您所见,第一个创建matplotlib.axes._subplots.Axes3DSubplot对象,而第二个创建matplotlib.axes._subplots.AxesSubplot对象。我一直在搜索help(plt.subplots)以查找projection关键字,但没有找到任何内容。因此,我尝试对plt.subplots使用与fig.add_subplot相同的参数,但我得到以下错误:In [7]: fig, ax = plt.subplots(111, projection='3d')

---------------------------------------------------------------------------

TypeError Traceback (most recent call last)

in ()

----> 1 fig, ax = plt.subplots(111, projection='3d')

/usr/lib/python2.7/dist-packages/matplotlib/pyplot.pyc in subplots(nrows, ncols, sharex, sharey, squeeze, subplot_kw, gridspec_kw, **fig_kw)

1076 gridspec_kw = {}

1077

-> 1078 fig = figure(**fig_kw)

1079 gs = GridSpec(nrows, ncols, **gridspec_kw)

1080

/usr/lib/python2.7/dist-packages/matplotlib/pyplot.pyc in figure(num, figsize, dpi, facecolor, edgecolor, frameon, FigureClass, **kwargs)

433 frameon=frameon,

434 FigureClass=FigureClass,

--> 435 **kwargs)

436

437 if figLabel:

/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.pyc in new_figure_manager(num, *args, **kwargs)

78 """

79 FigureClass = kwargs.pop('FigureClass', Figure)

---> 80 figure = FigureClass(*args, **kwargs)

81 return new_figure_manager_given_figure(num, figure)

82

TypeError: __init__() got an unexpected keyword argument 'projection'

问题:

是fig, ax = plt.subplots()和fig = plt.figure(); ax = fig.add_subplot(111, projection='3d')的等价物,如果是,我如何在示例中使用fig, ax = plt.subplots()?在

在上述页面上还有以下代码:#!/bin/env python

import matplotlib.pyplot as plt

import numpy as np

x = np.linspace(0, 2, 100)

# The first call to plt.plot will automatically create the necessary figure and axes to achieve the desired plot.

plt.plot(x, x, label='linear')

# Subsequent calls to plt.plot re-use the current axes and each add another line.

plt.plot(x, x**2, label='quadratic')

plt.plot(x, x**3, label='cubic')

# Setting the title, legend, and axis labels also automatically use the current axes and set the title, create the legend, and label the axis respectively.

plt.xlabel('x label')

plt.ylabel('y label')

plt.title("Simple Plot")

plt.legend()

plt.show()

如您所见,没有这样的函数fig = plt.figure()也没有fig, ax_lst = plt.subplots(2, 2)

问题:

在这个例子中,层次结构是如何维护的,图形是默认创建的还是发生了什么?在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值