在 matplotlib 中使用科学记数法时定位刻度标签的指数 [英] Positioning the exponent of tick labels when using scientific notation in matplotlib

查看:56
本文介绍了在 matplotlib 中使用科学记数法时定位刻度标签的指数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在使用科学记数法时更改指数在轴上的位置的方法.我已经被这个问题卡住了几次.我已经知道默认格式化程序是 ScalarFormatter 但它无法以某种方式访问​​指数.

有一个,您可以使用该类的方法,例如 get_text():

print([s.get_text() for s in ax2.get_xmajorticklabels()])

但是,结果是

<9 个 Text xticklabel 对象的列表>[u'',u'',u'',u'',u'',u'',u'',u'',u'']<10 个文本 xticklabel 对象的列表>[u'',u'',u'',u'',u'',u'',u'',u'',u'',u'']

运行fig.tight_layout()后,现在可以枚举这些Text xticklabel对象的输出:

 < 9个Text xticklabel对象的列表>[(0.0,0),(20.0,0),(40.0,0),(60.0,0),(80.0,0),(100.0,0),(120.0,0),(140.0,0),(160.0, 0)]< 10个Text xticklabel对象的列表>[(0.0, 0), (2.0000000000000002e-05, 0), (4.0000000000000003e-05, 0), (6.0000000000000008e-05, 0), 0,00.00) (0.00.0), 00.00.00), 00.00.000.00012000000000000002, 0), (0.00014000000000000001, 0), (0.00016000000000000001, 0), (0, 0)]

对于像 -7 这样的指数,实际上两个列表中的对象数量相同.

我找到的最接近的标签定位方法在

I am looking for a way to change the position of the exponent on an axis when using scientific notation. I got stuck at this problem already a few times. I know already that the default formatter is the ScalarFormatter but it has no option to access the exponent somehow.

There is a workaround but I don't like it, since it also manipulates the existing ticklabels. So far I found out, that the list returned by the axes' get_xmajorticklabels() method contains one text object more if scientific notation is used. For example:

import matplotlib.pyplot as plt
import numpy as np

fig= plt.figure(figsize = plt.figaspect(0.5))
ax1= fig.add_subplot(121)
ax1.plot(np.arange(146), np.random.rand(146))
print(ax1.get_xmajorticklabels())

ax2= fig.add_subplot(122)
ax2.plot(np.arange(146)*1e-6, np.random.rand(146))
print(ax2.get_xmajorticklabels())

The prints give: <a list of 9 Text xticklabel objects> and <a list of 10 Text xticklabel objects> So I thought the additional list item could be the text object for the exponent. But it's empty when I print the text.

Is there any way to access this exponent as a text object? Then it should be possible to set its position, isn't it?

解决方案

To access the list of Text objects you can use a method of that class, e.g. get_text():

print([s.get_text() for s in ax2.get_xmajorticklabels()])

However, the result of that is

<a list of 9 Text xticklabel objects>
[u'', u'', u'', u'', u'', u'', u'', u'', u'']
<a list of 10 Text xticklabel objects>
[u'', u'', u'', u'', u'', u'', u'', u'', u'', u'']

After running fig.tight_layout(), the output of these Text xticklabel objects can now be enumerated:

<a list of 9 Text xticklabel objects>
[(0.0, 0), (20.0, 0), (40.0, 0), (60.0, 0), (80.0, 0), (100.0, 0), (120.0, 0), (140.0, 0), (160.0, 0)]

<a list of 10 Text xticklabel objects>
[(0.0, 0), (2.0000000000000002e-05, 0), (4.0000000000000003e-05, 0), (6.0000000000000008e-05, 0), (8.0000000000000007e-05, 0), (0.0001, 0), (0.00012000000000000002, 0), (0.00014000000000000001, 0), (0.00016000000000000001, 0), (0, 0)]

For an exponent like -7, there is actually the same amount of objects in both lists.

The closest method I've found for positioning the label is detailed here by Scott. Sadly, it will only work horizontally for the x-axis and vertically for the y-axis, so you can't really arbitrarily position the label on the graph.

ax2.get_xaxis().get_offset_text().set_position((0.5,0))

这篇关于在 matplotlib 中使用科学记数法时定位刻度标签的指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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