简体   繁体   中英

Matplotlib: Remove scientific notation in subplot

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:

在此输入图像描述

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.

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([])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM