Skip to content

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

@jason-s

Description

@jason-s

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy