0

From female/male counts in a csv file like below,

female,length
1,4
1,3
1,9
1,2
1,7
1,8
1,9

I generated the violin plot by using Python Seaborn. However, it has a sawtooth look. I do not have any prior experience in generating violin plots; however, there are examples on the web generating violin plots with the y-axis having integer samples. Can you identify the reason?

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
plt.rcParams['figure.figsize'] = (20.0, 10.0)
plt.rcParams['font.family'] = "serif"
    
df = pd.read_csv('scores.csv')
df = df.loc[df.length < 15]
  
p = sns.violinplot(data=df,
                   x = 'female',
                   y = 'length')

enter image description here

JohanC
  • 71,591
  • 8
  • 33
  • 66
Erdem Tuna
  • 500
  • 4
  • 18
  • 1
    Apparently, the lengths are discrete, so the plot has peaks at these discrete values – ForceBru Apr 06 '21 at 12:39
  • 1
    Make a histrogram / bar plot at the discrete values of length instead. – MaxNoe Apr 06 '21 at 12:54
  • 1
    Are you using the latest seaborn version (0.11.1)? It has been adapted to cope more intuitively with discrete data. How many rows are there in your dataframe? In older versions, you could try a different bandwidth, e.g. `ax = sns.violinplot(..., bw=.5)` – JohanC Apr 06 '21 at 12:54
  • @JohanC playing with `bw` field solved it. Thank you. – Erdem Tuna Apr 06 '21 at 13:18
  • @MaxNoe I will try that as well. Thanks. – Erdem Tuna Apr 06 '21 at 13:19
  • 1
    > playing with bw field solved it. Thank you. I wouldn't do that, since you imply a continous quantity by that. You just smooth out the discrete values. – MaxNoe Apr 06 '21 at 13:54
  • @MaxNoe, definetely right. I will go for histogram / bar plots instead. – Erdem Tuna Apr 06 '21 at 13:57
  • 1
    You might also be interested in showing the data as a pyramid plot, e.g. [Population Pyramide with Python and Seaborn](https://stackoverflow.com/questions/63619776/population-pyramide-with-python-and-seaborn) or [No whitespace between Seaborn barplot bars](https://stackoverflow.com/questions/59506982/no-whitespace-between-seaborn-barplot-bars) – JohanC Apr 06 '21 at 17:58
  • Great suggestion, thank you @JohanC – Erdem Tuna Apr 06 '21 at 19:28

0 Answers0