Search code examples
pythonnumpytime-seriesseabornviolin-plot

Seaborn violin plot over time given numpy ndarray


I have a distribution that changes over time for which I would like to plot a violin plot for each time step side-by-side using seaborn. My initial attempt failed as violinplot cannot handle a np.ndarray for the y argument:

import numpy as np
import seaborn as sns

time = np.arange(0, 10)
samples = np.random.randn(10, 200)

ax = sns.violinplot(x=time, y=samples)  # Exception: Data must be 1-dimensional

The seaborn documentation has an example for a vertical violinplot grouped by a categorical variable. However, it uses a DataFrame in long format.

Do I need to convert my time series into a DataFrame as well? If so, how do I achieve this?


Solution

  • A closer look at the documentation made me realize that omitting the x and y argument altogether leads to the data argument being interpreted in wide-form:

    import numpy as np
    import seaborn as sns
    import matplotlib.pyplot as plt
    
    
    samples = np.random.randn(20, 10)
    ax = sns.violinplot(data=samples)
    plt.show()
    

    Violin plot over time