Understanding matplotlib.subplots python

I built a set of pie charts with some help. Insert an image into a slice of a pie chart. My charts look great, now I need to put all 6 of them in a 2x3 shape with common marks on the common x and y axis. To start, I look at the subtitles and thought that I could make it work. I downloaded a few examples and started trying 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)) 

gives:

class 'matplotlib.figure.Figure'

class 'matplotlib.axes._subplots.AxesSubplot'

a

  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)) 

returns:

class 'matplotlib.figure.Figure'

class 'numpy.ndarray'

When I do this:

 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)) 

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

  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: not enough values ​​to unpack (expected 2, received 1)

but again:

  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)) 

gives class 'matplotlib.figure.Figure'

class 'matplotlib.axes._subplots.AxesSubplot'

class 'matplotlib.axes._subplots.AxesSubplot'

Why is it either an array or an axis, and also why is there no work of 2X1 and 1X2? I want from heaven, I could better understand the documentation. Thanks.

+5
source share
1 answer

The various types of data returned are associated with the squeeze keyword plt.subplots() argument, which is set to True by default. Let me improve the documentation with the corresponding unpacks:

compress: bool, optional, default: True

  • If True, additional dimensions are extruded from the returned Axes object:

    • if only one subplot is built (nrows = ncols = 1), the resulting single Axes object is returned as a scalar.
      fig, ax = plt.subplots()
    • for Nx1 or 1xN subnets, the returned object is an array of 1D numpy objects of Axes objects that 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, subsets with N> 1 and M> 1 are returned as 2D arrays.
      fig, ((ax11, .., ax1M),..,(axN1, .., axNM)) = plt.subplots(nrows=N, ncols=M)
  • If False, no compression is performed at all: the returned Axes object is always a 2D array containing Axes instances, even if it ends with 1x1.
    fig, ((ax,),) = plt.subplots(nrows=1, ncols=1, squeeze=False)
    fig, ((ax,), .. ,(axN,)) = plt.subplots(nrows=N, ncols=1, squeeze=False) for Nx1
    fig, ((ax, .. ,axN),) = plt.subplots(nrows=1, ncols=N, squeeze=False) for 1xN
    fig, ((ax11, .., ax1M),..,(axN1, .., axNM)) = plt.subplots(nrows=N, ncols=M)

Alternatively, you can always use the unpacked version.

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

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

So, for a 2 x 3 grid, it really doesn't matter if you set squeeze to False . The result will always be a two-dimensional array. You can unpack it as

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

have ax{i} as matplotlib axis objects, or you can use the packaged 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 
+4
source

Source: https://habr.com/ru/post/1268921/


All Articles