Reputation: 16169
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.
Upvotes: 0
Views: 2408
Reputation: 69116
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()
Upvotes: 1
Reputation: 16169
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)
Upvotes: 1