セクション 1. 章 10
single
Creating Swarm Plots
メニューを表示するにはスワイプしてください
The swarmplot is very similar to the stripplot, but it has a major advantage: points do not overlap.
In a stripplot, we use "jitter" to randomly scatter points so they don't sit on top of each other, but overlaps can still happen. The swarmplot uses a specific algorithm to adjust the points along the categorical axis so that they form a clear, non-overlapping shape that resembles the distribution of the data (similar to a violin plot).
Key Parameters
dodge=True: when using ahuevariable (e.g., separating smokers vs. non-smokers), this parameter separates the groups into distinct "swarms" side-by-side, rather than mixing them;linewidth: adds a border around each point, making them distinct even if they are small.
Example
Here is how dodge changes the visualization. Notice how the blue and orange points are separated.
1234567891011121314151617import seaborn as sns import matplotlib.pyplot as plt # Load dataset df = sns.load_dataset('tips') # Create a swarmplot sns.swarmplot( data=df, x='day', y='total_bill', hue='sex', dodge=True, # Separate Male/Female into side-by-side swarms size=4 ) plt.show()
タスク
スワイプしてコーディングを開始
Visualize the tips distribution using a swarmplot with distinct styling.
- Set the style to
'whitegrid'. Pass a dictionary to set the'axes.facecolor'to'seashell'. - Create a swarmplot using the
tipsdataset (df):- Map
'day'tox,'total_bill'toy, and'sex'tohue. - Set the point
sizeto2to fit more points without crashing into each other. - Add a border to the points using
linewidth=1. - Separate the categories (male/female) side-by-side by setting
dodge=True. - Use the
'rocket'palette.
- Map
- Display the plot.
解答
すべて明確でしたか?
フィードバックありがとうございます!
セクション 1. 章 10
single
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください