13.2: Matplotlib basics
- Page ID
- 23995
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)At the core of this library is the plot
command, which primarily takes in x and y coordinates to create a variety of plots – from line and scatter plots to bar and pie charts. It's crucial to understand that x is typically the independent variable and y, the dependent one. x and y can be either lists or numpy arrays, so the interface is quite flexible.
It pairs the elements of x and y to generate the plot points. So plot([1,2,3],[4,5,6])
plots the point pairs [1,4], [2,5], and [3,6]. Note that you do not have to provide an x array; if you do not, it will pair the y values with sequential integers beginning with zero. So plot([10,12,14])
plots the points [0,10], [1,12], and [2,14].
Matplotlib provides an extensive range of customization options to fine-tune the appearance of your plots, ensuring they are not only informative but also engaging. Central to this capability are parameters like color, marker, and line style.
Color: The color of a plot can convey a multitude of information. Matplotlib allows users to choose from a wide palette of colors. You can use standard color abbreviations like 'r' for red, 'b' for blue, or even use hexadecimal color codes for precision. It's essential to pick colors that are distinguishable, especially when multiple plots overlap, and ensure they are accessible to those with color vision deficiencies.
Markers: Markers are symbols used to represent data points. Matplotlib provides a plethora of options, from standard circles ('o') and triangles ('^') to more specialized ones like pixel markers (','). The choice of marker can help differentiate data series and should be chosen to best represent the nature and number of data points.
Line Style: The manner in which data points are connected or represented can be controlled using line styles. Options range from solid lines ('-') to dashed ('--'), dash-dot ('-.'), and dotted lines (':'). The choice of line style can indicate different types of data trends or emphasize certain aspects of your data.
For example, as mentioned, the combination 'r--o' would depict a series of red-colored data points marked with circles, connected by dashed lines. Such a combination not only provides a clear visual representation of data points but also the trend they follow.
These customizations can be defined using two primary methods: using a string shorthand after specifying the `y` values or using explicit keyword arguments. Let's delve into both methods:
1. String Shorthand Method:
When calling the `plot` function, immediately after specifying the `x` and `y` values, you can include a string that defines the line's appearance.
- Color: Single letters represent colors (e.g., 'r' for red, 'g' for green, 'b' for blue).
- Marker: Characters represent data point symbols (e.g., 'o' for circles, '^' for triangles, 's' for squares).
- Line Style: Characters represent the type of line (e.g., '-' for a solid line, '--' for a dashed line, '-.' for dash-dot).
You can combine these in a string to specify multiple attributes. For example:
- `'ro-'`: Red color, circle markers, solid line.
- `'g^--'`: Green color, triangle markers, dashed line.
2. Using Keyword Arguments:
Instead of the shorthand string, you can specify each attribute using keyword arguments in the `plot` function.
- Color: Use the `color` keyword. Example: `color='red'`
- Marker: Use the `marker` keyword. Example: `marker='o'`
- Line Style: Use the `linestyle` keyword. Example: `linestyle='--'`
When using the `plot` function, you can combine these keywords:
plt.plot(x, y, color='red', marker='o', linestyle='-')
Both methods offer the same level of customization, so the choice comes down to personal or organizational preference. The string shorthand provides a concise way to specify common combinations of properties, while the keyword arguments offer a more verbose but perhaps clearer method, especially for those unfamiliar with the shorthand conventions.