本文介绍了将y轴设置为百万的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对这个情节有疑问:
[![在此处输入图片描述] [1]] [1]
[![enter image description here][1]][1]
y轴为单位,但我需要以百万为单位:
The y-axis is in unit but I need them to be in millions as such:
[![在此处输入图片描述] [2]] [2]
[![enter image description here][2]][2]
您知道实现此目标的方法吗?预先感谢.
Do you know a method to achieve this? Thanks in advance.
推荐答案
您可以像这样使用自定义FuncFormatter:
You can use a custom FuncFormatter like this:
from matplotlib.ticker import FuncFormatter
import matplotlib.pyplot as plt
def millions(x, pos):
'The two args are the value and tick position'
return '%1.1fM' % (x * 1e-6)
formatter = FuncFormatter(millions)
fig, ax = plt.subplots()
ax.yaxis.set_major_formatter(formatter)
或者您甚至可以用以下功能替换数百万个以支持所有大小:
Or you can even replace millions with the following functions to support all magnitudes:
def human_format(num, pos):
magnitude = 0
while abs(num) >= 1000:
magnitude += 1
num /= 1000.0
# add more suffixes if you need them
return '%.2f%s' % (num, ['', 'K', 'M', 'G', 'T', 'P'][magnitude])
这篇关于将y轴设置为百万的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!