Matplotlib 直方图,频率以千为单位
我在用matplotlib画一个直方图,数据大概有260,000个值。
问题是,直方图的频率轴(y轴)上的数字太大了,比如会出现100,000这样的数字。我其实想把y轴的标签改成以千为单位,也就是说,像这样:
100000
75000
50000
25000
0
我希望能变成这样:
100
75
50
25
0
然后我可以把y轴的标题改成“频率(千)”——这样看起来就简单多了。有没有人知道怎么做到这一点?
2 个回答
0
在数据输入之前,自己先把数值转换一下就行。在numpy中,你可以直接用 array/1000
来代替 array
。
15
使用 matplotlib.ticker.FuncFormatter:
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np
mu, sigma = 100, 15
x = mu + sigma * np.random.randn(1000000)
fig, ax = plt.subplots()
n, bins, patches = ax.hist(x, 50, facecolor='green', alpha=0.75)
ax.yaxis.set_major_formatter(ticker.FuncFormatter(
lambda y, pos: '%.0f' % (y * 1e-3)))
ax.set_ylabel('Frequency (000s)')
plt.show()