Grouped boxplot with seaborn
<p>I have following data in python panda DataFrame. I would like to have grouped box plot similar to one in <a href="https://stanford.edu/~mwaskom/software/seaborn/examples/grouped_boxplot.html" rel="nofollow">https://stanford.edu/~mwaskom/software/seaborn/examples/grouped_boxplot.html</a></p>

<p>For each id, I would like to have two box plots plotted side by side. How do i achieve this. I tried plotting it with the seaborn package but without any success. </p>

<pre><code>id               predicted                              real
1            [10, 10, 10]                      [16, 18, 20]   
2            [12, 12, 15]              [15, 17, 19, 21, 23]
3           [20, 5, 4, 4]                          [29, 32]
4    [25, 25, 25, 24, 21]  [21, 24, 25, 26, 28, 29, 30, 33]
5        [20, 20, 20, 21]  [21, 22, 24, 26, 28, 30, 31, 32]
6           [8, 3, 3, 14]                          [25, 27]
7  [1, 4, 4, 4, 5, 6, 10]                      [69, 71, 72]
8        [11, 11, 11, 11]              [19, 21, 22, 23, 24]
9            [7, 6, 9, 9]                  [19, 26, 27, 28]
10    [30, 30, 30, 30, 30]                          [38, 39]
</code></pre>

<p>Notice the table structure in the example you are looking at</p>

<pre><code>import seaborn as sns

tips = sns.load_dataset("tips")
sns.boxplot(x="day", y="total_bill", hue="sex", data=tips, palette="PRGn")
sns.despine(offset=10, trim=True)
</code></pre>

<p><a href="http://i.stack.imgur.com/Y1DJO.png" rel="nofollow"><img src="http://i.stack.imgur.com/Y1DJO.png" alt="enter image description here"></a></p>

<pre><code>tips.head()
</code></pre>

<p><a href="http://i.stack.imgur.com/DtcGp.png" rel="nofollow"><img src="http://i.stack.imgur.com/DtcGp.png" alt="enter image description here"></a></p>

<hr>

<p>Our goal is to set your table up like this</p>

<h3>Setup</h3>

<pre><code>from StringIO import StringIO
import pandas as pd

text = """id               predicted                              real
1            [10, 10, 10]                      [16, 18, 20]   
2            [12, 12, 15]              [15, 17, 19, 21, 23]
3           [20, 5, 4, 4]                          [29, 32]
4    [25, 25, 25, 24, 21]  [21, 24, 25, 26, 28, 29, 30, 33]
5        [20, 20, 20, 21]  [21, 22, 24, 26, 28, 30, 31, 32]
6           [8, 3, 3, 14]                          [25, 27]
7  [1, 4, 4, 4, 5, 6, 10]                      [69, 71, 72]
8        [11, 11, 11, 11]              [19, 21, 22, 23, 24]
9            [7, 6, 9, 9]                  [19, 26, 27, 28]
10    [30, 30, 30, 30, 30]                          [38, 39]"""

df = pd.read_csv(StringIO(text), sep='\s{2,}', engine='python', index_col=0)

df = df.stack().str.strip('[]') \
       .str.split(', ').unstack()
df
</code></pre>

<p><a href="http://i.stack.imgur.com/Zz4ev.png" rel="nofollow"><img src="http://i.stack.imgur.com/Zz4ev.png" alt="enter image description here"></a></p>

<hr>

<h3>Get your table in the correct format</h3>

<pre><code>df1 = df.stack().apply(pd.Series).stack().astype(int) \
          .rename_axis(['id', 'reality', None]) \
          .rename('value').reset_index(['id', 'reality']) \
          .reset_index(drop=True)

df1.head()
</code></pre>

<p><a href="http://i.stack.imgur.com/ed8Lr.png" rel="nofollow"><img src="http://i.stack.imgur.com/ed8Lr.png" alt="enter image description here"></a></p>

<pre><code>sns.boxplot(x='id', y='value', hue='reality', data=df1, palette='PRGn')
sns.despine(offset=10, trim=True)
</code></pre>

<p><a href="http://i.stack.imgur.com/cJbQW.png" rel="nofollow"><img src="http://i.stack.imgur.com/cJbQW.png" alt="enter image description here"></a></p>

python pandas seaborn