0

Matplotlib inherently sets logarithmic scales to use scientific notation. If you want to avoid this you have to use a workaround such as the following minimal example:

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
x=np.arange(100,110)
y=np.arange(10)
fig, axes = plt.subplots(1, 2, sharey=True, squeeze=False)
axes = axes[0]
axes[0].plot(x,y)
axes[1].plot(x,y)
for ax in axes:
     ax.set_yscale('log')
     ax.yaxis.set_major_formatter(mpl.ticker.ScalarFormatter())
plt.show() 

Output

This works fine under all cases and has been shown as a solution as under many questions such as Example 1 Example 2

I have used this solution fine before. However there is another issue that I have not seen discussed. If you want to set a ax.ylim(...) this work around stops working

Consider the following example (note the new line ax.set_ylim(0,10)):

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
x=np.arange(100,110)
y=np.arange(10)
fig, axes = plt.subplots(1, 2, sharey=True, squeeze=False)
axes = axes[0]
axes[0].plot(x,y)
axes[1].plot(x,y)
for ax in axes:
    ax.set_ylim(0, 10)
    ax.set_yscale('log')
    ax.yaxis.set_major_formatter(mpl.ticker.ScalarFormatter())

plt.show()

Error

This is no longer the desired output.

Is there anyway to avoid this weird interaction?

akozi
  • 425
  • 1
  • 7
  • 20

1 Answers1

1

The problem is that you are currently using only major_formatter which only applies to the major ticks on your log scale. You need to additionally use minor_formatter in your second case because what you see as 2 x10^0, 3 x 10^0 etc are the minor tick-labels the formatting of which has to be treated separately.

for ax in axes:
    ax.set_ylim(0, 10)
    ax.set_yscale('log')
    ax.yaxis.set_major_formatter(mpl.ticker.ScalarFormatter())
    ax.yaxis.set_minor_formatter(mpl.ticker.ScalarFormatter()) # <---- Added

enter image description here

Sheldore
  • 37,862
  • 7
  • 57
  • 71