我想把y轴偏移的格式设置成非科学符号,但找不到设置。其他问题及其解决方案描述了如何完全删除偏移量,或者将y记号设置为科学/简单表示法;我还没有找到设置偏移量本身表示法的答案。
我已经试过使用这两个选项,但我认为它们是针对Y记号的,而不是偏移:
ax.ticklabel_format(axis='y', style='plain', useOffset=6378.1)
和
ax.get_yaxis().get_major_formatter().set_scientific(False)
所以,实际结果是
+6.3781e3
,当我想要+6378.1
时有办法吗?
编辑:添加示例代码和图:
#!/usr/bin/env python
from matplotlib import pyplot as plt
from matplotlib import ticker
plt.rcParams['font.family'] = 'monospace'
import random
Date = range(10)
R = [6373.1+10*random.random() for i in range(10)]
fig, ax = plt.subplots(figsize=(9,6))
ax.plot(Date,R,'-D',zorder=2,markersize=3)
ax.ticklabel_format(axis='y', style='plain', useOffset=6378.1)
ax.set_ylabel('Mean R (km)',fontsize='small',labelpad=1)
plt.show()
最佳答案
这样做的一种方法是禁用偏移文本本身,并在其中添加自定义的ax.text
,如下所示
from matplotlib import pyplot as plt
import random
plt.rcParams['font.family'] = 'monospace'
offset = 6378.1
Date = range(10)
R = [offset+10*random.random() for i in range(10)]
fig, ax = plt.subplots(figsize=(9,6))
ax.plot(Date,R,'-D',zorder=2,markersize=3)
ax.ticklabel_format(axis='y', style='plain', useOffset=offset)
ax.set_ylabel('Mean R (km)',fontsize='small',labelpad=1)
ax.yaxis.offsetText.set_visible(False)
ax.text(x = 0.0, y = 1.01, s = str(offset), transform=ax.transAxes)
plt.show()