Matplotlib:删除子图中的科学计数法 [英] Matplotlib: Remove scientific notation in subplot
问题描述
我想创建一个包含四个子图的图形.行中的每个图共享相同的y
轴,同一列中的图共享相同的x
轴.在每个轴上,我都使用科学计数法.虽然可以使用ticklabel_format
删除刻度线的数量,但这并不能删除轴上的指数.使用ax1.xaxis.set_visible(False)
,将删除x-axis
处的1e5
,同时还会删除刻度线.如何在保留刻度线的情况下仅删除与另一个轴共享轴的子图上的1eX
?例如,如何摆脱子图2中的1e5
和1e2
?
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()
返回:
推荐答案
如果为每个轴添加以下行(以ax1
为例):
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.
这篇关于Matplotlib:删除子图中的科学计数法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!