简体   繁体   English

Matplotlib:删除子图中的科学记数法

[英]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. 连续的每个图共享相同的y轴,同一列中的图共享相同的x轴。 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. 虽然我可以使用ticklabel_format删除刻度ticklabel_format ,但这并不会删除轴上的指数。 With ax1.xaxis.set_visible(False) , the 1e5 at the x-axis is removed but also the tick marks. 使用ax1.xaxis.set_visible(False)x-axis1e5将被删除,但也会删除刻度线。 How can I remove only the 1eX at the subplots that share the axis with another one while keeping the tick marks? 如何在保留刻度线的同时仅删除与另一个轴共用轴的子图上的1eX For example, how do I get rid of the 1e5 and 1e2 in subplot 2? 例如,如何摆脱子图2中的1e51e2

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 ):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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