265 lines
7.5 KiB
Plaintext
265 lines
7.5 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Simple Line Plots"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Perhaps the simplest of all plots is the visualization of a single function $y = f(x)$.\n",
|
|
"Here we will take a first look at creating a simple plot of this type.\n",
|
|
"As in all the following chapters, we'll start by setting up the notebook for plotting and importing the packages we will use:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"tags": []
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"%matplotlib inline\n",
|
|
"import matplotlib.pyplot as plt\n",
|
|
"plt.style.use('seaborn-whitegrid')\n",
|
|
"import numpy as np"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"For all Matplotlib plots, we start by creating a figure and axes.\n",
|
|
"In their simplest form, this can be done as follows (see the following figure):"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"jupyter": {
|
|
"outputs_hidden": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"fig = plt.figure()\n",
|
|
"ax = plt.axes()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"In Matplotlib, the *figure* (an instance of the class `plt.Figure`) can be thought of as a single container that contains all the objects representing axes, graphics, text, and labels.\n",
|
|
"The *axes* (an instance of the class `plt.Axes`) is what we see above: a bounding box with ticks, grids, and labels, which will eventually contain the plot elements that make up our visualization.\n",
|
|
"Throughout this part of the book, I'll commonly use the variable name `fig` to refer to a figure instance and `ax` to refer to an axes instance or group of axes instances.\n",
|
|
"\n",
|
|
"Once we have created an axes, we can use the `ax.plot` method to plot some data. Let's start with a simple sinusoid, as shown in the following figure:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"jupyter": {
|
|
"outputs_hidden": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"fig = plt.figure()\n",
|
|
"ax = plt.axes()\n",
|
|
"\n",
|
|
"x = np.linspace(0, 10, 1000)\n",
|
|
"ax.plot(x, np.sin(x));"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Note that the semicolon at the end of the last line is intentional: it suppresses the textual representation of the plot from the output.\n",
|
|
"\n",
|
|
"Alternatively, we can use the PyLab interface and let the figure and axes be created for us in the background\n",
|
|
"(see [Two Interfaces for the Price of One](04.00-Introduction-To-Matplotlib.ipynb#Two-Interfaces-for-the-Price-of-One) for a discussion of these two interfaces); as the following figure shows, the result is the same:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"jupyter": {
|
|
"outputs_hidden": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"plt.plot(x, np.sin(x));"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"If we want to create a single figure with multiple lines (see the following figure), we can simply call the `plot` function multiple times:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"jupyter": {
|
|
"outputs_hidden": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"plt.plot(x, np.sin(x))\n",
|
|
"plt.plot(x, np.cos(x));"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"That's all there is to plotting simple functions in Matplotlib!\n",
|
|
"We'll now dive into some more details about how to control the appearance of the axes and lines."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Adjusting the Plot: Line Colors and Styles"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"The first adjustment you might wish to make to a plot is to control the line colors and styles.\n",
|
|
"The `plt.plot` function takes additional arguments that can be used to specify these.\n",
|
|
"To adjust the color, you can use the `color` keyword, which accepts a string argument representing virtually any imaginable color.\n",
|
|
"The color can be specified in a variety of ways; see the following figure for the output of the following examples:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"jupyter": {
|
|
"outputs_hidden": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"plt.plot(x, np.sin(x - 0), color='blue') # specify color by name\n",
|
|
"plt.plot(x, np.sin(x - 1), color='g') # short color code (rgbcmyk)\n",
|
|
"plt.plot(x, np.sin(x - 2), color='0.75') # grayscale between 0 and 1\n",
|
|
"plt.plot(x, np.sin(x - 3), color='#FFDD44') # hex code (RRGGBB, 00 to FF)\n",
|
|
"plt.plot(x, np.sin(x - 4), color=(1.0,0.2,0.3)) # RGB tuple, values 0 to 1\n",
|
|
"plt.plot(x, np.sin(x - 5), color='chartreuse'); # HTML color names supported"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"If no color is specified, Matplotlib will automatically cycle through a set of default colors for multiple lines.\n",
|
|
"\n",
|
|
"Similarly, the line style can be adjusted using the `linestyle` keyword (see the following figure):"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"jupyter": {
|
|
"outputs_hidden": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"plt.plot(x, x + 0, linestyle='solid')\n",
|
|
"plt.plot(x, x + 1, linestyle='dashed')\n",
|
|
"plt.plot(x, x + 2, linestyle='dashdot')\n",
|
|
"plt.plot(x, x + 3, linestyle='dotted');\n",
|
|
"\n",
|
|
"# For short, you can use the following codes:\n",
|
|
"plt.plot(x, x + 4, linestyle='-') # solid\n",
|
|
"plt.plot(x, x + 5, linestyle='--') # dashed\n",
|
|
"plt.plot(x, x + 6, linestyle='-.') # dashdot\n",
|
|
"plt.plot(x, x + 7, linestyle=':'); # dotted"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Though it may be less clear to someone reading your code, you can save some keystrokes by combining these `linestyle` and `color` codes into a single non-keyword argument to the `plt.plot` function; the following figure shows the result:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"jupyter": {
|
|
"outputs_hidden": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"plt.plot(x, x + 0, '-g') # solid green\n",
|
|
"plt.plot(x, x + 1, '--c') # dashed cyan\n",
|
|
"plt.plot(x, x + 2, '-.k') # dashdot black\n",
|
|
"plt.plot(x, x + 3, ':r'); # dotted red"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"anaconda-cloud": {},
|
|
"jupytext": {
|
|
"formats": "ipynb,md"
|
|
},
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.8.8"
|
|
},
|
|
"widgets": {
|
|
"application/vnd.jupyter.widget-state+json": {
|
|
"state": {},
|
|
"version_major": 2,
|
|
"version_minor": 0
|
|
}
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
}
|