2

I'm trying to set tick mark positions in matplotlib. I get errors when running the following minimal example:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import MultipleLocator

x = 10.*np.random.randn(1000)
y = 10.*np.random.randn(1000)

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(x, y)
ax.xaxis.set_major_formatter(MultipleLocator(1.))
ax.yaxis.set_major_formatter(MultipleLocator(1.))
plt.show()

The error lies with the two lines that set the x- and y-axis tick marks. If I use NullFormatter() instead, or omit these lines entirely, the code runs fine and produces the expected plot. The above code, however, returns the following error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1413, in __call__
    return self.func(*args)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 245, in resize
    self.show()
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 248, in draw
    FigureCanvasAgg.draw(self)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 394, in draw
    self.figure.draw(self.renderer)
  File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 798, in draw
    func(*args)
  File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 1946, in draw
    a.draw(renderer)
  File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 971, in draw
    tick_tups = [ t for t in self.iter_ticks()]
  File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 906, in iter_ticks
    self.major.formatter.set_locs(majorLocs)
AttributeError: MultipleLocator instance has no attribute 'set_locs'

I've tried googling this error, but I can't find anyone else who has a similar problem. Any ideas as to why the use of locators is yielding errors?

1
  • I should have mentioned I'm using python 2.7.2+ Commented Dec 17, 2011 at 22:05

1 Answer 1

8

MultipleLocator is a locator, not a formatter. You want to use

ax.xaxis.set_major_locator(MultipleLocator(1.))
ax.yaxis.set_major_locator(MultipleLocator(1.))

This works for me (doesn't look too pretty using 1, but it works).

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

1 Comment

Thanks! I just figured this out a second ago.

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.