Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Creating Swarm Plots | Section
Statistical Visualization with Seaborn
セクション 1.  10
single

single

bookCreating 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 a hue variable (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.

1234567891011121314151617
import 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()
copy
タスク

スワイプしてコーディングを開始

Visualize the tips distribution using a swarmplot with distinct styling.

  1. Set the style to 'whitegrid'. Pass a dictionary to set the 'axes.facecolor' to 'seashell'.
  2. Create a swarmplot using the tips dataset (df):
    • Map 'day' to x, 'total_bill' to y, and 'sex' to hue.
    • Set the point size to 2 to 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.
  3. Display the plot.

解答

Switch to desktop実践的な練習のためにデスクトップに切り替える下記のオプションのいずれかを利用して、現在の場所から続行する
すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  10
single

single

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

some-alt