Adjust number of ticks based on length of axis #5588

Merged
merged 13 commits into from Dec 14, 2015

Conversation

Projects
None yet
4 participants
Owner

mdboom commented Nov 30, 2015

Follow on to #5494.

mdboom added this to the next major release (2.0) milestone Nov 30, 2015

mdboom added the needs_review label Nov 30, 2015

@tacaswell tacaswell commented on an outdated diff Dec 1, 2015

lib/matplotlib/ticker.py
+ *symmetric*
+ If True, autoscaling will result in a range symmetric
+ about zero.
+
+ *prune*
+ ['lower' | 'upper' | 'both' | None]
+ Remove edge ticks -- useful for stacked or ganged plots
+ where the upper tick of one axes overlaps with the lower
+ tick of the axes above it.
+ If prune=='lower', the smallest tick will
+ be removed. If prune=='upper', the largest tick will be
+ removed. If prune=='both', the largest and smallest ticks
+ will be removed. If prune==None, no ticks will be removed.
+
+ """
+ if 'nbins' in kwargs:
@tacaswell

tacaswell Dec 1, 2015

Owner

need to over-ride set_params and black-list nbins there as well.

@tacaswell tacaswell and 1 other commented on an outdated diff Dec 1, 2015

lib/matplotlib/ticker.py
@@ -1449,6 +1449,48 @@ def view_limits(self, dmin, dmax):
return np.take(self.bin_boundaries(dmin, dmax), [0, -1])
+class AutoSpacedLocator(MaxNLocator):
+ """
+ Behaves like a MaxNLocator, except N is automatically determined
+ from the length of the axis.
+ """
+ def __init__(self, *args, **kwargs):
@tacaswell

tacaswell Dec 1, 2015

Owner

don't actually use the *args.

@tacaswell

tacaswell Dec 1, 2015

Owner

and can we enumerate the kwargs in the signature?

@mdboom

mdboom Dec 1, 2015

Owner

Done. The kwargs list is now explicit.

@tacaswell tacaswell and 1 other commented on an outdated diff Dec 1, 2015

lib/matplotlib/ticker.py
@@ -1872,7 +1914,13 @@ def tick_values(self, vmin, vmax):
return self.raise_if_exceeds(np.array(ticklocs))
-class AutoLocator(MaxNLocator):
@tacaswell

tacaswell Dec 1, 2015

Owner

I am 50/50 on doing the renaming like this. If we black list setting nbins in set_params this will break user code (like an SO question I wrote this morning).

@tacaswell

tacaswell Dec 1, 2015

Owner

I am talking my self out of any argument I have against doing this, but still don't like it.

@mdboom

mdboom Dec 1, 2015

Owner

Yeah, I hear you. The fact is AutoLocator is basically an alias for the "default locator" (though that's not obvious). Anyone who actually cares which they want should use MaxNLocator. A documentation problem, maybe?

Owner

tacaswell commented Dec 1, 2015

set_params should blacklist nbins as it will be ignored.

Owner

mdboom commented Dec 1, 2015

set_params should blacklist nbins as it will be ignored.

You mean, raise an exception if it's there?

Owner

tacaswell commented Dec 1, 2015

Yes

On Tue, Dec 1, 2015 at 6:22 PM Michael Droettboom notifications@github.com
wrote:

set_params should blacklist nbins as it will be ignored.

You mean, raise an exception if it's there?


Reply to this email directly or view it on GitHub
#5588 (comment)
.

Owner

mdboom commented Dec 1, 2015

Done.

Owner

efiring commented Dec 2, 2015

Does this mean that the default will now always be AutoLocator? And that a user will no longer be able to use locator_params(nbins=4) to adjust it? If so, I think this is a mistake. Letting the initial selection of nbins be done by an algorithm is good; but making it impossible to override this with a kwarg is not good. Or have I misunderstood?

Owner

mdboom commented Dec 2, 2015

@efiring: That's a good point. I've simplified this so that nbins == None can be used to indicate auto mode. This is the default for "new style", whereas classic mode will use the old default of 9. This actually significantly reduces the patch size as well.

Owner

tacaswell commented Dec 2, 2015

It might be better to use the string 'auto' as the default? It is clearer
we will be doing something magic.

On Wed, Dec 2, 2015, 07:54 Michael Droettboom notifications@github.com
wrote:

@efiring https://github.com/efiring: That's a good point. I've
simplified this so that nbins == None can be used to indicate auto mode.
This is the default for "new style", whereas classic mode will use the old
default of 9. This actually significantly reduces the patch size as well.


Reply to this email directly or view it on GitHub
#5588 (comment)
.

Owner

mdboom commented Dec 2, 2015

It might be better to use the string 'auto' as the default? It is clearer we will be doing something magic.

Sure.

Member

WeatherGod commented Dec 2, 2015

Right, "auto" is better for meaning automatic, meanwhile, None can continue
to mean "do the default thing".

On Wed, Dec 2, 2015 at 8:24 AM, Michael Droettboom <notifications@github.com

wrote:

It might be better to use the string 'auto' as the default? It is clearer
we will be doing something magic.

Sure.


Reply to this email directly or view it on GitHub
#5588 (comment)
.

Owner

efiring commented Dec 2, 2015

Good. Now, I am wondering whether there is any point in making the additional AutoLocator class; wouldn't it be simpler to fold the small modification of __init__ into MaxNLocator itself? Then the selection of one functionality or the other is entirely a matter of how the nbins kwarg is set.

Owner

mdboom commented Dec 2, 2015

Good. Now, I am wondering whether there is any point in making the additional AutoLocator class; wouldn't it be simpler to fold the small modification of init into MaxNLocator itself? Then the selection of one functionality or the other is entirely a matter of how the nbins kwarg is set.

AutoLocator isn't new -- it's been there forever. It sets up reasonable defaults for MaxNLocator. I agree it doesn't have much reason to exist, but removing would theoretically break backward compatibility.

Owner

mdboom commented Dec 9, 2015

Just rebased. I think this is good-to-go once Travis passes.

mdboom added some commits Nov 16, 2015

@mdboom mdboom Fix #5488: Adjust nticks based on length of axis d203807
@mdboom mdboom Cache tick space calculation 63c9b43
@mdboom mdboom Make behavior backward compatible 926c7e0
@mdboom mdboom Fix example 53e3491
@mdboom mdboom Add test 7a9a596
@mdboom mdboom Be explicit about kwargs 5b16906
@mdboom mdboom Blacklist nbins 2e1af1f
@mdboom mdboom Simplify -- use nbins == None to indicate auto 5a2f962
@mdboom mdboom Use 'auto' instead of None 5b75d0b
@mdboom mdboom Bugfix 6137ecc
@mdboom mdboom Add get_tick_space to _DummyAxis 97e0d9f
@mdboom mdboom Update test image
b4aa340
@mdboom mdboom Fix backward compatibility on MaxNLocator
eafa79b
Owner

mdboom commented Dec 14, 2015

And... it's green (on Travis)

@efiring efiring added a commit that referenced this pull request Dec 14, 2015

@efiring efiring Merge pull request #5588 from mdboom/dynamic-ticking
Adjust number of ticks based on length of axis
7a29ad1

@efiring efiring merged commit 7a29ad1 into matplotlib:master Dec 14, 2015

2 of 3 checks passed

continuous-integration/appveyor/pr AppVeyor build failed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
coverage/coveralls Coverage increased (+0.02%) to 68.272%
Details

efiring removed the needs_review label Dec 14, 2015

Owner

efiring commented Dec 14, 2015

@mdboom, I suspect some corner cases will turn up and tweaks might be needed, but I think it makes sense to merge this so that it will get more exercise.

Owner

mdboom commented Dec 14, 2015

Agreed. Thanks.

Owner

tacaswell commented Dec 17, 2015

This was merged to master, but the merge is in the v2.0.x branch. I think something fishy went on around b7c7c5e but I am not sure.

@efiring efiring added a commit that referenced this pull request Dec 20, 2015

@efiring @tacaswell efiring + tacaswell Merge pull request #5588 from mdboom/dynamic-ticking
Adjust number of ticks based on length of axis
7488da6
Owner

tacaswell commented Dec 20, 2015

backported as 7488da6

Owner

efiring commented Dec 21, 2015

@mdboom, I finally took a look to see what this does on master, with a minimal plot plot([1,2,3]) on OS-X, using the qt4agg backend, and I think there is a problem: it is coming up with 11 ticks. Yes, the labels all fit, but it looks bad--very cluttered. I suggest that the default max number should be much smaller, more similar to what it used to be.

Owner

mdboom commented Dec 22, 2015

@efiring: You're suggesting a hard maximum? Just adding more space is unlikely to be a good fix, since in cases with many small axes you do want to allow the ticks to get reasonably close together.

Owner

efiring commented Dec 22, 2015

Yes, a hard maximum is what I had in mind. Then the behavior would not change for the default single-axes-per-figure case, but the multiple axes case would still be supported.

Owner

efiring commented Dec 22, 2015

Of course that hard maximum could be controllable with yet another rcParam if necessary. We didn't have one for that before, though.

Owner

mdboom commented Dec 22, 2015

I'm fine with just setting the max to the old hard-coded value (which appears to be 9) and we can always add an rcParam later if people want more control.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment