python - 以百万为单位设置 y 轴
问题描述
我对这个情节有疑问:
[![在此处输入图像描述][1]][1]
y 轴是单位,但我需要它们以百万为单位:
[![在此处输入图像描述][2]][2]
你知道实现这一目标的方法吗?提前致谢。
解决方案
您可以像这样使用自定义 FuncFormatter:
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)
或者您甚至可以用以下函数替换数百万以支持所有量级:
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])
推荐阅读
- hangfire - Hangfire 仅在访问 hangfire 仪表板站点时有效,在后台无法继续工作
- scala - 在 SPARK 数据框中,我想 groupBy,然后 orderBY 并且它们连接另一列的行
- c++ - Why the C++ code couldn't display bitmap?(code from textbook)
- javascript - 用于更大传输的 Nodejs 套接字发射
- java - 如何在editText中放置一个按钮
- ruby - Stripe 在 macbook localhost 上失败,但在 iMac localhost 上工作
- python - 从请求正文 django 访问数据
- android - VectorDrawable 以像素显示
- javascript - Query.formatError 的“where 子句”中的未知列“users.email”
- c# - 实体查询中的 If Else 语句