擅长:python、mysql、java
<p><a href="https://stackoverflow.com/questions/49781927/normalizing-a-histogram-with-matplotlib#answer-49782401">tel's answer is great!</a>我只想提供一种替代方法,用更少的行来给你想要的柱状图。关键思想是在matplotlib <code>hist</code>函数中使用<code>weights</code>参数来规范化计数。您可以用以下三行代码替换<code>sns.distplot(luminance.flatten(), kde_kws={"cumulative": True})</code>:</p>
<pre><code>lf = luminance.flatten()
sns.kdeplot(lf, cumulative=True)
sns.distplot(lf, kde=False,
hist_kws={'weights': numpy.full(len(lf), 1/len(lf))})
</code></pre>
<p><a href="https://i.stack.imgur.com/9T1vS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9T1vS.png" alt="enter image description here"/></a></p>
<p>如果您想在第二个y轴上查看直方图(更直观),请将<code>ax=bottom_left.twinx()</code>添加到<code>sns.distplot</code>:</p>
<p><a href="https://i.stack.imgur.com/jX97q.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jX97q.png" alt="enter image description here"/></a></p>