Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix lost ticks #1686

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 12 commits into from
Feb 19, 2013
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
PEP8 fixes/protection try/except
PEP8 fixes, and part of a comment changed to a docstring.
Also added a try/except around each call to _get_pixel_distance_along_axis, so that
if something goes wrong with some off-beat transform, we just don't pad; things
revert back to old matplotlib behavior.
  • Loading branch information
dhyams committed Feb 11, 2013
commit e2a6f92fc7d75167242fc31df2c711532baa523c
34 changes: 20 additions & 14 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,12 +978,18 @@ def _update_ticks(self, renderer):
if interval[0] <= interval[1]:
interval_expanded = interval
else:
interval_expanded = interval[1],interval[0]
interval_expanded = interval[1], interval[0]

if hasattr(self,'_get_pixel_distance_along_axis'):
ds1 = self._get_pixel_distance_along_axis(interval_expanded[0],-1)/2.0
ds2 = self._get_pixel_distance_along_axis(interval_expanded[1],+1)/2.0
interval_expanded = (interval[0]-ds1,interval[1]+ds2)
try:
ds1 = self._get_pixel_distance_along_axis(interval_expanded[0],-1)/2.0
except:
ds1 = 0.0
try:
ds2 = self._get_pixel_distance_along_axis(interval_expanded[1],+1)/2.0
except:
ds2 = 0.0
interval_expanded = (interval[0] - ds1, interval[1] + ds2)

ticks_to_draw = []
for tick, loc, label in tick_tups:
Expand Down Expand Up @@ -1613,26 +1619,26 @@ def _get_offset_text(self):
return offsetText

def _get_pixel_distance_along_axis(self,where,perturb):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and below, PEP8 spaces after commas, please.

# returns the amount, in data coordinates, that a single pixel corresponds to in the
# locality given by "where", which is also given in data coordinates, and is an x coordinate.
# "perturb" is the amount to perturb the pixel. Usually +1 or -1.
"""
Returns the amount, in data coordinates, that a single pixel corresponds to in the
locality given by "where", which is also given in data coordinates, and is an x coordinate.
"perturb" is the amount to perturb the pixel. Usually +1 or -1.
"""

# first figure out the pixel location of the "where" point. We use 1e-10 for the
# y point, so that we remain compatible with log axes.
#
# Note that this routine does not work for a polar axis, because of the 1e-10 below. To
# do things correctly, we need to use rmax instead of 1e-10 for a polar axis. But
# since we do not have that kind of information at this point, we just don't try to
# pad anything for the theta axis of a polar plot.
#
if self.axes.name == 'polar':
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like this approach. What if I wanted to implement my own projection and it didn't even have an inverted data transformation? The safer, though perhaps not best, option would be to make sure self.axes were rectangular (I'd be happy with that).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm; I think the approach is correct here...the only problem with the polar projection is the singularity at r=0, which is why it is specifically exempted. The code should activate for everything else.

As for not having an inverted transformation, would that even make sense? Not being able to go from pixel coordinates to data coordinates? Seems like there would be quite a few other things that would break in that situation; not just this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @pelson's point might be that it's not a great idea to single out a particular projection here -- the user can install and use any number of projections. I haven't given it much thought, but it would be great to find a way to detect this case other than just checking for polar.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The singling out, though, is for the one projection that I know it doesn't work for. It should work for others, but just for safety, in the latest commits, I wrapped a try/except around each call that gets the amount of padding to use, which defaults the padding to zero in the case of a problem. This recovers old matplotlib behavior.

return 0.0

#first figure out the pixel location of the "where" point. We use 1e-10 for the
#y point, so that we remain compatible with log axes.
trans = self.axes.transData # transformation from data coords to display coords
transinv = trans.inverted() # transformation from display coords to data coords
pix = trans.transform_point((where,1e-10))
ptp = transinv.transform_point((pix[0]+perturb,pix[1])) # perturb the pixel.
dx = abs(ptp[0]-where)
pix = trans.transform_point((where, 1e-10))
ptp = transinv.transform_point((pix[0] + perturb, pix[1])) # perturb the pixel.
dx = abs(ptp[0] - where)

return dx

Expand Down