1

I plotted a graph in matplotlib, and it looks like this, where the x-axis ticks show correctly :

Here is my code:

import matplotlib
import matplotlib.pyplot as plt
fig1, ax1 = plt.subplots()
ax1.plot([1, 2, 3], [1,2,3])
ax1.set_xscale('log')
plt.xlim(1,100)
ax1.set_xticks([1,2,3,4,5,6,7,8,9,10])
ax1.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())
plt.show()

Then I adjusted the limits of the x-axis, plt.xlim(1,10) Here is the new code:

import matplotlib
import matplotlib.pyplot as plt
fig1, ax1 = plt.subplots()
ax1.plot([1, 2, 3], [1,2,3])
ax1.set_xscale('log')
plt.xlim(1,10)
ax1.set_xticks([1,2,3,4,5,6,7,8,9,10])
ax1.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())
plt.show()

And now there are some wrong numbers that I don't need:

How may I achieve the first graph as the small x-axis limits ?

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
Damao
  • 35
  • 4

1 Answers1

1

I was having the same problem when I updated to matplotlib 2+. I found the solution in this thread. The problem seems to be the minor ticks, which you can remove with the following command:

plt.gca().xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())