2

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?

0mid
  • 149
  • 5
  • 1
    Those additional labels come from minor ticklabels. You would need to enforce a `matplotlib.ticker.NullFormatter()` on the axes, see [duplicate question](https://stackoverflow.com/questions/42845694/matplotlib-setting-x-limits-also-forces-tick-labels)'s answer. – ImportanceOfBeingErnest Jun 02 '17 at 12:08
  • @ImportanceOfBeingErnest: Thank you. I did a lot of search on Stackoverflow, but I did not find the question you linked to. I confirm that enforcing a matplotlib.ticker.NullFormatter() solves the problem. I am perplexed though. This behavior change from matplotlib 1.5.x to 2.0.y is highly counterintuitive. Any idea why this was introduced? – 0mid Jun 02 '17 at 18:44

1 Answers1

0

You don't need this line ax1.set_xscale('log'). The reason being that the data points are not logarithmic.

DavidG
  • 24,279
  • 14
  • 89
  • 82
Oluwafemi Sule
  • 36,144
  • 1
  • 56
  • 81
  • 2
    This doesn't answer the question. – Thomas Kühn Jun 02 '17 at 11:53
  • @Oluwafemi Sule: Any set of data points (x, y) may be represented using different graphs, depending on the purpose you have in mind. You may graph the (x, y) points directly; or you may graph (f(x), g(y)), as long as x is in the domain of f and y is in the domain of g. In my case, I have f:=log and g:=log. Data is just that, data. We choose how to represent and interpret it. – 0mid Jun 02 '17 at 18:53
  • 1
    @Omid You're right. The answer by ImportanceOfBeingErnest is really enlightening. I'm seeing a whole new possiblity with formatting tickers. – Oluwafemi Sule Jun 02 '17 at 19:03