Skip to content

weird bug when using custom formatter; tick labels overlapping #8401

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

Closed
jason-s opened this issue Mar 29, 2017 · 3 comments
Closed

weird bug when using custom formatter; tick labels overlapping #8401

jason-s opened this issue Mar 29, 2017 · 3 comments

Comments

@jason-s
Copy link

jason-s commented Mar 29, 2017

Bug report

Bug summary

I use a custom tick formatter in my plot. It worked great with an older version of matplotlib but I just upgraded to 2.0 and I see overlapping tickmark labels (the default labels are still there):

image

Code for reproduction

import numpy as np
import matplotlib
import matplotlib.pyplot as plt


class MyLogFormatter(matplotlib.ticker.LogFormatter):
    def __call__(self, x, pos=None):
        e = np.floor(np.log10(x))
        m = x/(10**e)
        if x < 0.01:
            return '%de%d' % (round(m), e)
        elif x < 1:
            return '%.2f' % x
        elif x < 10:
            return '%.1f' % x
        elif x < 1000:
            return '%.0f' % x
        else:
            return '%de%d' % (round(m), e)
    @staticmethod
    def ticks(xmin, xmax):
        ndecades = np.ceil(np.log10(xmax/xmin))
        if ndecades > 6:
            mantissas = [1]
        elif ndecades > 3:
            mantissas = [1,3]
        else:
            mantissas = [1,2,5]
        roundnumbers = (10**np.arange(np.floor(np.log10(xmin)), np.ceil(np.log10(xmax)))*np.atleast_2d(mantissas).T).T.flatten()
        return np.hstack((xmin, [x for x in roundnumbers if xmin*1.25 < x < 0.8*xmax], xmax))        

data = 0
for k in xrange(4):
    data += np.random.randint(6, size=100000)
    
count,edges = np.histogram(data,10)
x = 100 * (1.1 ** edges)

fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot(1,1,1,xscale='log')
ax.bar(x[:-1], count, width=x[1:] - x[:-1], color='red',edgecolor='black',align='edge')
xmin = min(x)
xmax = max(x)
LF = MyLogFormatter(labelOnlyBase=False)
ax.set_xticks(LF.ticks(xmin, xmax))
ax.xaxis.set_major_formatter(LF)     

Actual outcome

  • The output produced by the above code, which may be a screenshot, console output, etc.

image

Expected outcome

previous version was Matplotlib 1.5.1
(can't post right now, I will have to downgrade my anaconda version)

Matplotlib version

  • Matplotlib 2.0
    Python 2.7.12 |Anaconda 4.1.1 (64-bit)| (default, Jun 29 2016, 11:07:13) [MSC v.1500 64 bit (AMD64)] on win32
@jason-s
Copy link
Author

jason-s commented Mar 29, 2017

Oh, never mind, adding this line suppresses the minor tick labels:

ax.xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())

Incidentally, what changed around minor tick labels?

@jason-s jason-s closed this as completed Mar 29, 2017
@tacaswell
Copy link
Member

See http://matplotlib.org/users/dflt_style_changes.html#logformatter-labeling-of-minor-ticks for documentation of the changes to minor ticks.

We made it better by default, but seem to have broken a bunch of people who were doing custom things.

@phobson
Copy link
Member

phobson commented Mar 30, 2017

duplicate of #8386

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants