import vscodenb
from iplot_widget import iplot, iplot_settings
iplot_settings(theme={'palette': 'viridis'})
import seaborn as sns
data = sns.load_dataset('penguins')
data56 Plotting

This chapter introduces plotting in notebooks
You can do all the plotting you will ever need using Matplotlib, but ..
The Seaborn library builds on top of matplotlib
For inspiration, you can see some of the plots you can make on the gallery page page. tutorial
and use the interactive iplot-widget package but focuses on how data can be visualized rather than the training
Run this cell import the iplot function and to download the penguins dataframe alwo used in ?sec-pandas.
Exercise 56-1
Set X to bill_length_mm and Plot to histplot and press “Show plot”
Set Col to species and press “Show plot”.
Set Hue to sex and press “Show plot”.
What does each new version of the plot teach you about penguins?
iplot(data)Exercise 56-2
Click on “Show code for plot” to reveal the code used to produce the plot. Copy the code into a cell below this one and run it. You should see the same plot as above.
You may wonder why the arguments to the sns.displot function are written as name=value rather than just the values.
In Python, you can pass values to a function in this way too. Such keyword arguments (often called named arguments) explicitly stating the parameter name and its value using the parameter_name = value syntax. This is useful for three reasons:
- Order independence: Keyword arguments do not need to match the exact order of parameters defined in the function. This is useful if there are many in an order that hard to remember.
- Readability: Code becomes self-documenting because the intent behind each value is explicitly labeled.
- Selective defaults: If a function has multiple parameters with default values, you can override just one specific parameter without passing values for the others too.
Exercise 56-3
Click on “Show code for plot” to reveal the code used to produce the plot. Copy the code into a cell below this one and run it. You should see the same plot as above.
Change aspect=1 to aspect=2 and run it again.
Change palette='muted' to palette='colorblind' and run it again.
Add bins=30 as arguments to sns.displot and run the cell again.
How do the arguments change the plot?
Exercise 56-4
Set X to bill_length_mm, Y to bill_depth_mm, Col to species, Hue to sex, Plot to scatterplot, and press “Show plot”
Set X to bill_length_mm, Y to bill_depth_mm, Col to species, Hue to sex, Plot to kdeplot, and press “Show plot”
iplot(data)What did each new version of the plot teach you about penguins? Ask the AI for how to read a KDE plot. Then click “Show code for plot” and follow the link to visit the documentation page and verify the explanation.
Exercise 56-5
Set X to species, Y to body_mass_g, Hue to sex, Plot to boxplot, and press “Show plot”
How does this visualization compare to the one you made in the first exercise? Ask the AI for how to read a box plot. Then click “Show code for plot” and follow the link to visit the documentation page and verify the explanation.
iplot(data)Exercise 56-6
Set X to species, Y to body_mass_g, Hue to sex as in the previous exercise.
Set Plot to boxenplot and press “Show plot”
Set Plot to violinplot and press “Show plot”.
Set Plot to stripplot and press “Show plot”.
Visit the documentation page to learn how to interpret each plot.
iplot(data)Exercise 56-7
Set X to bill_length_mm, Y to bill_depth_mm, Hue to flipper_length_mm, Col to species, Row to sex, Plot to scatterplot and press “Show plot”. Any new penguin insight?
iplot(data)Exercise 56-8
Set X to bill_length_mm, Y to flipper_length_mm, Hue to body_mass_g, Col to species, Plot to scatterplot and press “Show plot”. Any new penguin insight?
iplot(data)I hope you realized along the way that kinds of plots you did will let you do some pretty powerful visualizations of just about any large data table.