Plotting with Seaborn:
- Seaborn is a plotting package that leverages the power of Matplotlib, but specializes and refines that interface to support visualization of tabular data and their statistical properties.
- By convention, seaborn is imported as sns, a module that contains a variety of different plotting functions, as outlined here.
Seabornโs Datasets:
- The seaborn package contains some sample datasets that can be loaded using the sns.load_dataset function.
- If you want to find out what datasets are available, you can call sns.get_dataset_names().
- For example, the tips dataset contains information about the total bill.
python
1import seaborn as sns
2tips = sns.load_dataset("tips")
3sns.set_theme(style="darkgrid")
4tips.head()Output:
| total_bill | tip | sex | smoker | day | time | size |
|---|---|---|---|---|---|---|
| 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
| 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
| 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
| 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
| 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
Visualizing Tips vs. Total Bill with Seaborn:
- There are several ways to draw a scatter plot in seaborn, including the relplot() function.
- The relplot() function is a figure-level function for visualizing statistical relationships using two common approaches: scatter plots and line plots.
python
5tips = sns.load_dataset("tips")
6sns.relplot(data=tips, x="total_bill", y="tip")Output:
