如何创建由二进制类分隔的数据列表的小提琴图?

时间:2019-01-29 18:27:00

标签: python pandas seaborn violin-plot

我要显示的是各年龄段的小提琴图,每个年龄段都属于0类或1类。我创建了一个年龄列表,以及一个与类相对应的单独列表。我可以为一个列表绘制小提琴图,但是如何绘制由1类和0类分开的年龄分布呢?

import numpy as np
import seaborn as sns
import csv
import matplotlib.pyplot as plt

    reader = csv.reader(file)

    ages = []
    class = []

    #Here we populate our list with data from our csv
    for column in reader:
        ages.append(column[3])
        class.append(column[0])

    #Here we can initialize Figure and Axes object
    fig, ax = plt.subplots()

    #Here we create our violin plot for the age distribution
    ax.violinplot(ages, vert=False)

    #
    # Here we need to add code to plot age distribution seperated by class
    #

    #Here we show our violin plot
    plt.show()

1 个答案:

答案 0 :(得分:0)

我猜您正在寻找的是使用Seaborn小提琴图函数的x或hue参数

对于您的数据,这将作为

 #
# Here we need to add code to plot age distribution seperated by class
 sns.violinplot(x=class, y=ages)

您可以在seaborn文档中找到一些hue和x参数的示例:https://seaborn.pydata.org/generated/seaborn.violinplot.html