python: box plot(or violin plot) from multidimensional data

I'm new to python and have no backgrounds at all. The thing is I'd like to create box or violin plot from multidimensional data set such as, 20 groups having 5 variables. Each group has 20~100 elements (raw data before averaging) Which function can help me parsing this? In matlab I think cell array would be helpful but I guess there is more brilliant way to parse this data in python. Please help..

1 answer

  • answered 2018-04-17 04:53 distant1219

    There is a very useful python package called Matplotlib you can use to draw box plot or violin plot. Below is a example of box plot vs violin plot.

    import matplotlib.pyplot as plt
    import numpy as np
    
    fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(9, 4))
    
    # Fixing random state for reproducibility
    np.random.seed(19680801)
    
    
    # generate some random test data
    all_data = [np.random.normal(0, std, 100) for std in range(6, 10)]
    
    # plot violin plot
    axes[0].violinplot(all_data,
                      showmeans=False,
                      showmedians=True)
    axes[0].set_title('Violin plot')
    
    # plot box plot
    axes[1].boxplot(all_data)
    axes[1].set_title('Box plot')
    
    # adding horizontal grid lines
    for ax in axes:
        ax.yaxis.grid(True)
        ax.set_xticks([y + 1 for y in range(len(all_data))])
        ax.set_xlabel('Four separate samples')
        ax.set_ylabel('Observed values')
    
    # add x-tick labels
    plt.setp(axes, xticks=[y + 1 for y in range(len(all_data))],
         xticklabels=['x1', 'x2', 'x3', 'x4'])
    plt.show()
    

    enter image description here

    There are more examples in matplotlib's gallery.I hope you can find a example which can satisfy you.