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.

3
  • For hiding the offset: Did you try axes.formatter.useoffsetalready? Cf. matplotlib remove axis label offset by default the answer of @Christoph Commented Jul 5, 2016 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. Commented Jul 5, 2016 at 15:14
  • Everyone loves these best previous attempts (and why they did not work) inside the questions, as comments are somewhat inaccessible ... Commented Jul 5, 2016 at 15:16

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.