セクション 1. 章 12
single
Drawing Violin Plots
メニューを表示するにはスワイプしてください
A violinplot is a hybrid of a box plot and a kernel density plot (KDE).
While a boxplot only shows summary statistics (median, quartiles), a violinplot reveals the full distribution of the data. The "width" of the violin at any given point represents the density (how many data points are there).
Key Parameters
split=True: if you have ahuevariable with exactly two categories (e.g., "Male"/"Female"), this parameter draws one category on the left half of the violin and the other on the right. This makes comparing them incredibly easy;inner: controls what is drawn inside the violin;'box'(default): draws a mini boxplot;'point': draws individual data points;'quartile': draws lines for the 25th, 50th, and 75th percentiles;bw(bandwidth): controls the smoothness of the curve (just like in KDE). A smaller number reveals more detail (and noise); a larger number makes it smoother.
Example
Here is a violinplot showing the total_bill distribution. Notice how split=True allows us to compare "Smokers" vs "Non-Smokers" inside the same violin.
123456789101112131415161718import seaborn as sns import matplotlib.pyplot as plt # Load dataset df = sns.load_dataset('tips') # Create a split violinplot sns.violinplot( data=df, x='day', y='total_bill', hue='smoker', split=True, # Compare sides directly inner='quartile', # Show quartile lines palette='muted' ) plt.show()
タスク
スワイプしてコーディングを開始
Create a detailed visualization of the tips data.
- Import the necessary libraries and read the
tips.csvdataset. - Create a violinplot and assign the result to a variable named
g(this captures the plot's Axes object):- Map
'day'toxand'total_bill'toy. - Group by
'sex'usinghue. - Use the
'rocket'palette. - Split the violins to compare genders side-by-side (
split=True). - Show individual data points inside by setting
inner='point'. - Set the smoothing bandwidth
bwto0.2.
- Map
- Set the title of the plot to
'Tips violinplot'using thegvariable (e.g.,g.set_title(...)). - Display the plot.
解答
すべて明確でしたか?
フィードバックありがとうございます!
セクション 1. 章 12
single
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください