Changeset 4415 for trunk/GSASIIplot.py
 Timestamp:
 May 7, 2020 12:17:26 PM (3 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/GSASIIplot.py
r4408 r4415 290 290 fil.write(line+'\n') 291 291 292 def MPLsubplots(figure, nrows=1, ncols=1, sharex=False, sharey=False,293 squeeze=True, subplot_kw=None, gridspec_kw=None):294 """295 Add a set of subplots to this figure.296 297 This is a copy of Figure.figure.subplots from matplotlib.figure.py.298 Included here because this appears only in later versions of299 matplotlib. When v2.2 is standard, this should be removed from GSASII.300 301 :param int nrows, ncols: default: 1302 Number of rows/cols of the subplot grid.303 :param bool/str sharex, sharey: True/False or {'none', 'all', 'row', 'col'}, default: False304 305 Controls sharing of properties among x (`sharex`) or y (`sharey`)306 axes:292 # def MPLsubplots(figure, nrows=1, ncols=1, sharex=False, sharey=False, 293 # squeeze=True, subplot_kw=None, gridspec_kw=None): 294 # """ 295 # Add a set of subplots to this figure. 296 297 # This is a copy of Figure.figure.subplots from matplotlib.figure.py. 298 # Included here because this appears only in later versions of 299 # matplotlib. When v2.2 is standard, this should be removed from GSASII. 300 301 # :param int nrows, ncols: default: 1 302 # Number of rows/cols of the subplot grid. 303 # :param bool/str sharex, sharey: True/False or {'none', 'all', 'row', 'col'}, default: False 304 305 # Controls sharing of properties among x (`sharex`) or y (`sharey`) 306 # axes: 307 307 308  True or 'all': x or yaxis will be shared among all309 subplots.310  False or 'none': each subplot x or yaxis will be311 independent.312  'row': each subplot row will share an x or yaxis.313  'col': each subplot column will share an x or yaxis.308 #  True or 'all': x or yaxis will be shared among all 309 # subplots. 310 #  False or 'none': each subplot x or yaxis will be 311 # independent. 312 #  'row': each subplot row will share an x or yaxis. 313 #  'col': each subplot column will share an x or yaxis. 314 314 315 When subplots have a shared xaxis along a column, only the x tick316 labels of the bottom subplot are visible. Similarly, when317 subplots have a shared yaxis along a row, only the y tick labels318 of the first column subplot are visible.319 :param bool squeeze: default: True320 321  If True, extra dimensions are squeezed out from the returned322 axis object:315 # When subplots have a shared xaxis along a column, only the x tick 316 # labels of the bottom subplot are visible. Similarly, when 317 # subplots have a shared yaxis along a row, only the y tick labels 318 # of the first column subplot are visible. 319 # :param bool squeeze: default: True 320 321 #  If True, extra dimensions are squeezed out from the returned 322 # axis object: 323 323 324  if only one subplot is constructed (nrows=ncols=1), the325 resulting single Axes object is returned as a scalar.326  for Nx1 or 1xN subplots, the returned object is a 1D numpy327 object array of Axes objects are returned as numpy 1D328 arrays.329  for NxM, subplots with N>1 and M>1 are returned as a 2D330 arrays.324 #  if only one subplot is constructed (nrows=ncols=1), the 325 # resulting single Axes object is returned as a scalar. 326 #  for Nx1 or 1xN subplots, the returned object is a 1D numpy 327 # object array of Axes objects are returned as numpy 1D 328 # arrays. 329 #  for NxM, subplots with N>1 and M>1 are returned as a 2D 330 # arrays. 331 331 332  If False, no squeezing at all is done: the returned Axes object333 is always a 2D array containing Axes instances, even if it ends334 up being 1x1.332 #  If False, no squeezing at all is done: the returned Axes object 333 # is always a 2D array containing Axes instances, even if it ends 334 # up being 1x1. 335 335 336 :param dict subplot_kw: default: {}337 Dict with keywords passed to the338 :meth:`matplotlib.figure.Figure.add_subplot` call used to create339 each subplots.340 :param dict gridspec_kw: default: {}341 Dict with keywords passed to the342 :class:`matplotlib.gridspec.GridSpec` constructor used to create343 the grid the subplots are placed on.336 # :param dict subplot_kw: default: {} 337 # Dict with keywords passed to the 338 # :meth:`matplotlib.figure.Figure.add_subplot` call used to create 339 # each subplots. 340 # :param dict gridspec_kw: default: {} 341 # Dict with keywords passed to the 342 # :class:`matplotlib.gridspec.GridSpec` constructor used to create 343 # the grid the subplots are placed on. 344 344 345 :returns: A single Axes object or array of Axes objects with346 the added axes. The dimensions of the resulting array can be347 controlled with the squeeze keyword, see above.345 # :returns: A single Axes object or array of Axes objects with 346 # the added axes. The dimensions of the resulting array can be 347 # controlled with the squeeze keyword, see above. 348 348 349 See also: pyplot.subplots: pyplot API; docstring includes examples.350 351 """352 353 # for backwards compatibility354 if isinstance(sharex, bool):355 sharex = "all" if sharex else "none"356 if isinstance(sharey, bool):357 sharey = "all" if sharey else "none"358 share_values = ["all", "row", "col", "none"]359 if sharex not in share_values:360 # This check was added because it is very easy to type361 # `subplots(1, 2, 1)` when `subplot(1, 2, 1)` was intended.362 # In most cases, no error will ever occur, but mysterious behavior363 # will result because what was intended to be the subplot index is364 # instead treated as a bool for sharex.365 if isinstance(sharex, int):366 warnings.warn(367 "sharex argument to subplots() was an integer. "368 "Did you intend to use subplot() (without 's')?")369 370 raise ValueError("sharex [%s] must be one of %s" %371 (sharex, share_values))372 if sharey not in share_values:373 raise ValueError("sharey [%s] must be one of %s" %374 (sharey, share_values))375 if subplot_kw is None:376 subplot_kw = {}377 if gridspec_kw is None:378 gridspec_kw = {}379 380 # if figure.get_constrained_layout():381 # gs = GridSpec(nrows, ncols, figure=figure, **gridspec_kw)382 # else:383 # # this should turn constrained_layout off if we don't want it384 # gs = GridSpec(nrows, ncols, figure=None, **gridspec_kw)385 gs = mpl.gridspec.GridSpec(nrows, ncols, **gridspec_kw)386 387 # Create array to hold all axes.388 axarr = np.empty((nrows, ncols), dtype=object)389 for row in range(nrows):390 for col in range(ncols):391 shared_with = {"none": None, "all": axarr[0, 0],392 "row": axarr[row, 0], "col": axarr[0, col]}393 subplot_kw["sharex"] = shared_with[sharex]394 subplot_kw["sharey"] = shared_with[sharey]395 axarr[row, col] = figure.add_subplot(gs[row, col], **subplot_kw)396 397 # turn off redundant tick labeling398 if sharex in ["col", "all"]:399 # turn off all but the bottom row400 for ax in axarr[:1, :].flat:401 ax.xaxis.set_tick_params(which='both',402 labelbottom=False, labeltop=False)403 ax.xaxis.offsetText.set_visible(False)404 if sharey in ["row", "all"]:405 # turn off all but the first column406 for ax in axarr[:, 1:].flat:407 ax.yaxis.set_tick_params(which='both',408 labelleft=False, labelright=False)409 ax.yaxis.offsetText.set_visible(False)410 411 if squeeze:412 # Discarding unneeded dimensions that equal 1. If we only have one413 # subplot, just return it instead of a 1element array.414 return axarr.item() if axarr.size == 1 else axarr.squeeze()415 else:416 # Returned axis array will be always 2d, even if nrows=ncols=1.417 return axarr349 # See also: pyplot.subplots: pyplot API; docstring includes examples. 350 351 # """ 352 353 # # for backwards compatibility 354 # if isinstance(sharex, bool): 355 # sharex = "all" if sharex else "none" 356 # if isinstance(sharey, bool): 357 # sharey = "all" if sharey else "none" 358 # share_values = ["all", "row", "col", "none"] 359 # if sharex not in share_values: 360 # # This check was added because it is very easy to type 361 # # `subplots(1, 2, 1)` when `subplot(1, 2, 1)` was intended. 362 # # In most cases, no error will ever occur, but mysterious behavior 363 # # will result because what was intended to be the subplot index is 364 # # instead treated as a bool for sharex. 365 # if isinstance(sharex, int): 366 # warnings.warn( 367 # "sharex argument to subplots() was an integer. " 368 # "Did you intend to use subplot() (without 's')?") 369 370 # raise ValueError("sharex [%s] must be one of %s" % 371 # (sharex, share_values)) 372 # if sharey not in share_values: 373 # raise ValueError("sharey [%s] must be one of %s" % 374 # (sharey, share_values)) 375 # if subplot_kw is None: 376 # subplot_kw = {} 377 # if gridspec_kw is None: 378 # gridspec_kw = {} 379 380 # # if figure.get_constrained_layout(): 381 # # gs = GridSpec(nrows, ncols, figure=figure, **gridspec_kw) 382 # # else: 383 # # # this should turn constrained_layout off if we don't want it 384 # # gs = GridSpec(nrows, ncols, figure=None, **gridspec_kw) 385 # gs = mpl.gridspec.GridSpec(nrows, ncols, **gridspec_kw) 386 387 # # Create array to hold all axes. 388 # axarr = np.empty((nrows, ncols), dtype=object) 389 # for row in range(nrows): 390 # for col in range(ncols): 391 # shared_with = {"none": None, "all": axarr[0, 0], 392 # "row": axarr[row, 0], "col": axarr[0, col]} 393 # subplot_kw["sharex"] = shared_with[sharex] 394 # subplot_kw["sharey"] = shared_with[sharey] 395 # axarr[row, col] = figure.add_subplot(gs[row, col], **subplot_kw) 396 397 # # turn off redundant tick labeling 398 # if sharex in ["col", "all"]: 399 # # turn off all but the bottom row 400 # for ax in axarr[:1, :].flat: 401 # ax.xaxis.set_tick_params(which='both', 402 # labelbottom=False, labeltop=False) 403 # ax.xaxis.offsetText.set_visible(False) 404 # if sharey in ["row", "all"]: 405 # # turn off all but the first column 406 # for ax in axarr[:, 1:].flat: 407 # ax.yaxis.set_tick_params(which='both', 408 # labelleft=False, labelright=False) 409 # ax.yaxis.offsetText.set_visible(False) 410 411 # if squeeze: 412 # # Discarding unneeded dimensions that equal 1. If we only have one 413 # # subplot, just return it instead of a 1element array. 414 # return axarr.item() if axarr.size == 1 else axarr.squeeze() 415 # else: 416 # # Returned axis array will be always 2d, even if nrows=ncols=1. 417 # return axarr 418 418 419 419 class _tabPlotWin(wx.Panel): … … 2999 2999 Plot.set_visible(False) #hide old plot frame, will get replaced below 3000 3000 GS_kw = {'height_ratios':[4, 1],} 3001 try:3002 3003 except AttributeError: # figure.Figure.subplots added in MPL 2.23004 Plot,Plot1 = MPLsubplots(Page.figure, 2, 1, sharex=True, gridspec_kw=GS_kw)3001 # try: 3002 Plot,Plot1 = Page.figure.subplots(2,1,sharex=True,gridspec_kw=GS_kw) 3003 # except AttributeError: # figure.Figure.subplots added in MPL 2.2 3004 # Plot,Plot1 = MPLsubplots(Page.figure, 2, 1, sharex=True, gridspec_kw=GS_kw) 3005 3005 Plot1.set_ylabel(r'$\mathsf{\Delta(I)/\sigma(I)}$',fontsize=16) 3006 3006 Plot1.set_xlabel(xLabel,fontsize=16) … … 7642 7642 Plot.set_visible(False) 7643 7643 GS_kw = {'width_ratios':[1,2],} 7644 try:7645 7646 except AttributeError: # figure.Figure.subplots added in MPL 2.27647 Plot1,Plot = MPLsubplots(Page.figure, 1,2, gridspec_kw=GS_kw)7644 # try: 7645 Plot1,Plot = Page.figure.subplots(1,2,gridspec_kw=GS_kw) 7646 # except AttributeError: # figure.Figure.subplots added in MPL 2.2 7647 # Plot1,Plot = MPLsubplots(Page.figure, 1,2, gridspec_kw=GS_kw) 7648 7648 Plot1.set_title('Line scan at azm= %6.1f'%Data['linescan'][1]) 7649 7649 Plot1.set_xlabel(r'$\mathsf{2\Theta}$',fontsize=12)
Note: See TracChangeset
for help on using the changeset viewer.