matplotlib:y 轴格式拒绝科学化

问题描述 投票:0回答:1

我尝试使用(https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.ticklabel_format.html)中描述的Axes.ticklabel_format函数将matplotlib绘图轴标签的格式更改为科学格式。使用以下玩具代码

import numpy as np
import matplotlib.pyplot as plt

  # Data...
T     = np.arange(0, 101, 0.01)

R_e   = 1.68799673810490
R     = np.sin(T) + R_e

  # Plotting figure     
fig, ax = plt.subplots(1, 1)
ax.plot(T, R)

  # Attempting to set scientific format for both axes
ax.ticklabel_format(axis = 'both', style = 'scientific', scilimits = (0, 0))

  # Save figure
plt.savefig('Test.png', format = 'png')

我预计它会同时改变 x 轴和 y 轴。它仅更改 x 轴。 Test plot 任何帮助将不胜感激!

matplotlib
1个回答
0
投票

您可以手动或使用

matplotlib.ticker
:

设置这样的偏移量
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker


T     = np.arange(0, 101, 0.01)

R_e   = 1.68799673810490
R     = np.sin(T) + R_e

fig, ax = plt.subplots(1, 1)
ax.plot(T, R)

formatter = ticker.ScalarFormatter(useOffset=True, useMathText=True)
formatter.set_scientific(True)
formatter.set_powerlimits((1, 1))  # defines when to apply scientific notation

ax.xaxis.set_major_formatter(formatter)
ax.yaxis.set_major_formatter(formatter)

plt.show()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.