-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Additional axis kwargs #2294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Additional axis kwargs #2294
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4973c49
Support xscale, yscale, xticks, yticks, xlim, ylim kwargs.
0ab162c
Forgot to replace autofmt_xdate for 2D plots.
9f220ba
Use matplotlib's axis inverting methods.
83f1d24
Add what's new and docs.
092dd29
doh
b90a7ef
Minor fixes.
f82ae0a
fix section header
shoyer 8947d43
Fix assert statements.
4c767d3
fix whats-new
56dc546
Merge branch 'master' into axis-kwargs
shoyer 4d846a9
Merge branch 'master' into axis-kwargs
dcherian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -270,6 +270,10 @@ def line(darray, *args, **kwargs): | |
Coordinates for x, y axis. Only one of these may be specified. | ||
The other coordinate plots values from the DataArray on which this | ||
plot method is called. | ||
xscale, yscale : 'linear', 'symlog', 'log', 'logit', optional | ||
Specifies scaling for the x- and y-axes respectively | ||
xticks, yticks : Specify tick locations for x- and y-axes | ||
xlim, ylim : Specify x- and y-axes limits | ||
xincrease : None, True, or False, optional | ||
Should the values on the x axes be increasing from left to right? | ||
if None, use the default for the matplotlib function. | ||
|
@@ -305,8 +309,14 @@ def line(darray, *args, **kwargs): | |
hue = kwargs.pop('hue', None) | ||
x = kwargs.pop('x', None) | ||
y = kwargs.pop('y', None) | ||
xincrease = kwargs.pop('xincrease', True) | ||
yincrease = kwargs.pop('yincrease', True) | ||
xincrease = kwargs.pop('xincrease', None) # default needs to be None | ||
yincrease = kwargs.pop('yincrease', None) | ||
xscale = kwargs.pop('xscale', None) # default needs to be None | ||
yscale = kwargs.pop('yscale', None) | ||
xticks = kwargs.pop('xticks', None) | ||
yticks = kwargs.pop('yticks', None) | ||
xlim = kwargs.pop('xlim', None) | ||
ylim = kwargs.pop('ylim', None) | ||
add_legend = kwargs.pop('add_legend', True) | ||
_labels = kwargs.pop('_labels', True) | ||
if args is (): | ||
|
@@ -343,7 +353,8 @@ def line(darray, *args, **kwargs): | |
xlabels.set_rotation(30) | ||
xlabels.set_ha('right') | ||
|
||
_update_axes_limits(ax, xincrease, yincrease) | ||
_update_axes(ax, xincrease, yincrease, xscale, yscale, | ||
xticks, yticks, xlim, ylim) | ||
|
||
return primitive | ||
|
||
|
@@ -378,37 +389,69 @@ def hist(darray, figsize=None, size=None, aspect=None, ax=None, **kwargs): | |
""" | ||
ax = get_axis(figsize, size, aspect, ax) | ||
|
||
xincrease = kwargs.pop('xincrease', None) # default needs to be None | ||
yincrease = kwargs.pop('yincrease', None) | ||
xscale = kwargs.pop('xscale', None) # default needs to be None | ||
yscale = kwargs.pop('yscale', None) | ||
xticks = kwargs.pop('xticks', None) | ||
yticks = kwargs.pop('yticks', None) | ||
xlim = kwargs.pop('xlim', None) | ||
ylim = kwargs.pop('ylim', None) | ||
|
||
no_nan = np.ravel(darray.values) | ||
no_nan = no_nan[pd.notnull(no_nan)] | ||
|
||
primitive = ax.hist(no_nan, **kwargs) | ||
|
||
ax.set_ylabel('Count') | ||
|
||
ax.set_title('Histogram') | ||
ax.set_xlabel(label_from_attrs(darray)) | ||
|
||
_update_axes(ax, xincrease, yincrease, xscale, yscale, | ||
xticks, yticks, xlim, ylim) | ||
|
||
return primitive | ||
|
||
|
||
def _update_axes_limits(ax, xincrease, yincrease): | ||
def _update_axes(ax, xincrease, yincrease, | ||
xscale=None, yscale=None, | ||
xticks=None, yticks=None, | ||
xlim=None, ylim=None): | ||
""" | ||
Update axes in place to increase or decrease | ||
For use in _plot2d | ||
Update axes with provided parameters | ||
""" | ||
if xincrease is None: | ||
pass | ||
elif xincrease: | ||
ax.set_xlim(sorted(ax.get_xlim())) | ||
elif not xincrease: | ||
ax.set_xlim(sorted(ax.get_xlim(), reverse=True)) | ||
elif xincrease and ax.xaxis_inverted(): | ||
ax.invert_xaxis() | ||
elif not xincrease and not ax.xaxis_inverted(): | ||
ax.invert_xaxis() | ||
|
||
if yincrease is None: | ||
pass | ||
elif yincrease: | ||
ax.set_ylim(sorted(ax.get_ylim())) | ||
elif not yincrease: | ||
ax.set_ylim(sorted(ax.get_ylim(), reverse=True)) | ||
elif yincrease and ax.yaxis_inverted(): | ||
ax.invert_yaxis() | ||
elif not yincrease and not ax.yaxis_inverted(): | ||
ax.invert_yaxis() | ||
|
||
# The default xscale, yscale needs to be None. | ||
# If we set a scale it resets the axes formatters, | ||
# This means that set_xscale('linear') on a datetime axis | ||
# will remove the date labels. So only set the scale when explicitly | ||
# asked to. https://github.com/matplotlib/matplotlib/issues/8740 | ||
if xscale is not None: | ||
ax.set_xscale(xscale) | ||
if yscale is not None: | ||
ax.set_yscale(yscale) | ||
|
||
if xticks is not None: | ||
ax.set_xticks(xticks) | ||
if yticks is not None: | ||
ax.set_yticks(yticks) | ||
|
||
if xlim is not None: | ||
ax.set_xlim(xlim) | ||
if ylim is not None: | ||
ax.set_ylim(ylim) | ||
|
||
|
||
# MUST run before any 2d plotting functions are defined since | ||
|
@@ -500,6 +543,10 @@ def _plot2d(plotfunc): | |
If passed, make column faceted plots on this dimension name | ||
col_wrap : integer, optional | ||
Use together with ``col`` to wrap faceted plots | ||
xscale, yscale : 'linear', 'symlog', 'log', 'logit', optional | ||
Specifies scaling for the x- and y-axes respectively | ||
xticks, yticks : Specify tick locations for x- and y-axes | ||
xlim, ylim : Specify x- and y-axes limits | ||
xincrease : None, True, or False, optional | ||
Should the values on the x axes be increasing from left to right? | ||
if None, use the default for the matplotlib function. | ||
|
@@ -577,7 +624,8 @@ def newplotfunc(darray, x=None, y=None, figsize=None, size=None, | |
cmap=None, center=None, robust=False, extend=None, | ||
levels=None, infer_intervals=None, colors=None, | ||
subplot_kws=None, cbar_ax=None, cbar_kwargs=None, | ||
**kwargs): | ||
xscale=None, yscale=None, xticks=None, yticks=None, | ||
xlim=None, ylim=None, **kwargs): | ||
# All 2d plots in xarray share this function signature. | ||
# Method signature below should be consistent. | ||
|
||
|
@@ -723,11 +771,17 @@ def newplotfunc(darray, x=None, y=None, figsize=None, size=None, | |
raise ValueError("cbar_ax and cbar_kwargs can't be used with " | ||
"add_colorbar=False.") | ||
|
||
_update_axes_limits(ax, xincrease, yincrease) | ||
_update_axes(ax, xincrease, yincrease, xscale, yscale, | ||
xticks, yticks, xlim, ylim) | ||
|
||
# Rotate dates on xlabels | ||
# Do this without calling autofmt_xdate so that x-axes ticks | ||
# on other subplots (if any) are not deleted. | ||
# https://stackoverflow.com/questions/17430105/autofmt-xdate-deletes-x-axis-labels-of-all-subplots | ||
if np.issubdtype(xval.dtype, np.datetime64): | ||
ax.get_figure().autofmt_xdate() | ||
for xlabels in ax.get_xticklabels(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missed this bit in an older PR. |
||
xlabels.set_rotation(30) | ||
xlabels.set_ha('right') | ||
|
||
return primitive | ||
|
||
|
@@ -739,7 +793,9 @@ def plotmethod(_PlotMethods_obj, x=None, y=None, figsize=None, size=None, | |
add_labels=True, vmin=None, vmax=None, cmap=None, | ||
colors=None, center=None, robust=False, extend=None, | ||
levels=None, infer_intervals=None, subplot_kws=None, | ||
cbar_ax=None, cbar_kwargs=None, **kwargs): | ||
cbar_ax=None, cbar_kwargs=None, | ||
xscale=None, yscale=None, xticks=None, yticks=None, | ||
xlim=None, ylim=None, **kwargs): | ||
""" | ||
The method should have the same signature as the function. | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why we weren't using these methods earlier?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so!