Assignment 5

Instructions:

Assignment 5 will cover the MatPlotLib and Seaborn packages.

The overall goal of this assignment is to ensure that you are comfortable with basic usage of these packages.

Follow the steps below to ensure that you receive a passing grade:

- Complete the following tasks within this notebook
- When finished, convert this notebook to an HTML/PDF file
- Place the following items into a zip folder:
    - assignment.HTML or assignment.PDF (showing the generated plots)
    - assignment.ipynb 
    - The assignment.ipynb should also write five pdf files (plot-1.pdf, plot-2.pdf ... plot-5.pdf)
- Name this folder using the naming convention: FIRSTNAME_LASTNAME_ASSIGNMENT5.zip
    - For example, If your name was Jane Doe, then the zipped folder should be called JANE_DOE_ASSIGNMENT5.zip

Recall the following content from the lesson:

Basic Guidelines for visualizations

When making professional level plots, it is very important to adhere to the following guidelines (at minimum).

    1. INCLUDE DESCRIPTIVE LABELS AND UNITS:
    • Always include a descriptive label and units on any axis-label that you create!
    • A number without units is often meaningless and frustrating for the reader
    • For example, if you plot the price of the S&P 500 along the y-axis, then it is unacceptable to just label it “price”.
      • If an outsider looks at your graph they will think, what the heck is price?
      • Is “price” the price of shoes at walmart in units of british pounds?
      • Is “price” the average price of a motorcycle at your local used car dealership in units of US pennies?
      • … what the heck is “price”
      • “price” –> BAD
      • “The instantaneous price of the S&P-500 stock index (US dollars)” –> GOOD
    1. USE LARGE FONTS: Always use relatively large font sizes for your axis-labels, tic-marks, and other plot annotations.
    • why is this important?
      • First off, it makes your plots more accessible to people who have trouble seeing small text.
      • Second, often your plots will be used in presentations. Therefore, your plots need to be legible, even from the back of the room.
    • (fontsize=12 –> BAD)(fontsize=20 –> GOOD)
    • Note: Don’t take it too far by making your axis labels comically large.
    1. DONT OVERPLOT: Over plotting is when you try to cram too much information into a single plot.
    • Usually the result is very hard for the reader to digest.
    • Typically it is best to keep your graphics relatively simple, conveying only 1 or 2 messages.
    1. ALWAYS HAVE A MESSAGE: Never include “plots for plots sake”.
    • Graphics are almost always part of some larger narrative.
    • If a particular plot doesn’t strengthen the narrative, and add meaningful value, then it should not be included.

Failure to adhere to these guidelines will consistently lose you points in any class you take in the DSAN program.

1. Seaborn and MatPlotLib

  • MPL and Seaborn are so interdependent that you can practice them simultaneously, as is the case in the assignment below.

  • IMPORTANT: MAKE SURE EVERY PLOT ADHERES TO THE VISUALIZATION GUIDELINES ABOVE

    • i.e. don’t use the default values, customize the graphs so they have large fonts, descriptive axis labels, and units.
  • Assignment: Using the Penguins database provided with seaborn, generate the following plots using Seaborn, and then customize them using MPL commands.

import seaborn as sns
import matplotlib.pyplot as plt

df = sns.load_dataset("penguins")
print(df)

Plot-1:

Use sns.relplot() to reproduce the plot in the “plot-1-original.pdf”

Hint * sns.set_theme(style=‘white’, palette=“Pastel1”)

  • Always use print(type(g)) to determine what type of object you are dealing with
  • font size = 18
  • To save use plt.savefig(‘plot-1.pdf’, bbox_inches=‘tight’)
  • g=sns.relplot() stores the MPL axes object in g.axes[i,j], starting with i,j=0,0
  • The following commands can be used to modify the legend of <class ‘seaborn.axisgrid.FacetGrid’> object
    • plt.setp(g._legend.get_texts(), fontsize=FS)
    • plt.setp(g._legend.get_title(), fontsize=FS)
# INSERT YOUR SOLUTION HERE

Plot-2:

Use sns.jointplot() to reproduce the plot in the “plot-2-original.pdf”

Hint * You can modify the MPL axes object for <class ‘matplotlib.axes._subplots.AxesSubplot’> objects using the following command * g.ax_joint.INSERT_MPL_COMMAND

* font size = 18

# INSERT YOUR SOLUTION HERE

Plot-3:

Use sns.catplot() with kind=“violin” to reproduce the plot in the “plot-3-original.pdf”

Hint * sns.catplot() outputs a <class ‘seaborn.axisgrid.FacetGrid’> type object * Therefore, various lines of code from plot-1 can be recycled

# INSERT YOUR SOLUTION HERE

Plot-4:

Use sns.catplot() with kind=“swarm” to reproduce the plot in the “plot-4-original.pdf”

Hint: you can recycle plot-3 entirely and just change one line

# INSERT YOUR SOLUTION HERE

Plot-5:

Use sns.pairplot() to reproduce the plot in the “plot-5-original.pdf”

Hint * sns.pairplot() returns a <class ‘seaborn.axisgrid.PairGrid’> type object * g=sns.pairplot() stores the MPL axes object in g.axes[i,j], starting with i,j=0,0 (similar to plot-1) * The font legend can be modified using the same method as plot-1

# INSERT YOUR SOLUTION HERE