matplotlib使用教程(二):Axes和subplot

您所在的位置:网站首页 subplot的使用 matplotlib使用教程(二):Axes和subplot

matplotlib使用教程(二):Axes和subplot

2023-02-10 00:00| 来源: 网络整理| 查看: 265

这一系列文章原载于公众号工程师milter,如果文章对大家有帮助,恳请大家动手关注下哈~

今天继续分析matplotlib中的基本概念。

在上一篇文章中,我们获得了一个figure,并且也大致了解了一下figure的相关设置项。

现在,我们要在figure上画图了。你本来以为直接在figure上画就行了,但事实没这么简单。matplotlib允许我们将一个figure通过栅格系统划分成不同的格子,然后在格子中画图,这样就可以在一个figure中画多个图了。这里的每个格子有两个名称:Axes和subplot。subplot是从figure所有的格子来看的。因为figure要统一管理协调这些格子的位置、间隔等属性,管理协调的方法和属性设置就在subplots的层面进行。

Axes是从作为画图者的我们的角度来定义的,我们要画的点、线等都在Axes这个层面来进行。画图用的坐标系统自然也是在Axes中来设置的。

搞清楚这两个概念后,我们就来看看如何将figure划分格子,并获得我们画图使用的Axes。

通过下边的代码,我们将整个fig划分成了2x2 4个subplots。返回给我们的自然是四个axes,可以通过查看axes证实:

axes = fig.subplots(2,2)

在上图里,我们看到返回的对象是AxesSubplot,它实质上是包含了Axes的Subplot。在使用上,我们完全可以把它当做Axes使用。

如果我们只想在figure上画一幅图,就有两种方法:

axes = fig.subplots(1,1) or axes = fig.subplots()

此时得到的axes是就是一个AxesSubplot对象。

如果大家观察仔细,会看到里面有3个值,它们确定了subplot在figure中的位置。可以通过下图感受到:

fig = plt.figure() fig.set_facecolor("green") axis = fig.subplots() plt.show()

前两个值实际上是坐标原点相对于figure左下角的位置。第三个值是subplot的宽和高。

figure中还有一个方法:add_subplot。其目的也是将figure划分成栅格,并获取其中某一个。使用方法如下所示:

fig = plt.figure() ax1 = fig.add_subplot(2, 3, 1) fig.add_subplot(232, facecolor="blue") fig.add_subplot(233, facecolor="yellow") fig.add_subplot(234, sharex=ax1) fig.add_subplot(235, facecolor="red") fig.add_subplot(236, facecolor="green") plt.show()

这里有两个地方需要注意一下。add_subplot(232)和add_subplot(2,3,2)等价的。

另外,如果将最后一个236改成436,你猜会发生什么呢?

答案是如下所示:

可以看到436 相当于将figure重新划分了网格,并将第6个网格设置成绿色。

两种不同的网格划分产生了重叠。这再次体现了matplotlib的灵活性。

最佳的实践是:在开始时就将figure的网格划分好,并不再改变。

Axes 概览

最后,我们还是通览一下Axes的属性:

{'figure': , '_subplotspec': , 'figbox': Bbox([[0.125, 0.125], [0.9, 0.88]]), 'rowNum': 0, 'colNum': 0, 'numRows': 1, 'numCols': 1, '_stale': True, 'stale_callback': , '_axes': , '_transform': None, '_transformSet': False, '_visible': True, '_animated': False, '_alpha': None, 'clipbox': None, '_clippath': None, '_clipon': True, '_label': '', '_picker': None, '_contains': None, '_rasterized': None, '_agg_filter': None, '_mouseover': False, 'eventson': False, '_oid': 0, '_propobservers': {}, '_remove_method': , '_url': None, '_gid': None, '_snap': None, '_sketch': None, '_path_effects': [], '_sticky_edges': _XYPair(x=[], y=[]), '_in_layout': True, '_position': Bbox([[0.125, 0.125], [0.9, 0.88]]), '_originalPosition': Bbox([[0.125, 0.125], [0.9, 0.88]]), '_aspect': 'auto', '_adjustable': 'box', '_anchor': 'C', '_sharex': None, '_sharey': None, 'bbox': , 'dataLim': Bbox([[inf, inf], [-inf, -inf]]), 'viewLim': Bbox([[0.0, 0.0], [1.0, 1.0]]), 'transScale': , 'transAxes': , 'transLimits': , 'transData': , '_xaxis_transform': , '_yaxis_transform': , '_axes_locator': None, 'spines': OrderedDict([('left', ), ('right', ), ('bottom', ), ('top', )]), 'xaxis': , 'yaxis': , '_facecolor': 'white', '_frameon': True, '_axisbelow': 'line', '_rasterization_zorder': None, '_connected': {}, 'ignore_existing_data_limits': True, 'callbacks': , '_autoscaleXon': True, '_autoscaleYon': True, '_xmargin': 0.05, '_ymargin': 0.05, '_tight': None, '_use_sticky_edges': True, '_get_lines': , '_get_patches_for_fill': , '_gridOn': False, 'lines': [], 'patches': [], 'texts': [], 'tables': [], 'artists': [], 'images': [], '_mouseover_set': , 'child_axes': [], '_current_image': None, 'legend_': None, 'collections': [], 'containers': [], 'title': Text(0.5, 1, ''), '_left_title': Text(0.0, 1, ''), '_right_title': Text(1.0, 1, ''), 'titleOffsetTrans': , '_autotitlepos': True, 'patch': , 'axison': True, 'fmt_xdata': None, 'fmt_ydata': None, '_navigate': True, '_navigate_mode': None, '_xcid': 0, '_ycid': 0, '_layoutbox': None, '_poslayoutbox': None}

可以看到,里面有Axes在网格系统中的坐标,所属的figure,还有title,_facecolor等属性。yaxis、xaxis代表坐标轴。

此时,建议大家对比一下Axes和上一讲中的Figure的属性。就可以大致有个感觉,什么样的设置要在哪里去设。



【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3