[Python] How to create asymmetric violin plot in python using Matplotlib

Discussão em 'Python' iniciado por Stack, Maio 30, 2025.

  1. Stack

    Stack Membro Participativo

    I have these two sample datasets:

    data = np.exp ( np.random.randn(N) )
    data[data>threshold] = threshold + np.random.randn(sum(data>threshold))*0.2
    data_1 = data
    data_2 = np.random.randn(N)


    And I would like to know how to create an asymmetric violin plot using Matplotlib plt.violinplot() in which both datasets plotted in two sides of the same axis. Unfortunately I could not find the proper options for this function as it is available for statsmodels.graphics.boxplots.violinplot(side=) or for seaborn library.
    This is my code for separated violin plots:

    import numpy as np
    import matplotlib.pyplot as plt

    N = 10000
    threshold = 5

    data = np.exp ( np.random.randn(N) )
    data[data>threshold] = threshold + np.random.randn(sum(data>threshold))*0.2

    data_1 = data
    data_2 = np.random.randn(N)
    plt.figure(figsize = (15,5))
    plt.subplot(1,2,1)
    plt.violinplot(data_1)
    plt.title('Violin Plot For Dataset 1')

    plt.subplot(1,2,2)[enter image description here][1]
    plt.violinplot(data_2)
    plt.title('Violin Plot For Dataset 2');


    The result is attached. [1]: https://i.sstatic.net/hx0Ha.png

    Continue reading...

Compartilhe esta Página