7

I want to create a figure with four subplots. Each plot in a row shares the same y axis and the plots in the same column share the same x axis. On each axis I use the scientific notation. While I can remove the numbers of the ticks with ticklabel_format, this does not remove the exponent at the axis. With ax1.xaxis.set_visible(False), the 1e5 at the x-axis is removed but also the tick marks. How can I remove only the 1eX at the subplots that share the axis with another one while keeping the tick marks? For example, how do I get rid of the 1e5 and 1e2 in subplot 2?

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()

ax3 = fig.add_subplot(223)
ax1 = fig.add_subplot(221, sharex = ax3)
ax4 = fig.add_subplot(224, sharey = ax3)
ax2 = fig.add_subplot(222, sharex = ax4, sharey = ax1)

#First plot
x = np.arange(0, 10**5, 100)
y = x
ax1.plot(x,y)
ax1.set_title('Subplot 1')

# Third plot
y = -x
ax3.plot(x,y)
ax3.set_title('Subplot 3')

#Second plot
x = np.arange(0, 100)
y = 10**3 * x + 100
ax2.plot(x,y)
ax2.set_title('Subplot 2')

#Fourth plot
y = -10**3 * x - 100
ax4.plot(x,y)
ax4.set_title('Subplot 4')

ax4.ticklabel_format(style = 'sci', axis='x', scilimits=(0,0))
ax3.ticklabel_format(style = 'sci', axis='x', scilimits=(0,0))
ax1.ticklabel_format(style = 'sci', axis='y', scilimits=(0,0))
ax3.ticklabel_format(style = 'sci', axis='y', scilimits=(0,0))

plt.setp(ax1.get_xticklabels(), visible=False)
plt.setp(ax2.get_xticklabels(), visible=False)
plt.setp(ax2.get_yticklabels(), visible=False)
plt.setp(ax4.get_yticklabels(), visible=False)

plt.show()

returns:

enter image description here

2 Answers 2

9

If you add these lines for each of the axes (ax1 as an example):

ax1.xaxis.get_offset_text().set_visible(False)
ax1.yaxis.get_offset_text().set_visible(False)

This will remove scientific notation text from both axis.

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

Comments

1

Modify the first part to this:

ax3 = fig.add_subplot(223)
ax1 = fig.add_subplot(221, sharex = ax3)
ax4 = fig.add_subplot(224)
ax2 = fig.add_subplot(222, sharex = ax4)

Then add this:

ax2.axes.xaxis.set_ticklabels([])
ax2.axes.yaxis.set_ticklabels([])
ax4.axes.yaxis.set_ticklabels([])
ax4.axes.yaxis.set_ticklabels([])

1 Comment

The last two lines seem to be redundant. The x axis of subplot 1 has still the 1e5 and subplot 4 is missing the ticklabels of the x axis.

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.