<ul>
<li>我想你真的需要一个<a href="https://seaborn.pydata.org/generated/seaborn.barplot.html#seaborn.barplot" rel="nofollow noreferrer">barplot</a>作为这个数据</李>
<li><a href="https://seaborn.pydata.org/generated/seaborn.countplot.html" rel="nofollow noreferrer">countplot</a>使用条形图显示每个分类箱中的观察计数</李>
<li>需要将数据<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.stack.html" rel="nofollow noreferrer">stacked</a>转换为长格式</li>
</ul>
<pre class="lang-py prettyprint-override"><code>import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
btraits = {'Behavioural Traits': ['Communicaiton', 'Teamwork', 'Leadership', 'Negotiation', 'Agreeableness'],
'James': [0,-2,0,-2,-2],
'John': [2,0,0,2,-1],
'Gary': [0,-1,0,1,-3],
'Raymond': [3,-5,0,1,0]}
df = pd.DataFrame(btraits)
df.set_index('Behavioural Traits', inplace=True)
James John Gary Raymond
Behavioural Traits
Communicaiton 0 2 0 3
Teamwork -2 0 -1 -5
Leadership 0 0 0 0
Negotiation -2 2 1 1
Agreeableness -2 -1 -3 0
# stack the columns
dfs = df.stack().reset_index().rename(columns={'level_1': 'names', 0: 'values'})
Behavioural Traits names values
0 Communicaiton James 0
1 Communicaiton John 2
2 Communicaiton Gary 0
3 Communicaiton Raymond 3
4 Teamwork James -2
5 Teamwork John 0
6 Teamwork Gary -1
7 Teamwork Raymond -5
8 Leadership James 0
9 Leadership John 0
10 Leadership Gary 0
11 Leadership Raymond 0
12 Negotiation James -2
13 Negotiation John 2
14 Negotiation Gary 1
15 Negotiation Raymond 1
16 Agreeableness James -2
17 Agreeableness John -1
18 Agreeableness Gary -3
19 Agreeableness Raymond 0
</code></pre>
<h2>计数图</h2>
<pre class="lang-py prettyprint-override"><code>sns.countplot(data=dfs, x='names', hue='Behavioural Traits')
</code></pre>
<p><a href="https://i.stack.imgur.com/6GEJh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6GEJh.png" alt="enter image description here"/></a></p>
<h2>条形图</h2>
<pre class="lang-py prettyprint-override"><code>sns.barplot(x='names', y='values', hue='Behavioural Traits', data=dfs)
</code></pre>
<p><a href="https://i.stack.imgur.com/IYYUK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IYYUK.png" alt="enter image description here"/></a></p>