1

I am using matplot lib to plot some curves with the x-axis set to a log scale. I figured out how to set custom ticks from set ticks with logarithmic scale:

import matplotlib
from matplotlib import pyplot as plt


ax = plt.gca()
ax.set_xscale('log')
ax.set_xticks([0.001, 0.003, 0.010, 0.033, 0.10, 0.33, 1.0, 3.3, 10.0, 33.0, 100.0])
ax.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())
ax.get_xaxis().set_tick_params(which='minor', size=0, width=0)
plt.savefig('logscale.png')
plt.close()

This code produces the following plot:

enter image description here

As you can see, the two leftmost ticks are 0.00 instead of 0.001 and 0.003 as they should be. How can I format these to show more digits after the decimal point?

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268

1 Answers1

2

ScalarFormatter automatically determines a useful number of significant digits to show. This is most often desired. However, ScalarFormatter is not optimized to be used on log scales. Hence it will fail in this case.

Because here, we don't need much of the capabilities of ScalarFormatter anyways, we may opt for a StrMethodFormatter instead. For three digits, use e.g.

matplotlib.ticker.StrMethodFormatter("{x:1.3f}")
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712