#!/usr/bin/env python # coding: utf-8 # # Background # this notebook is focused on documenting the use of [Matplotlib](https://matplotlib.org/) and [Seaborn](https://seaborn.pydata.org/) to plot various data in Python. # # You can find the docs at: # # # Setup # for plotting in Jupyter, see https://ipython.readthedocs.io/en/stable/interactive/reference.html#plotting-with-matplotlib # # That documentation states: # # If IPython is already running, you can run the `%matplotlib` magic. If no arguments are given, IPython will automatically detect your choice of matplotlib backend. You can also request a specific backend with `%matplotlib backend`, where `backend` must be one of: ‘tk’, ‘qt’, ‘wx’, ‘gtk’, ‘osx’. In the web notebook and Qt console, ‘inline’ is also a valid backend value, which produces static figures inlined inside the application window instead of matplotlib’s interactive figures that live in separate windows. # # https://github.com/matplotlib/jupyter-matplotlib is the extension for interactive # To install ipympl with pip: # ``` # pip install ipympl # # If using JupyterLab # # Install nodejs: https://nodejs.org/en/download/ # jupyter labextension install @jupyter-widgets/jupyterlab-manager # jupyter labextension install jupyter-matplotlib # ``` # # To enable the jupyter-matplotlib backend, simply use the matplotlib Jupyter magic: # # `%matplotlib widget` # # In[1]: # magic for plotting in notebook get_ipython().run_line_magic('matplotlib', 'inline') # In[2]: import matplotlib.pyplot as plt # In[4]: import seaborn as sns # # Get data # In[7]: # Seaborn has some datasets available sns.get_dataset_names() # In[8]: iris = sns.load_dataset('iris') # In[18]: type(iris) # In[9]: # datasets are pandas dataframes iris # In[ ]: # # Colors # seaborns's color palettes is documented at https://seaborn.pydata.org/tutorial/color_palettes.html#palette-tutorial # # `color_palette()` will accept the name of any seaborn palette or matplotlib colormap (except `jet`, which you should never use). It can also take a list of colors specified in any valid matplotlib format (RGB tuples, hex color codes, or HTML color names). The return value is always a list of RGB tuples. # In[26]: # get default color palette tuples sns.color_palette() # In[28]: # or to look at them sns.palplot(sns.color_palette()) # In[35]: # predefined palettes `Blues` sns.palplot(sns.color_palette("Blues")) # In[41]: # predefined palettes dark version sns.palplot(sns.color_palette("Blues_d")) # In[39]: # set size of palette sns.palplot(sns.color_palette("Blues", 10)) # In[40]: # predefined palettes sns.palplot(sns.color_palette("Greens")) # In[ ]: # # Scatter # seaborns's `scatterplot()` is documented at https://seaborn.pydata.org/generated/seaborn.scatterplot.html#seaborn.scatterplot # In[21]: # start simple sns.scatterplot(x="petal_length", y="petal_width", data=iris[iris.species == "setosa"]) # In[20]: # can separate another column by color using `hue` sns.scatterplot(x="petal_length", y="petal_width", data=iris, hue="species") # In[30]: # can choose colors redefining the `palette` sns.scatterplot(x="petal_length", y="petal_width", data=iris, hue="species", palette=["r", "g", "b"]) # In[22]: # can also separate another column by size using `size` sns.scatterplot(x="petal_length", y="petal_width", data=iris, size="species") # In[33]: # can also separate another column by markers using `style` sns.scatterplot(x="petal_length", y="petal_width", data=iris, style="species") # In[34]: # can mix combinations sns.scatterplot(x="petal_length", y="petal_width", data=iris, hue="species", style="species") # In[ ]: