Reputation: 31
I run the attached code. On figure, ticks are labed from 0 to 1 by a step of 0.2, but I get -0.2 to 1.2 by running ax.get_xticks(). This is a bug, isn't it?
import matplotlib.pyplot as plt
fig = plt.figure()
plt.ion()
ax = fig.add_subplot(111)
ax.get_xticks()
ax.plot([-0.05, 1.06], [0.4, 0.5])
ax.get_xticks()
I expected to get array([0. , 0.2, 0.4, 0.6, 0.8, 1.]) other than array([-0.2, 0. , 0.2, 0.4, 0.6, 0.8, 1. , 1.2])
Upvotes: 2
Views: 3233
Reputation: 36658
Not a bug. Plotting just outside of the default 0 to 1 range in the x axis will cause the additional ticks to be added in order to rescale the axis to accommodate the new data range. However, they are not displayed because they are outside of the xlim
bounds.
You can see this by setting the ticks and xlim manually:
fig = plt.figure()
plt.ion()
ax = fig.add_subplot(111)
ax.plot([-0.05, 1.06], [0.4, 0.5])
xlim = ax.get_xlim()
ax.set_xticks([-100.0, 0.0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 100.0])
ax.set_xlim(xlim)
To get which xticks
are shown you can use:
x0, x1 = ax.get_xlim()
visible_ticks = [t for t in ax.get_xticks() if t>=x0 and t<=x1]
Upvotes: 3