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

查看:97
本文介绍了了解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),'\n',type(a),'\n')#,type(b))

产量:

class'matplotlib.figure.Figure'

class 'matplotlib.figure.Figure'

class'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),'\n',type(a),'\n')#,type(b))

返回:

class'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),'\n',type(a),'\n',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),'\n',type(a),'\n',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),'\n',type(a),'\n',type(b))

给予 类"matplotlib.figure.Figure"

gives class 'matplotlib.figure.Figure'

class'matplotlib.axes._subplots.AxesSubplot'

class 'matplotlib.axes._subplots.AxesSubplot'

class'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.

推荐答案

不同的返回类型是由于

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:布尔值,可选,默认值: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的子图作为2D数组返回.
    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(..).

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

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天全站免登陆