4

Trying to plot, I got the following error from matplotlib:

TypeError: %d format: a number is required, not numpy.float64

This is the complete traceback (I've modified path names):

Traceback (most recent call last):
  File ".../plotmod.py", line 154, in _plot
    fig.autofmt_xdate()
  File ".../local/lib/python2.7/site-packages/matplotlib/figure.py", line 426, in autofmt_xdate
    for label in ax.get_xticklabels():
  File ".../local/lib/python2.7/site-packages/matplotlib/axes.py", line 2620, in get_xticklabels
    self.xaxis.get_ticklabels(minor=minor))
  File ".../local/lib/python2.7/site-packages/matplotlib/axis.py", line 1118, in get_ticklabels
    return self.get_majorticklabels()
  File ".../local/lib/python2.7/site-packages/matplotlib/axis.py", line 1102, in get_majorticklabels
    ticks = self.get_major_ticks()
  File ".../local/lib/python2.7/site-packages/matplotlib/axis.py", line 1201, in get_major_ticks
    numticks = len(self.get_major_locator()())
  File ".../local/lib/python2.7/site-packages/matplotlib/dates.py", line 595, in __call__
    'RRuleLocator estimated to generate %d ticks from %s to %s: exceeds Locator.MAXTICKS * 2 (%d) ' % (estimate, dmin, dmax, self.MAXTICKS * 2))
TypeError: %d format: a number is required, not numpy.float64

Where can this error come from?

Some basic research I've made results:

  • the error is not the real error, but instead one which is caused while trying to format the RuntimeError message that matplotlib.dates raises
  • the formatting error was due to python's %d, which, it seems, cannot handle numpy.float64 instances
  • the instance which has contained that data type is either estimate, which is some inner calculation result of matplotlib, or MAXTICKS, which is probably a constant, hence I tend to believe it's the first option
  • the calculation of estimate involves date2num which should return legitimate values, and _get_unit() and _get_interval(), which go deep enough into the module, and this is where my research stops.

I can easily reproduce the error in my entire software framework, but I can't isolate it for easy reproduction code. I think it tends to happen when the entire axis that should be plotted is very short (say, up to a few minutes long).

Any thoughts?

1

2 Answers 2

4

It seems you have a NaN or infinity that you are trying to format as an integer which raises the error (there's no such thing as a NaN or Inf for the int datatype).

In [1]: import numpy as np

In [2]: '%d' % np.float64(42)
Out[2]: '42'

In [3]: '%d' % np.float64('nan')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

<ipython console> in <module>()

TypeError: %d format: a number is required, not numpy.float64

In [4]: '%d' % np.float64('inf')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

ipython console> in <module>()

TypeError: %d format: a number is required, not numpy.float64

You could go into the matplotlib file (or use a python debugger) that generates the error and change the print line to have a %f which will work with all numpy floats. ('%f' % np.float64('nan') returns 'nan').

Sign up to request clarification or add additional context in comments.

Comments

0

Convert numpy float to Python float manually.

np.asscalar(np.float64(42)) 

2 Comments

1. Since the numbers appear inside the matplotlib module, I can't do that without editing it. I wish to avoid that, as this is an external module.
2. This will solve the formatting error, but as I wrote, this is not the real error - this will only format the RuntimeError properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.