0

I'm looking to show a violin plot of peoples ages, each belonging to either class 0 or 1. I have created a list of ages, and a seperate list corresponding to class. I am able to plot a violin plot for a single list, but how can I plot the age distribution seperated by class 1 and 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()
Sreekiran A R
  • 3,123
  • 2
  • 20
  • 41
Brent
  • 51
  • 2
  • 10

1 Answers1

0

I am guessing that what you are looking for is using the x or hue parameter of seaborn violin plot function

With your data, this would go as

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

You can find some examples of the hue and x parameter in the seaborn documentation: https://seaborn.pydata.org/generated/seaborn.violinplot.html

clemgaut
  • 106
  • 1
  • 4