0

I have two subplots that share the same x-axis. I removed the xticklabels of the upper subplot, but the offset "1e7" remains visible. How can I hide it?

Here is the code I used :

import matplotlib.pyplot as plt
import numpy as np

plt.figure()
s1 = plt.subplot(2,1,1)
s1.plot(np.arange(0,1e8,1e7),np.arange(10))
s1.tick_params(axis="x", labelbottom=False)

s2 = plt.subplot(2,1,2, sharex=s1)
s2.plot(np.arange(0,1e8,1e7),np.arange(10))

I also tried s1.get_xaxis().get_major_formatter().set_useOffset(False), but it did nothing and I also tried s1.get_xaxis().get_major_formatter().set_powerlimits((-9,9)) but it impacted also the lower suplot.

j_4321
  • 15,431
  • 3
  • 34
  • 61
  • For hiding the offset: Did you try `axes.formatter.useoffset`already? Cf. [matplotlib remove axis label offset by default](http://stackoverflow.com/questions/24171064/matplotlib-remove-axis-label-offset-by-default) the answer of @Christoph – Dilettant Jul 05 '16 at 14:57
  • I tried `s1.get_xaxis().get_major_formatter().set_useOffset(False)`, but it did nothing and I also tried `s1.get_xaxis().get_major_formatter().set_powerlimits((-9,9))` but it impacted also the lower suplot. – j_4321 Jul 05 '16 at 15:14
  • Everyone loves these best previous attempts (and why they did not work) inside the questions, as comments are somewhat inaccessible ... – Dilettant Jul 05 '16 at 15:16

2 Answers2

1

I finally find the answer on https://github.com/matplotlib/matplotlib/issues/4445. I need to add the following line to my code :

plt.setp(s1.get_xaxis().get_offset_text(), visible=False)
j_4321
  • 15,431
  • 3
  • 34
  • 61
1

An alternative is to use plt.subplots to create the subplots, and use sharex=True as an option there. This automatically turns off all ticklabels and the offset_text from the top subplot. From the docs:

sharex : string or bool

If True, the X axis will be shared amongst all subplots. If True and you have multiple rows, the x tick labels on all but the last row of plots will have visible set to False

import matplotlib.pyplot as plt
import numpy as np

fig, (s1, s2) = plt.subplots(2, 1, sharex=True)
s1.plot(np.arange(0,1e8,1e7),np.arange(10))

s2.plot(np.arange(0,1e8,1e7),np.arange(10))

plt.show()

enter image description here

tmdavison
  • 64,360
  • 12
  • 187
  • 165