{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "![Matplotlib logo](https://matplotlib.org/stable/_images/sphx_glr_logos2_003.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Matplotlib Basics" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "## Overview\n", "We will cover the basics of using the Matplotlib library to create plots in Python, including a few different plots available within the library. This page is laid out as follows:\n", "\n", "1. Why Matplotlib?\n", "1. Figure and axes\n", "1. Basic line plots\n", "1. Labels and grid lines\n", "1. Customizing colors\n", "1. Subplots\n", "1. Scatterplots\n", "1. Displaying Images\n", "1. Contour and filled contour plots." ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "## Prerequisites\n", "| Concepts | Importance | Notes |\n", "| --- | --- | --- |\n", "| [NumPy Basics](../numpy/numpy-basics) | Necessary | |\n", "| MATLAB plotting experience | Helpful | |\n", "\n", "* **Time to Learn**: 30 minutes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Imports" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's import the Matplotlib library's `pyplot` interface; this interface is the simplest way to create new Matplotlib figures. To shorten this long name, we import it as `plt`; this helps keep things short, but clear." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
Info
\n", " Matplotlib is a Python 2-D plotting library. It is used to produce publication quality figures in a variety of hard-copy formats and interactive environments across platforms.\n", "Info
\n", " By default,ax.plot
will create a line plot, as seen in the following example: \n",
"Info
\n", " If desired, you can move the location of your legend; to do this, specify theloc
keyword argument when calling ax.legend()
.\n",
"Info
\n", " You can also use thescatter
method, which is slower, but will give you more control, such as being able to color the points individually based upon a third variable.\n",
"