Setting xticks with log scale results in some overlapping labels

Viewed 537

The following minimal working example on my system (Python 3.5.3, matplotlib 2.0.2) leads to the image that follows the code snippet:

import matplotlib.ticker
from matplotlib import pyplot as plt

npts = [2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0]
error = [0.0005650356236921, 1.9222448391e-06, 1.05166913e-08,
         6.92182e-11, 5.029e-13, 3.8e-15, 4e-16, 2e-16]

fig1, ax1 = plt.subplots()
ax1.plot(npts, error , 'ro')

ax1.set_xscale('log')
ax1.set_yscale('log')

ax1.set_xticks(npts)
ax1.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())
plt.show()

loglog plot with some unwanted xtick labels

Notice that the label 3 x 10^0 should not be there at all. Also, 2 and 4 labels have been overlapped by the unwanted 2 x 10^0 and 4 x 10^0 labels.

This does not happen if I make the plot "semilogy", by dropping the

ax1.set_xscale('log')

line, for example, as can be seen from the image below: semil

Why is this happening and how it can be resolved?

1 Answers
Related