Matplotlib-对数刻度,但需要非对数标签 [英] Matplotlib - logarithmic scale, but require non-logarithmic labels

查看:96
本文介绍了Matplotlib-对数刻度,但需要非对数标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何停止y轴在y轴上显示对数符号的标签?

How can I stop the y-axis displaying a logarithmic notation label on the y-axis?

我对数刻度很满意,但想显示绝对值,例如Y轴上的[500,1500,4500,11000,110000].我不想明确标记每个刻度,因为标签将来可能会更改(我尝试了不同的格式化程序,但尚未成功使它们工作).下面的示例代码.

I'm happy with the logarithmic scale, but want to display the absolute values, e.g. [500, 1500, 4500, 11000, 110000] on the Y-axis. I don't want to explicitly label each tick as the labels may change in the future (I've tried out the different formatters but haven't successfully gotten them to work). Sample code below.

谢谢

-collern2

-collern2

import matplotlib.pyplot as plt
import numpy as np

a = np.array([500, 1500, 4500, 11000, 110000])
b = np.array([10, 20, 30, 40, 50])

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.set_yscale('log')

plt.plot(b, a)
plt.grid(True)
plt.show()

推荐答案

如果我理解正确,

ax.set_yscale('log')

任何

ax.yaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter())
ax.yaxis.set_major_formatter(matplotlib.ticker.FormatStrFormatter('%d'))
ax.yaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(lambda x, pos: str(int(round(x)))))

应该工作.如果刻度标签的位置位于4.99之类的地方,则'%d'将有问题,但是您明白了.

should work. '%d' will have problems if the tick labels locations wind up being at places like 4.99, but you get the idea.

请注意,根据轴的限制,您可能需要对次要格式化程序set_minor_formatter执行相同的操作.

Note that you may need to do the same with the minor formatter, set_minor_formatter, depending on the limits of the axes.

这篇关于Matplotlib-对数刻度,但需要非对数标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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