Wayne's Github Page

A place to learn about statistics

Visualization in Python

There are 2 main packages you need to familiarize yourself to make decent Python graphics:

Overall, seaborn creates modern looking visuals but matplotlib.pyplot controls some important internals between graphics. For example, plotting 2 subplots side by side requires plt to coordinate the two plots but sns will create beautiful looking plots without much specification.

The seaborn tutorial is quite thorough. I recommend reading the “Data structures accepted by seaborn” before reading the “Overview of seaborn plotting functions”.

To examine the plots:

The example below is a simplified version from the seaborn tutorial on function overview to show all the commands in one code block. I recommend you to add the additional parameters back into the example below to

import matplotlib.pyplot as plt
import seaborn as sns

penguins = sns.load_dataset("penguins")

f, axs = plt.subplots(1, 2)  # 1 row and 2 columns for the subplots
sns.scatterplot(data=penguins,
                x="flipper_length_mm",
                y="bill_length_mm",
                hue="species",
                ax=axs[0]) # This places this graph on the first subplot
sns.histplot(data=penguins,
             x="species",
             hue="species",
             legend=False, # This is supresses bc the first subplot has it
             ax=axs[1])    # This places this graph on the second subplot
plt.savefig('test_seaborn.png') # create a file at your working directory
plt.close()

To create good graphics, most people think about what they want to plot, look up examples online that shows a similar graphic, then tweaks the code until they achieve the desired graphic.