python直方图y轴显示占比,而不是绝对数值

  统计/机器学习 数据可视化 Python    浏览次数:25990        分享
3

python里面(pyplot)怎么画直方图可以让y轴显示百分比,而不是绝对的数值统计?

我现在是直接用ply.hist,得到的是长下面这样的

我希望y轴显示的是百分比,比重之类。

谢谢!


 

小菜   2017-06-11 10:37



   1个回答 
5


import matplotlib
from numpy.random import randn
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
# 把y轴转化为百分比。
def to_percent(y, position):
    return str(100 * y) + '%'
# 直方图的数据集
x = randn(500)
# 设置直方图分组数量bins=60.
plt.hist(x, bins=60, weights= [1./ len(x)] * len(x))
formatter = FuncFormatter(to_percent)
plt.gca().yaxis.set_major_formatter(formatter)
plt.show()




SofaSofa数据科学社区DS面试题库 DS面经

黄前志   2017-07-19 10:48



  相关讨论

plt.scatter plot怎么让不同的类别有不同的颜色

matplotlib.pyplot做折线图的时候,显示为虚线,或者点划线?

pyplot的散点图怎么画空心的点?

如何用matplotlib在3D空间呈现x^2+y^2?

matplotlib怎么把图例放到图外面

怎么在matplotlib的图中加入箭头标志注释?

matplotlib如何设置画图格式,使得可视化效果更好

plt.scatter中c参数和cmap参数的区别是什么?

%matplotlib inline什么意思?

matplotlib.pyplot环境下给曲线下方的面积涂色?

  随便看看

不用洛必达法则证明sin x比上x的极限是1

多个独立同分布的均匀随机变量的最小值的期望是多少?

怎么在matplotlib.pyplot的plot上加上文字?

关于方差膨胀因子(VIF)的问题

xgboost怎么调参?