2

I'd like to make a violin plot from a dictionary. Here is an example of what my dictionary looks like, though my actual one has many more patients and many more values.

paired_patients={'Patient_1': {'n':[1, nan, 3, 4], 't': [5,6,7,8]},
                 'Patient_2': {'n':[9,10,11,12], 't':[14,nan,16,17]},
                 'Patient_3': {'n':[1.5,nan,3.5,4.5], 't':[5.5,6.5,7.5,8.5]}}

For each patient, I'd like there to be a set of two violin plots side-by-side, one 'n' and one for 't'. I'd like all six violin plots to be on the same graph, sharing the y axis.

I am trying to use matplotlib violinplot, but I am not sure how to enter my dictionary in the 'dataset' option, nor how to group the 'n' and 't' by patient.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Emerson
  • 125
  • 8

1 Answers1

6

Answer

I suggest to save your data in a pandas.DataFrame.
First of all, I loop over patients to save the data in the dataframe:

df = pd.DataFrame(columns = ['Patient', 'n', 't'])

for key, value in paired_patients.items():
    patient_df = pd.DataFrame({'Patient': [key]*len(value['n']),
                               'n': value['n'],
                               't': value['t']})
    df = df.append(patient_df, ignore_index = True)

So I get:

      Patient     n    t
0   Patient_1   1.0    5
1   Patient_1   NaN    6
2   Patient_1   3.0    7
3   Patient_1   4.0    8
4   Patient_2   9.0   14
5   Patient_2  10.0  NaN
6   Patient_2  11.0   16
7   Patient_2  12.0   17
8   Patient_3   1.5  5.5
9   Patient_3   NaN  6.5
10  Patient_3   3.5  7.5
11  Patient_3   4.5  8.5

Then I need to stack 'n' and 't' columns through pd.melt:

df = pd.melt(frame = df,
             id_vars = 'Patient',
             value_vars = ['n', 't'],
             var_name = 'type',
             value_name = 'value')

In this way the dataframe in reshaped as follows:

      Patient type value
0   Patient_1    n     1
1   Patient_1    n   NaN
2   Patient_1    n     3
3   Patient_1    n     4
4   Patient_2    n     9
5   Patient_2    n    10
6   Patient_2    n    11
7   Patient_2    n    12
8   Patient_3    n   1.5
9   Patient_3    n   NaN
10  Patient_3    n   3.5
11  Patient_3    n   4.5
12  Patient_1    t     5
13  Patient_1    t     6
14  Patient_1    t     7
15  Patient_1    t     8
16  Patient_2    t    14
17  Patient_2    t   NaN
18  Patient_2    t    16
19  Patient_2    t    17
20  Patient_3    t   5.5
21  Patient_3    t   6.5
22  Patient_3    t   7.5
23  Patient_3    t   8.5

Finally you may need to convert 'value' column type to float:

df['value'] = df['value'].astype(float)

Now it is possible to plot these data with the seaborn.violinplot:

fig, ax = plt.subplots()

sns.violinplot(ax = ax,
               data = df,
               x = 'Patient',
               y = 'value',
               hue = 'type',
               split = True)

plt.show()

Complete Code

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
from math import nan

paired_patients = {'Patient_1': {'n': [1, nan, 3, 4], 't': [5, 6, 7, 8]},
                   'Patient_2': {'n': [9, 10, 11, 12], 't': [14, nan, 16, 17]},
                   'Patient_3': {'n': [1.5, nan, 3.5, 4.5], 't': [5.5, 6.5, 7.5, 8.5]}}

df = pd.DataFrame(columns = ['Patient', 'n', 't'])

for key, value in paired_patients.items():
    patient_df = pd.DataFrame({'Patient': [key]*len(value['n']),
                               'n': value['n'],
                               't': value['t']})
    df = df.append(patient_df, ignore_index = True)

df = pd.melt(frame = df,
             id_vars = 'Patient',
             value_vars = ['n', 't'],
             var_name = 'type',
             value_name = 'value')

df['value'] = df['value'].astype(float)

fig, ax = plt.subplots()

sns.violinplot(ax = ax,
               data = df,
               x = 'Patient',
               y = 'value',
               hue = 'type',
               split = True)

plt.show()

Plot

enter image description here


Note

If you have many patients, you will have too many data along x axis, so I suggest you to set split = True in order to save some space.
Otherwise, if you set split = False, you will get:

enter image description here

Zephyr
  • 11,891
  • 53
  • 45
  • 80