理解 matplotlib.subplots python [英] understanding matplotlib.subplots python

查看:34
本文介绍了理解 matplotlib.subplots python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我借助一些帮助构建了一组饼图 将图像插入饼图切片我的图表看起来很棒,现在我需要将所有 6 个图表放在一个 2x3 的图形中,在共享的 x 和 y 轴上有公共刻度线.首先,我正在查看子图,并认为我可以让它发挥作用.我下载了一些示例并开始尝试一些东西.

I have constructed a set of pie charts with some help Insert image into pie chart slice My charts look wonderful, now I need to place all 6 of them in a 2x3 figure with common tick marks on the shared x and y axis. For starting I am looking at subplots and thought I could get it to work. I downloaded some examples and started to try a few things.

    f, (a) = (plt.subplots(nrows=1, ncols=1, sharex=True, sharey=True))#,
                 #squeeze=False, subplot_kw=None, gridspec_kw=None))
    print(type(f),'
',type(a),'
')#,type(b))

产量:

类'matplotlib.figure.Figure'

class 'matplotlib.figure.Figure'

类'matplotlib.axes._subplots.AxesSubplot'

class 'matplotlib.axes._subplots.AxesSubplot'

同时:

    f, (a) = (plt.subplots(nrows=1, ncols=1, sharex=True, sharey=True, squeeze=False, subplot_kw=None, gridspec_kw=None))
    print(type(f),'
',type(a),'
')#,type(b))

返回:

类'matplotlib.figure.Figure'

class 'matplotlib.figure.Figure'

类'numpy.ndarray'

class 'numpy.ndarray'

当我这样做时:

f, (a,b) = (plt.subplots(nrows=2, ncols=1, sharex=True, sharey=True, squeeze=False, subplot_kw=None, gridspec_kw=None))
    print(type(f),'
',type(a),'
',type(b))

我得到了类似的结果,但是如果 nrows=1 和 ncols=2 我得到一个错误:

I get the similar results, however if nrows=1 and ncols=2 I get an error:

    f, (a,b) = (plt.subplots(nrows=1, ncols=2, sharex=True, sharey=True, squeeze=False, subplot_kw=None, gridspec_kw=None))
    print(type(f),'
',type(a),'
',type(b))

ValueError: 没有足够的值来解包(预期为 2,得到 1)

ValueError: not enough values to unpack (expected 2, got 1)

但又是这个:

    f, (a , b) = (
    plt.subplots(nrows=1, ncols=2, sharex=True, sharey=True))#,
                 #squeeze=False, subplot_kw=None, gridspec_kw=None))
    print(type(f),'
',type(a),'
',type(b))

给类'matplotlib.figure.Figure'

gives class 'matplotlib.figure.Figure'

类'matplotlib.axes._subplots.AxesSubplot'

class 'matplotlib.axes._subplots.AxesSubplot'

类'matplotlib.axes._subplots.AxesSubplot'

class 'matplotlib.axes._subplots.AxesSubplot'

为什么是数组或轴,以及为什么 2X1 有效而 1X2 无效?我希望高天我能更好地理解文档.谢谢.

Why is it either or array or axes, and also why does a 2X1 work and a 1X2 does not? I wish to high heaven I could better understand the documentation. Thanks.

推荐答案

不同的返回类型是由于 plt.subplots() 默认设置为 True.让我们通过相应的解包来增强文档:

The different return types are due to the squeeze keyword argument to plt.subplots() which is set to True by default. Let's enhance the documentation with the respective unpackings:

squeeze : bool,可选,默认值:True

squeeze : bool, optional, default: True

  • 如果为 True,则从返回的 Axes 对象中挤出额外的维度:

  • If True, extra dimensions are squeezed out from the returned Axes object:

  • 如果只构建了一个子图 (nrows=ncols=1),则生成的单个 Axes 对象将作为标量返回.
    fig, ax = plt.subplots()
  • 对于 Nx1 或 1xN 子图,返回的对象是一个 1D numpy 对象数组,Axes 对象作为 numpy 1D 数组返回.
    fig, (ax1, ..., axN) = plt.subplots(nrows=N, ncols=1)(对于 Nx1)
    fig, (ax1, ..., axN) = plt.subplots(nrows=1, ncols=N)(对于 1xN)
  • 对于 NxM,N>1 和 M>1 的子图作为二维数组返回.
    fig, ((ax11, .., ax1M),..,(axN1, .., axNM)) = plt.subplots(nrows=N, ncols=M)
  • if only one subplot is constructed (nrows=ncols=1), the resulting single Axes object is returned as a scalar.
    fig, ax = plt.subplots()
  • for Nx1 or 1xN subplots, the returned object is a 1D numpy object array of Axes objects are returned as numpy 1D arrays.
    fig, (ax1, ..., axN) = plt.subplots(nrows=N, ncols=1) (for Nx1)
    fig, (ax1, ..., axN) = plt.subplots(nrows=1, ncols=N) (for 1xN)
  • for NxM, subplots with N>1 and M>1 are returned as a 2D arrays.
    fig, ((ax11, .., ax1M),..,(axN1, .., axNM)) = plt.subplots(nrows=N, ncols=M)

或者,您可以始终使用未打包的版本

Alternatively you may always use the unpacked version

fig, ax_arr = plt.subplots(nrows=N, ncols=M, squeeze=False)

并索引数组以获得轴,ax_arr[1,2].plot(..).

and index the array to obtain the axes, ax_arr[1,2].plot(..).

因此,对于 2 x 3 网格,将 squeeze 设置为 False 实际上并不重要.结果将始终是一个二维数组.您可以将其解压缩为

So for a 2 x 3 grid it wouldn't actually matter if you set squeeze to False. The result will always be a 2D array. You may unpack it as

fig, ((ax1, ax2, ax3),(ax4, ax5, ax6)) = plt.subplots(nrows=2, ncols=3)

ax{i} 作为 matplotlib 轴对象,或者您可以使用打包版本

to have ax{i} as the matplotlib axes objects, or you may use the packed version

fig, ax_arr = plt.subplots(nrows=2, ncols=3)
ax_arr[0,0].plot(..) # plot to first top left axes
ax_arr[1,2].plot(..) # plot to last bottom right axes

这篇关于理解 matplotlib.subplots python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆