8000 Allow zero sized ticks by scopatz · Pull Request #7959 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 8 additions & 2 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -2020,7 +2020,10 @@ def get_tick_space(self):
# There is a heuristic here that the aspect ratio of tick text
# is no more than 3:1
size = tick.label1.get_size() * 3
return int(np.floor(length / size))
if size > 0:
return int(np.floor(length / size))
else:
Copy link
Member

Choose a reason for hiding this comment

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

doesn't need the else. Also, I'd invert this and flag the special case

if size <= 0:
    return return 2**31 - 1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For what it is worth, keeping the else clause is about 10% faster. The order shouldn't matter and doesn't affect timings that I have noticed.

scopatz@artemis ~ $ def f(x):
`·.,¸,.·*¯`·.,¸,.·*     if x:
`·.,¸,.·*¯`·.,¸,.·*         return 42
`·.,¸,.·*¯`·.,¸,.·*     else:
`·.,¸,.·*¯`·.,¸,.·*         return 0
`·.,¸,.·*¯`·.,¸,.·*     
`·.,¸,.·*¯`·.,¸,.·* 
scopatz@artemis ~ $ def g(x):
`·.,¸,.·*¯`·.,¸,.·*     if x:
`·.,¸,.·*¯`·.,¸,.·*         return 42
`·.,¸,.·*¯`·.,¸,.·*     return 0
`·.,¸,.·*¯`·.,¸,.·* 
scopatz@artemis ~ $ timeit! f(True)
10000000 loops, best of 3: 57.6 ns per loop
scopatz@artemis ~ $ timeit! g(True)
10000000 loops, best of 3: 63.6 ns per loop
scopatz@artemis ~ $ timeit! f(False)
10000000 loops, best of 3: 58 ns per loop
scopatz@artemis ~ $ timeit! g(False)
10000000 loops, best of 3: 64 ns per loop
scopatz@artemis ~ $ 1 - (58.0/64)
0.09375

Copy link
Member
@story645 story645 Jan 27, 2017

Choose a reason for hiding this comment

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

I know order doesn't matter, I just think from a clarity/maintance point of view the special case should be in the if.

Though now I'm wondering if there's a way to modify int(np.floor(length/size)) such that it'll catch the size<=0 case

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is probably a bit of apples and oranges since I find the way I wrote it clearest. I looked around for something in numpy that would cast infinities to integers better, but I didn't come up with anything.

Copy link
Member
@story645 story645 Jan 27, 2017

Choose a reason for hiding this comment

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

shrugs

Copy link
Member

Choose a reason for hiding this comment

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

I also find the if else statement clearer.

Copy link
Member

Choose a reason for hiding this comment

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

shrugs wasn't gonna put up a fight

return 2**31 - 1


class YAxis(Axis):
Expand Down Expand Up @@ -2353,4 +2356,7 @@ def get_tick_space(self):
tick = self._get_tick(True)
# Having a spacing of at least 2 just looks good.
size = tick.label1.get_size() * 2.0
return int(np.floor(length / size))
if size > 0:
return int(np.floor(length / size))
else:
Copy link
Member

Choose a reason for hiding this comment

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

same, don't need else

return 2**31 - 1
10 changes: 10 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2341,6 +2341,16 @@ def test_manage_xticks():
assert_array_equal(old_xlim, new_xlim)


@cleanup
def test_tick_space_size_0():
# allow font size to be zero, which affects ticks when there is
# no other text in the figure.
plt.plot([0, 1], [0, 1])
matplotlib.rcParams.update({'font.size': 0})
b = io.BytesIO()
plt.savefig(b, dpi=80, format='raw')


@image_comparison(baseline_images=['errorbar_basic', 'errorbar_mixed',
'errorbar_basic'])
def test_errorbar():
Expand Down
0