<p>通过使用<a href="https://seaborn.pydata.org/generated/seaborn.catplot.html" rel="nofollow noreferrer">^{<cd1>}</a>可以避免循环,这允许您在一个<code>FacetGrid</code>上绘制多个<code>countplot</code></p>
<p>这在Python2.7.18中进行了测试(尽管它在Python3中仍然有效):</p>
<ol>
<li><p><a href="https://pandas.pydata.org/docs/reference/api/pandas.melt.html" rel="nofollow noreferrer">^{<cd4>}</a>将<code>TS</code>数据导入<a href="https://seaborn.pydata.org/tutorial/data_structure.html#long-form-data" rel="nofollow noreferrer">long form</a>:</p>
<pre><code>melted = df.melt(id_vars=[], value_vars=['TS1','TS2','TS3','TS4','TS5'],
var_name='testing', value_name='outcome')
# testing outcome
# 0 TS1 NaN
# 1 TS1 NaN
# 2 TS1 118
# 3 TS1 NaN
# .. ... ...
# 88 TS5 NaN
# 89 TS5 126
</code></pre>
</li>
<li><p>通过<a href="https://seaborn.pydata.org/generated/seaborn.catplot.html" rel="nofollow noreferrer">^{<cd7>}</a>绘制<code>melted</code>数据:</p>
<pre><code>g = sns.catplot(kind='count', x='outcome', col='testing',
col_wrap=3, order=OUTCOMES.categories,
data=melted, palette='GnBu_r')
g.set_xticklabels(rotation=90)
</code></pre>
<p><a href="https://i.stack.imgur.com/jUbQx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jUbQx.png" alt="catplot output"/></a></p>
</li>
</ol>
<hr/>
<p>版本:</p>
<pre><code>>>> sys.version
# 2.7.18 (default, Mar 15 2021, 14:29:03) \n[GCC 10.2.0]
>>> pandas.__version__
# 0.24.2
>>> matplotlib.__version__
# 2.2.5
>>> seaborn.__version__
# 0.9.1
</code></pre>