将Matplotlib偏移符号从科学更改为普通 [英] Change matplotlib offset notation from scientific to plain

查看:74
本文介绍了将Matplotlib偏移符号从科学更改为普通的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将绘图中 y 轴偏移的格式设置为非科学记数法,但我找不到执行此操作的设置.其他问题及其解决方案描述了如何完全删除偏移量,或将 y-ticks 设置为科学/普通符号;我还没有找到设置偏移量的表示法本身的答案.

I want to set the formatting of the y-axis offset in my plot to non-scientific notation, but I can't find a setting to do this. Other questions and their solutions describe how to either remove the offset altogether, or set the y-ticks to scientific/plain notation; I haven't found an answer for setting the notation of the offset itself.

我已经尝试过使用这两个选项,但是我认为它们用于y刻度,而不是偏移量:

I've already tried using these two options, but I think they're meant for the y-ticks, not the offsets:

ax.ticklabel_format(axis='y', style='plain', useOffset=6378.1)

ax.get_yaxis().get_major_formatter().set_scientific(False)

所以,实际结果是+6.3781e3,当我想要+6378.1

So, the actual result is +6.3781e3, when I want +6378.1

有没有办法做到这一点?

Any way to do this?

添加示例代码和图:

#!/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 ,如下所示

A way to do this is to disable the offset text itself and add your custom ax.text there as follows

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

这篇关于将Matplotlib偏移符号从科学更改为普通的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆