-2

When generating a Matplotlib line or scatter plot, what axis attribute specifies the spacing between ticks? I do not want to explicitly specify where each tick should be as prompted by this related question

ax.ticks(np.arange(-100, 100, 5))

What is the matplotlib axis attribute that controls the tick spacing? It should behave something like the following.

ax.set_x_tick_spacing(5)

This would use the same default xlim and origin point (usually 0) as the default settings.

Steven C. Howell
  • 16,902
  • 15
  • 72
  • 97
  • This question is not a duplicate of the [related question](https://stackoverflow.com/q/12608788/3585557) because this is specifically asking what the matplotlib attribute is to control the tick spacing. That question is much more broad and less focused. Additionally, the accepted answer to the related question would not apply at all in this case. – Steven C. Howell Apr 17 '18 at 13:30
  • I mean, you link to the answer which is almost exactly the same as yours and links to the matplotlib cookbook too. That post has been around a while and contains many ways to manipulate the ticks even if that specific answer is not the accepted one. Therefore, I do think this should be closed as a duplicate – DavidG Apr 17 '18 at 13:43
  • @DavidG This question is asking what the axis attribute is, not simply how to "change the tick frequency". In searching both on StackOverflow and Google for how to do this, I did not find the obscure answer on the related question. Even when writing this quest, StackOverflow does not prompt that the other question is related. – Steven C. Howell Apr 17 '18 at 13:58
  • @DavidG This question is much more clear than the referenced question. You should review the [StackOverflow guidance on "Vote Down"](https://stackoverflow.com/help/privileges/vote-down) because you should "Use your downvotes whenever you encounter an egregiously sloppy, no-effort-expended post, or an answer that is clearly and perhaps dangerously incorrect." This is neither of those. Even if you do consider this a duplicate, that guidance even says "Instead of voting down: If the question is duplicate or off-topic, flag it for moderator attention." – Steven C. Howell Apr 17 '18 at 14:14
  • I do not understand how this cannot be a duplicate, with the answer being exactly the same as those from the duplicate. I hence marked as such. – ImportanceOfBeingErnest Apr 17 '18 at 14:26
  • @ImportanceOfBeingErnest Just because the `matplotlib.ticker.MultipleLocator` object solves the other question, does not mean this question is asking the same thing. Are you saying I should "edit" the poorly worded question from the other OP replacing it with a better worded version of what he should have asked? The accepted answer from the other question is exactly the OPPOSITE to what this question is asking. – Steven C. Howell Apr 17 '18 at 14:59

1 Answers1

0

A more recent answer to the related question illustrates using the matplotlib.ticker.MultipleLocator object. The axis ticks are this type of matplotlib object. Here is an example of it's use.

ax.xaxis.set_major_locator(matplotlib.ticker.MultipleLocator(5))

will place ticks 5 units apart on the x-axis, and

ax.xaxis.set_minor_locator(matplotlib.ticker.MultipleLocator(1))

will place minor ticks 1 unit apart on the x-axis.

Here is an example from the matplotlib Plotting Cookbook

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

X = np.linspace(-15, 15, 1024)
Y = np.sinc(X)

ax = plt.axes()
ax.xaxis.set_major_locator(matplotlib.ticker.MultipleLocator(5))
ax.xaxis.set_minor_locator(matplotlib.ticker.MultipleLocator(1))

plt.plot(X, Y, c = 'k')
plt.show()

enter image description here

Steven C. Howell
  • 16,902
  • 15
  • 72
  • 97