@@ -270,6 +270,10 @@ def line(darray, *args, **kwargs):
270
270
Coordinates for x, y axis. Only one of these may be specified.
271
271
The other coordinate plots values from the DataArray on which this
272
272
plot method is called.
273
+ xscale, yscale : 'linear', 'symlog', 'log', 'logit', optional
274
+ Specifies scaling for the x- and y-axes respectively
275
+ xticks, yticks : Specify tick locations for x- and y-axes
276
+ xlim, ylim : Specify x- and y-axes limits
273
277
xincrease : None, True, or False, optional
274
278
Should the values on the x axes be increasing from left to right?
275
279
if None, use the default for the matplotlib function.
@@ -305,8 +309,14 @@ def line(darray, *args, **kwargs):
305
309
hue = kwargs .pop ('hue' , None )
306
310
x = kwargs .pop ('x' , None )
307
311
y = kwargs .pop ('y' , None )
308
- xincrease = kwargs .pop ('xincrease' , True )
309
- yincrease = kwargs .pop ('yincrease' , True )
312
+ xincrease = kwargs .pop ('xincrease' , None ) # default needs to be None
313
+ yincrease = kwargs .pop ('yincrease' , None )
314
+ xscale = kwargs .pop ('xscale' , None ) # default needs to be None
315
+ yscale = kwargs .pop ('yscale' , None )
316
+ xticks = kwargs .pop ('xticks' , None )
317
+ yticks = kwargs .pop ('yticks' , None )
318
+ xlim = kwargs .pop ('xlim' , None )
319
+ ylim = kwargs .pop ('ylim' , None )
310
320
add_legend = kwargs .pop ('add_legend' , True )
311
321
_labels = kwargs .pop ('_labels' , True )
312
322
if args is ():
@@ -343,7 +353,8 @@ def line(darray, *args, **kwargs):
343
353
xlabels .set_rotation (30 )
344
354
xlabels .set_ha ('right' )
345
355
346
- _update_axes_limits (ax , xincrease , yincrease )
356
+ _update_axes (ax , xincrease , yincrease , xscale , yscale ,
357
+ xticks , yticks , xlim , ylim )
347
358
348
359
return primitive
349
360
@@ -378,37 +389,69 @@ def hist(darray, figsize=None, size=None, aspect=None, ax=None, **kwargs):
378
389
"""
379
390
ax = get_axis (figsize , size , aspect , ax )
380
391
392
+ xincrease = kwargs .pop ('xincrease' , None ) # default needs to be None
393
+ yincrease = kwargs .pop ('yincrease' , None )
394
+ xscale = kwargs .pop ('xscale' , None ) # default needs to be None
395
+ yscale = kwargs .pop ('yscale' , None )
396
+ xticks = kwargs .pop ('xticks' , None )
397
+ yticks = kwargs .pop ('yticks' , None )
398
+ xlim = kwargs .pop ('xlim' , None )
399
+ ylim = kwargs .pop ('ylim' , None )
400
+
381
401
no_nan = np .ravel (darray .values )
382
402
no_nan = no_nan [pd .notnull (no_nan )]
383
403
384
404
primitive = ax .hist (no_nan , ** kwargs )
385
405
386
- ax .set_ylabel ('Count' )
387
-
388
406
ax .set_title ('Histogram' )
389
407
ax .set_xlabel (label_from_attrs (darray ))
390
408
409
+ _update_axes (ax , xincrease , yincrease , xscale , yscale ,
410
+ xticks , yticks , xlim , ylim )
411
+
391
412
return primitive
392
413
393
414
394
- def _update_axes_limits (ax , xincrease , yincrease ):
415
+ def _update_axes (ax , xincrease , yincrease ,
416
+ xscale = None , yscale = None ,
417
+ xticks = None , yticks = None ,
418
+ xlim = None , ylim = None ):
395
419
"""
396
- Update axes in place to increase or decrease
397
- For use in _plot2d
420
+ Update axes with provided parameters
398
421
"""
399
422
if xincrease is None :
400
423
pass
401
- elif xincrease :
402
- ax .set_xlim ( sorted ( ax . get_xlim ()) )
403
- elif not xincrease :
404
- ax .set_xlim ( sorted ( ax . get_xlim (), reverse = True ) )
424
+ elif xincrease and ax . xaxis_inverted () :
425
+ ax .invert_xaxis ( )
426
+ elif not xincrease and not ax . xaxis_inverted () :
427
+ ax .invert_xaxis ( )
405
428
406
429
if yincrease is None :
407
430
pass
408
- elif yincrease :
409
- ax .set_ylim (sorted (ax .get_ylim ()))
410
- elif not yincrease :
411
- ax .set_ylim (sorted (ax .get_ylim (), reverse = True ))
431
+ elif yincrease and ax .yaxis_inverted ():
432
+ ax .invert_yaxis ()
433
+ elif not yincrease and not ax .yaxis_inverted ():
434
+ ax .invert_yaxis ()
435
+
436
+ # The default xscale, yscale needs to be None.
437
+ # If we set a scale it resets the axes formatters,
438
+ # This means that set_xscale('linear') on a datetime axis
439
+ # will remove the date labels. So only set the scale when explicitly
440
+ # asked to. https://github.com/matplotlib/matplotlib/issues/8740
441
+ if xscale is not None :
442
+ ax .set_xscale (xscale )
443
+ if yscale is not None :
444
+ ax .set_yscale (yscale )
445
+
446
+ if xticks is not None :
447
+ ax .set_xticks (xticks )
448
+ if yticks is not None :
449
+ ax .set_yticks (yticks )
450
+
451
+ if xlim is not None :
452
+ ax .set_xlim (xlim )
453
+ if ylim is not None :
454
+ ax .set_ylim (ylim )
412
455
413
456
414
457
# MUST run before any 2d plotting functions are defined since
@@ -500,6 +543,10 @@ def _plot2d(plotfunc):
500
543
If passed, make column faceted plots on this dimension name
501
544
col_wrap : integer, optional
502
545
Use together with ``col`` to wrap faceted plots
546
+ xscale, yscale : 'linear', 'symlog', 'log', 'logit', optional
547
+ Specifies scaling for the x- and y-axes respectively
548
+ xticks, yticks : Specify tick locations for x- and y-axes
549
+ xlim, ylim : Specify x- and y-axes limits
503
550
xincrease : None, True, or False, optional
504
551
Should the values on the x axes be increasing from left to right?
505
552
if None, use the default for the matplotlib function.
@@ -577,7 +624,8 @@ def newplotfunc(darray, x=None, y=None, figsize=None, size=None,
577
624
cmap = None , center = None , robust = False , extend = None ,
578
625
levels = None , infer_intervals = None , colors = None ,
579
626
subplot_kws = None , cbar_ax = None , cbar_kwargs = None ,
580
- ** kwargs ):
627
+ xscale = None , yscale = None , xticks = None , yticks = None ,
628
+ xlim = None , ylim = None , ** kwargs ):
581
629
# All 2d plots in xarray share this function signature.
582
630
# Method signature below should be consistent.
583
631
@@ -723,11 +771,17 @@ def newplotfunc(darray, x=None, y=None, figsize=None, size=None,
723
771
raise ValueError ("cbar_ax and cbar_kwargs can't be used with "
724
772
"add_colorbar=False." )
725
773
726
- _update_axes_limits (ax , xincrease , yincrease )
774
+ _update_axes (ax , xincrease , yincrease , xscale , yscale ,
775
+ xticks , yticks , xlim , ylim )
727
776
728
777
# Rotate dates on xlabels
778
+ # Do this without calling autofmt_xdate so that x-axes ticks
779
+ # on other subplots (if any) are not deleted.
780
+ # https://stackoverflow.com/questions/17430105/autofmt-xdate-deletes-x-axis-labels-of-all-subplots
729
781
if np .issubdtype (xval .dtype , np .datetime64 ):
730
- ax .get_figure ().autofmt_xdate ()
782
+ for xlabels in ax .get_xticklabels ():
783
+ xlabels .set_rotation (30 )
784
+ xlabels .set_ha ('right' )
731
785
732
786
return primitive
733
787
@@ -739,7 +793,9 @@ def plotmethod(_PlotMethods_obj, x=None, y=None, figsize=None, size=None,
739
793
add_labels = True , vmin = None , vmax = None , cmap = None ,
740
794
colors = None , center = None , robust = False , extend = None ,
741
795
levels = None , infer_intervals = None , subplot_kws = None ,
742
- cbar_ax = None , cbar_kwargs = None , ** kwargs ):
796
+ cbar_ax = None , cbar_kwargs = None ,
797
+ xscale = None , yscale = None , xticks = None , yticks = None ,
798
+ xlim = None , ylim = None , ** kwargs ):
743
799
"""
744
800
The method should have the same signature as the function.
745
801
0 commit comments