# 4.2.1 Barplot

A **barplot** is one of the most common types of plot. It shows the relationship between a **numerical variable** and a **categorical variable**.

### 1. Simple Bar

```
x = np.array(list("ABCDEFGHIJ"))  # categorical variable 
y = np.arange(1, 11)
sns.barplot(x=x, y=y, palette="viridis")  
```

![Simple Barplot](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MBVV0qvqd8ON6_r9Zel%2F-MBVn10xAPTaQr7m1Hnq%2Fsimple%20barplot.png?alt=media\&token=dff77014-55ff-4ce2-a402-64becb20b6f4)

### 2. Vertical barplot

```
tips = sns.load_dataset("tips") # load embedded dataset "tips"

sns.barplot(x="day", y="total_bill", data=tips, color="royalblue")
```

![Vertical Barplot](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MBVV0qvqd8ON6_r9Zel%2F-MBW4xUWUMn4mRl1qfw_%2Fvertical%20barplot.png?alt=media\&token=9eb97ad5-d67e-4c51-8dfe-08c903ccce94)

### 3. Horizontal Barplot

```
# make a horizontal barplot
sns.barplot(x="total_bill", y="day", data=tips, color="royalblue")

# add a vertical line
plt.axvline(x = 19.8,color='r',linewidth = 2,linestyle = '--')

# add annotation
plt.text(19.8+0.2, 0.5, " Avg Bill: $20", size='small', color='r', weight='light')
```

![Horizontal Barplot](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MBVV0qvqd8ON6_r9Zel%2F-MBW5LLVViwXgU1RN-eQ%2Fhorizontal%20barplot.png?alt=media\&token=062344d6-3922-49b0-9a5e-238472a69638)

### 4. Grouped Barplot

```
sns.barplot(x="day", y="total_bill", hue="sex", data=tips, palette = "Set1")
plt.legend(loc=2)  # set legend position 
```

![Stacked Barplot](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MBVV0qvqd8ON6_r9Zel%2F-MBW65egeo9Q61jh1gEm%2Fgrouped%20barplot.png?alt=media\&token=63d2d155-be02-46ad-b444-93a3169feb08)

### 5. Facet Barplot

```
plt.figure(figsize = (12,6))
sns.catplot(x="sex", y="total_bill",
                hue="time", col="day",
                data=tips, kind="bar",
                height=5, aspect=.7,palette = "Set1");
```

![Facet Barplot](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MBVV0qvqd8ON6_r9Zel%2F-MBW6RD0SFt3C50wez3r%2FFacet%20barplot.png?alt=media\&token=2ff1b643-08c6-4b96-9dae-2af1c87b41d8)
