Reputation: 187
I have code which looks like this:
plt.figure()
plt.subplot(211)
plt.plot(timestamps_SW, np.random.randn(len(testLabelsSW_ds)), label='truth')
plt.plot(timestamps_SW, np.random.randn(len(testLabelsSW_ds)), label='pred.')
plt.locator_params(axis='y', nbins=5)
plt.legend()
plt.subplot(212)
plt.plot(timestamps_SE,np.random.randn(len(testLabelsSE_ds)), label='truth')
plt.plot(timestamps_SE,np.random.randn(len(testLabelsSE_ds)), label='pred.')
plt.locator_params(axis='y', nbins=5)
plt.legend()
The resulting plot looks as such:
Clearly each y-axis doesn't have 5 ticks. How do I fix?
Upvotes: 1
Views: 64
Reputation: 339052
Using plt.locator_params(..., nbins=5)
you ask the default locator to use 5 bins. The default locator is an AutoLocator
. It's a subclass of the MaxNLocator
. MaxN means, it will try to find a maximum of N
nice locations, where N
is equal to nbins + 1
.
"Nice" locations means, e.g. something like 1.0, 0.25 etc are considered "nice", while something like 0.761 is of course not so nice.
The two constraints (a) "nice" and (b) "N locations" are of course seldomly fulfillable at the same time. Therefore "nice" has priority here.
If you leave out the constraint (a), you can get N
locations simply by placing the ticks manually,
plt.yticks(np.linspace(*plt.ylim(), 5))
Upvotes: 1