matplotlib 中的轴数值偏移 [英] Axis numerical offset in matplotlib

查看:47
本文介绍了matplotlib 中的轴数值偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 matplotlib 绘制一些东西,它看起来像这样:

I'm plotting something with matplotlib and it looks like this:

我似乎无法弄清楚为什么 x 轴像现在这样偏移......它看起来像是在说,无论你从我这里读到什么,都将 2.398e9 添加到它的实际 x 值中".

I can't seem to figure out why the x-axis is offset like it is...It looks like it's saying, 'whatever you read from me, add 2.398e9 to it for the actual x value'.

这不是我想要的...我可以只用前4位数字代替吗?

This is not quite what I want...Can I make it take only the first 4 digits, instead?

这是代表频率,所以我想看看这样的东西:

This is representing frequency, so I'd like to see something that reads:

2000 或 2400 或 2800....我可以在轴标题中添加 'MHz' 部分...但是,一目了然.

2000 or 2400 or 2800....I can add the 'MHz' part in the axis title...But, this is unreadable at a glance.

这样做是因为它试图决定如何截断长数据吗?

Is this doing this because it's trying to make decisions on how to truncate long data?

这是绘图的代码:

plt.title(file_name +' at frequency '+ freq + 'MHz')
plt.xlabel('Frequency')
plt.ylabel('Conducted Power (dBm)')
plt.grid(True)
plt.plot(data['x'],data['y'])
#plt.axis([min(data['x']),max(data['x']),min(data['y'],max(data['y']))])
plt.savefig(file_name+'_'+freq)
print('plot written!')
#plt.show()
plt.close('all')

推荐答案

您需要从 matplotlib.ticker import 某些格式化程序.这是完整的股票代码文档

You need to import certain formatters from matplotlib.ticker. Here is the full documentation to ticker

from matplotlib.ticker import ScalarFormatter, FormatStrFormatter
ax.xaxis.set_major_formatter(FormatStrFormatter('%.0f'))

同样地将其设置为绘图后,您应该能够看到 +2.398e9 消失.

Once you have set this to your plot likewise, you should be able to see the +2.398e9 disappear.

一般来说,为了避免使用科学记数法,请使用以下内容:

In general to avoid scientific notation, use the following:

ax.get_xaxis().get_major_formatter().set_scientific(False)

这篇关于matplotlib 中的轴数值偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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