<ul>
<li>截至<a href="https://seaborn.pydata.org/whatsnew.html#v0-11-2-august-2021" rel="nofollow noreferrer">^{<cd1>}</a>
<ul>
<li><a href="https://seaborn.pydata.org/generated/seaborn.distplot.html" rel="nofollow noreferrer">^{<cd2>}</a>替换为图形级别<a href="https://seaborn.pydata.org/generated/seaborn.displot.html#seaborn.displot" rel="nofollow noreferrer">^{<cd3>}</a>和轴级别<a href="https://seaborn.pydata.org/generated/seaborn.histplot.html#seaborn.histplot" rel="nofollow noreferrer">^{<cd4>}</a>,它们具有<code>stat</code>参数。使用<code>stat='percent'</code></李>
</ul>
</li>
<li>对于这两种类型的图,使用<code>common_bins</code>和<code>common_norm</code>进行试验。
<ul>
<li>例如,<code>common_norm=True</code>将显示占整个人口的百分比,而<code>False</code>将显示相对于群体的百分比</李>
</ul>
</li>
<li>本<a href="https://stackoverflow.com/a/68851142/7758804">answer</a>中所示的实现展示了如何添加注释</李>
</ul>
<pre class="lang-py prettyprint-override"><code>import seaborn as sns
import matplotlib.pyplot as ply
# data
data = sns.load_dataset('titanic')
</code></pre>
<h2>图形级别</h2>
<pre class="lang-py prettyprint-override"><code>p = sns.displot(data=data, x='age', stat='percent', hue='sex', height=3)
plt.show()
</code></pre>
<p><a href="https://i.stack.imgur.com/ChSyT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ChSyT.png" alt="enter image description here"/></a></p>
<pre class="lang-py prettyprint-override"><code>p = sns.displot(data=data, x='age', stat='percent', col='sex', height=3)
plt.show()
</code></pre>
<p><a href="https://i.stack.imgur.com/lcBI9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lcBI9.png" alt="enter image description here"/></a></p>
<ul>
<li>在<code>labels</code>中使用的类型批注(<code>:=</code>)需要<code>python >= 3.8</code>。这可以通过<code>for-loop</code>实现,而无需使用<code>:=</code></李>
</ul>
<pre class="lang-py prettyprint-override"><code>fg = sns.displot(data=data, x='age', stat='percent', col='sex', height=3.5, aspect=1.25)
for ax in fg.axes.ravel():
# add annotations
for c in ax.containers:
# custom label calculates percent and add an empty string so 0 value bars don't have a number
labels = [f'{w:0.1f}%' if (w := v.get_height()) > 0 else '' for v in c]
ax.bar_label(c, labels=labels, label_type='edge', fontsize=8, rotation=90, padding=2)
ax.margins(y=0.2)
plt.show()
</code></pre>
<p><a href="https://i.stack.imgur.com/i54vm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/i54vm.png" alt="enter image description here"/></a></p>
<h2>轴水平</h2>
<pre class="lang-py prettyprint-override"><code>fig = plt.figure(figsize=(4, 3))
p = sns.histplot(data=data, x='age', stat='percent', hue='sex')
plt.show()
</code></pre>
<p><a href="https://i.stack.imgur.com/tWgRb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tWgRb.png" alt="enter image description here"/></a></p>