CalculusWithJuliaNotes.jl/quarto/0e7f54ed/alternatives/makie_plotting.html
2022-08-11 13:15:19 -04:00

1806 lines
173 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.0.32">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>Calculus with Julia - 65&nbsp; Calculus plots with Makie</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<script src="../site_libs/quarto-nav/quarto-nav.js"></script>
<script src="../site_libs/quarto-nav/headroom.min.js"></script>
<script src="../site_libs/clipboard/clipboard.min.js"></script>
<script src="../site_libs/quarto-search/autocomplete.umd.js"></script>
<script src="../site_libs/quarto-search/fuse.min.js"></script>
<script src="../site_libs/quarto-search/quarto-search.js"></script>
<meta name="quarto:offset" content="../">
<link href="../misc/getting_started_with_julia.html" rel="next">
<link href="../alternatives/plotly_plotting.html" rel="prev">
<script src="../site_libs/quarto-html/quarto.js"></script>
<script src="../site_libs/quarto-html/popper.min.js"></script>
<script src="../site_libs/quarto-html/tippy.umd.min.js"></script>
<script src="../site_libs/quarto-html/anchor.min.js"></script>
<link href="../site_libs/quarto-html/tippy.css" rel="stylesheet">
<link href="../site_libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="../site_libs/bootstrap/bootstrap.min.js"></script>
<link href="../site_libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="../site_libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
<script id="quarto-search-options" type="application/json">{
"location": "navbar",
"copy-button": false,
"collapse-after": 3,
"panel-placement": "end",
"type": "overlay",
"limit": 20,
"language": {
"search-no-results-text": "No results",
"search-matching-documents-text": "matching documents",
"search-copy-link-title": "Copy link to search",
"search-hide-matches-text": "Hide additional matches",
"search-more-match-text": "more match in this document",
"search-more-matches-text": "more matches in this document",
"search-clear-button-title": "Clear",
"search-detached-cancel-button-title": "Cancel",
"search-submit-button-title": "Submit"
}
}</script>
<script async="" src="https://hypothes.is/embed.js"></script>
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js" type="text/javascript"></script>
</head>
<body class="nav-sidebar floating nav-fixed">
<div id="quarto-search-results"></div>
<header id="quarto-header" class="headroom fixed-top">
<nav class="navbar navbar-expand-lg navbar-dark ">
<div class="navbar-container container-fluid">
<a class="navbar-brand" href="../index.html">
<img src="../logo.png" alt="">
<span class="navbar-title">Calculus with Julia</span>
</a>
<div id="quarto-search" class="" title="Search"></div>
</div> <!-- /container-fluid -->
</nav>
<nav class="quarto-secondary-nav" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
<div class="container-fluid d-flex justify-content-between">
<h1 class="quarto-secondary-nav-title"><span class="chapter-number">65</span>&nbsp; <span class="chapter-title">Calculus plots with Makie</span></h1>
<button type="button" class="quarto-btn-toggle btn" aria-label="Show secondary navigation">
<i class="bi bi-chevron-right"></i>
</button>
</div>
</nav>
</header>
<!-- content -->
<div id="quarto-content" class="quarto-container page-columns page-rows-contents page-layout-article page-navbar">
<!-- sidebar -->
<nav id="quarto-sidebar" class="sidebar collapse sidebar-navigation floating overflow-auto">
<div class="mt-2 flex-shrink-0 align-items-center">
<div class="sidebar-search">
<div id="quarto-search" class="" title="Search"></div>
</div>
</div>
<div class="sidebar-menu-container">
<ul class="list-unstyled mt-1">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../index.html" class="sidebar-item-text sidebar-link">Preface</a>
</div>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-1" aria-expanded="false">Precalculus Concepts</a>
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-1" aria-expanded="false">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-1" class="collapse list-unstyled sidebar-section depth1 ">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/calculator.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">1</span>&nbsp; <span class="chapter-title">From calculator to computer</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/variables.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">2</span>&nbsp; <span class="chapter-title">Variables</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/numbers_types.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">3</span>&nbsp; <span class="chapter-title">Number systems</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/logical_expressions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">4</span>&nbsp; <span class="chapter-title">Inequalities, Logical expressions</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/vectors.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">5</span>&nbsp; <span class="chapter-title">Vectors</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/ranges.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">6</span>&nbsp; <span class="chapter-title">Ranges and Sets</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/functions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">7</span>&nbsp; <span class="chapter-title">Functions</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/plotting.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">8</span>&nbsp; <span class="chapter-title">The Graph of a Function</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/transformations.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">9</span>&nbsp; <span class="chapter-title">Function manipulations</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/inversefunctions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">10</span>&nbsp; <span class="chapter-title">The Inverse of a Function</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/polynomial.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">11</span>&nbsp; <span class="chapter-title">Polynomials</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/polynomial_roots.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">12</span>&nbsp; <span class="chapter-title">Roots of a polynomial</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/polynomials_package.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">13</span>&nbsp; <span class="chapter-title">The Polynomials package</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/rational_functions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">14</span>&nbsp; <span class="chapter-title">Rational functions</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/exp_log_functions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">15</span>&nbsp; <span class="chapter-title">Exponential and logarithmic functions</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/trig_functions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">16</span>&nbsp; <span class="chapter-title">Trigonometric functions</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/julia_overview.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">17</span>&nbsp; <span class="chapter-title">Overview of Julia commands</span></a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-2" aria-expanded="false">Limits</a>
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-2" aria-expanded="false">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-2" class="collapse list-unstyled sidebar-section depth1 ">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../limits/limits.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">18</span>&nbsp; <span class="chapter-title">Limits</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../limits/limits_extensions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">19</span>&nbsp; <span class="chapter-title">Limits, issues, extensions of the concept</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../limits/continuity.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">20</span>&nbsp; <span class="chapter-title">Continuity</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../limits/intermediate_value_theorem.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">21</span>&nbsp; <span class="chapter-title">Implications of continuity</span></a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-3" aria-expanded="false">Derivatives</a>
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-3" aria-expanded="false">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-3" class="collapse list-unstyled sidebar-section depth1 ">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/derivatives.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">22</span>&nbsp; <span class="chapter-title">Derivatives</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/numeric_derivatives.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">23</span>&nbsp; <span class="chapter-title">Numeric derivatives</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/symbolic_derivatives.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">24</span>&nbsp; <span class="chapter-title">Symbolic derivatives</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/mean_value_theorem.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">25</span>&nbsp; <span class="chapter-title">The mean value theorem for differentiable functions.</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/optimization.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">26</span>&nbsp; <span class="chapter-title">Optimization</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/first_second_derivatives.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">27</span>&nbsp; <span class="chapter-title">The first and second derivatives</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/curve_sketching.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">28</span>&nbsp; <span class="chapter-title">Curve Sketching</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/linearization.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">29</span>&nbsp; <span class="chapter-title">Linearization</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/newtons_method.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">30</span>&nbsp; <span class="chapter-title">Newtons method</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/more_zeros.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">31</span>&nbsp; <span class="chapter-title">Derivative-free alternatives to Newtons method</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/lhospitals_rule.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">32</span>&nbsp; <span class="chapter-title">LHospitals Rule</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/implicit_differentiation.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">33</span>&nbsp; <span class="chapter-title">Implicit Differentiation</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/related_rates.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">34</span>&nbsp; <span class="chapter-title">Related rates</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/taylor_series_polynomials.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">35</span>&nbsp; <span class="chapter-title">Taylor Polynomials and other Approximating Polynomials</span></a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-4" aria-expanded="false">Integrals</a>
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-4" aria-expanded="false">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-4" class="collapse list-unstyled sidebar-section depth1 ">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/area.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">36</span>&nbsp; <span class="chapter-title">Area under a curve</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/ftc.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">37</span>&nbsp; <span class="chapter-title">Fundamental Theorem or Calculus</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/substitution.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">38</span>&nbsp; <span class="chapter-title">Substitution</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/integration_by_parts.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">39</span>&nbsp; <span class="chapter-title">Integration By Parts</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/partial_fractions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">40</span>&nbsp; <span class="chapter-title">Partial Fractions</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/improper_integrals.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">41</span>&nbsp; <span class="chapter-title">Improper Integrals</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/mean_value_theorem.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">42</span>&nbsp; <span class="chapter-title">Mean value theorem for integrals</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/area_between_curves.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">43</span>&nbsp; <span class="chapter-title">Area between two curves</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/center_of_mass.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">44</span>&nbsp; <span class="chapter-title">Center of Mass</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/volumes_slice.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">45</span>&nbsp; <span class="chapter-title">Volumes by slicing</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/arc_length.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">46</span>&nbsp; <span class="chapter-title">Arc length</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/surface_area.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">47</span>&nbsp; <span class="chapter-title">Surface Area</span></a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-5" aria-expanded="false">ODEs</a>
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-5" aria-expanded="false">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-5" class="collapse list-unstyled sidebar-section depth1 ">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../ODEs/odes.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">48</span>&nbsp; <span class="chapter-title">ODEs</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../ODEs/euler.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">49</span>&nbsp; <span class="chapter-title">Eulers method</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../ODEs/solve.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">50</span>&nbsp; <span class="chapter-title">The problem-algorithm-solve interface</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../ODEs/differential_equations.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">51</span>&nbsp; <span class="chapter-title">The <code>DifferentialEquations</code> suite</span></a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-6" aria-expanded="false">Differential vector calculus</a>
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-6" aria-expanded="false">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-6" class="collapse list-unstyled sidebar-section depth1 ">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../differentiable_vector_calculus/polar_coordinates.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">52</span>&nbsp; <span class="chapter-title">Polar Coordinates and Curves</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../differentiable_vector_calculus/vectors.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">53</span>&nbsp; <span class="chapter-title">Vectors and matrices</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../differentiable_vector_calculus/vector_valued_functions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">54</span>&nbsp; <span class="chapter-title">Vector-valued functions, <span class="math inline">\(f:R \rightarrow R^n\)</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../differentiable_vector_calculus/scalar_functions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">55</span>&nbsp; <span class="chapter-title">Scalar functions</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../differentiable_vector_calculus/scalar_functions_applications.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">56</span>&nbsp; <span class="chapter-title">Applications with scalar functions</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../differentiable_vector_calculus/vector_fields.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">57</span>&nbsp; <span class="chapter-title">Functions <span class="math inline">\(R^n \rightarrow R^m\)</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../differentiable_vector_calculus/plots_plotting.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">58</span>&nbsp; <span class="chapter-title">2D and 3D plots in Julia with Plots</span></a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-7" aria-expanded="false">Integral vector calculus</a>
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-7" aria-expanded="false">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-7" class="collapse list-unstyled sidebar-section depth1 ">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integral_vector_calculus/double_triple_integrals.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">59</span>&nbsp; <span class="chapter-title">Multi-dimensional integrals</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integral_vector_calculus/line_integrals.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">60</span>&nbsp; <span class="chapter-title">Line and Surface Integrals</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integral_vector_calculus/div_grad_curl.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">61</span>&nbsp; <span class="chapter-title">The Gradient, Divergence, and Curl</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integral_vector_calculus/stokes_theorem.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">62</span>&nbsp; <span class="chapter-title">Greens Theorem, Stokes Theorem, and the Divergence Theorem</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integral_vector_calculus/review.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">63</span>&nbsp; <span class="chapter-title">Quick Review of Vector Calculus</span></a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-8" aria-expanded="true">Alternatives</a>
<a class="sidebar-item-toggle text-start" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-8" aria-expanded="true">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-8" class="collapse list-unstyled sidebar-section depth1 show">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../alternatives/plotly_plotting.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">64</span>&nbsp; <span class="chapter-title">JavaScript based plotting libraries</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../alternatives/makie_plotting.html" class="sidebar-item-text sidebar-link active"><span class="chapter-number">65</span>&nbsp; <span class="chapter-title">Calculus plots with Makie</span></a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-9" aria-expanded="false">Appendices</a>
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-9" aria-expanded="false">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-9" class="collapse list-unstyled sidebar-section depth1 ">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../misc/getting_started_with_julia.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">66</span>&nbsp; <span class="chapter-title">Getting started with Julia</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../misc/julia_interfaces.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">67</span>&nbsp; <span class="chapter-title">Julia interfaces</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../misc/calculus_with_julia.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">68</span>&nbsp; <span class="chapter-title">The <code>CalculusWithJulia</code> package</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../misc/unicode.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">69</span>&nbsp; <span class="chapter-title">Usages of Unicode symbols</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../misc/quick_notes.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">70</span>&nbsp; <span class="chapter-title">Quick introduction to Calculus with Julia</span></a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../references.html" class="sidebar-item-text sidebar-link">References</a>
</div>
</li>
</ul>
</div>
</nav>
<!-- margin-sidebar -->
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
<nav id="TOC" role="doc-toc">
<h2 id="toc-title">Table of contents</h2>
<ul>
<li><a href="#figures" id="toc-figures" class="nav-link active" data-scroll-target="#figures"> <span class="header-section-number">65.1</span> Figures</a></li>
<li><a href="#points-scatter" id="toc-points-scatter" class="nav-link" data-scroll-target="#points-scatter"> <span class="header-section-number">65.2</span> Points (<code>scatter</code>)</a>
<ul class="collapse">
<li><a href="#point2-point3" id="toc-point2-point3" class="nav-link" data-scroll-target="#point2-point3"> <span class="header-section-number">65.2.1</span> <code>Point2</code>, <code>Point3</code></a></li>
<li><a href="#attributes" id="toc-attributes" class="nav-link" data-scroll-target="#attributes"> <span class="header-section-number">65.2.2</span> Attributes</a></li>
</ul></li>
<li><a href="#curves" id="toc-curves" class="nav-link" data-scroll-target="#curves"> <span class="header-section-number">65.3</span> Curves</a>
<ul class="collapse">
<li><a href="#plots-of-univariate-functions" id="toc-plots-of-univariate-functions" class="nav-link" data-scroll-target="#plots-of-univariate-functions"> <span class="header-section-number">65.3.1</span> Plots of univariate functions</a></li>
<li><a href="#text-annotations" id="toc-text-annotations" class="nav-link" data-scroll-target="#text-annotations"> <span class="header-section-number">65.3.2</span> Text (<code>annotations</code>)</a></li>
<li><a href="#plots-of-parametric-functions" id="toc-plots-of-parametric-functions" class="nav-link" data-scroll-target="#plots-of-parametric-functions"> <span class="header-section-number">65.3.3</span> Plots of parametric functions</a></li>
</ul></li>
<li><a href="#surfaces" id="toc-surfaces" class="nav-link" data-scroll-target="#surfaces"> <span class="header-section-number">65.4</span> Surfaces</a></li>
<li><a href="#contour-plots-contour-contourf-heatmap" id="toc-contour-plots-contour-contourf-heatmap" class="nav-link" data-scroll-target="#contour-plots-contour-contourf-heatmap"> <span class="header-section-number">65.5</span> Contour plots (<code>contour</code>, <code>contourf</code>, <code>heatmap</code>)</a>
<ul class="collapse">
<li><a href="#three-dimensional-contour-plots" id="toc-three-dimensional-contour-plots" class="nav-link" data-scroll-target="#three-dimensional-contour-plots"> <span class="header-section-number">65.5.1</span> Three dimensional contour plots</a></li>
<li><a href="#implicitly-defined-curves-and-surfaces" id="toc-implicitly-defined-curves-and-surfaces" class="nav-link" data-scroll-target="#implicitly-defined-curves-and-surfaces"> <span class="header-section-number">65.5.2</span> Implicitly defined curves and surfaces</a></li>
</ul></li>
<li><a href="#vector-fields.-visualizations-of-fr2-rightarrow-r2" id="toc-vector-fields.-visualizations-of-fr2-rightarrow-r2" class="nav-link" data-scroll-target="#vector-fields.-visualizations-of-fr2-rightarrow-r2"> <span class="header-section-number">65.6</span> Vector fields. Visualizations of <span class="math inline">\(f:R^2 \rightarrow R^2\)</span></a></li>
<li><a href="#layoutables-and-observables" id="toc-layoutables-and-observables" class="nav-link" data-scroll-target="#layoutables-and-observables"> <span class="header-section-number">65.7</span> Layoutables and Observables</a>
<ul class="collapse">
<li><a href="#layoutables" id="toc-layoutables" class="nav-link" data-scroll-target="#layoutables"> <span class="header-section-number">65.7.1</span> Layoutables</a></li>
<li><a href="#observables" id="toc-observables" class="nav-link" data-scroll-target="#observables"> <span class="header-section-number">65.7.2</span> Observables</a></li>
</ul></li>
</ul>
<div class="toc-actions"><div><i class="bi bi-github"></i></div><div class="action-links"><p><a href="https://github.com/jverzani/CalculusWithJuliaNotes.jl/edit/main/quarto/alternatives/makie_plotting.qmd" class="toc-action">Edit this page</a></p><p><a href="https://github.com/jverzani/CalculusWithJuliaNotes.jl/issues/new" class="toc-action">Report an issue</a></p></div></div></nav>
</div>
<!-- main -->
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title d-none d-lg-block"><span class="chapter-number">65</span>&nbsp; <span class="chapter-title">Calculus plots with Makie</span></h1>
</div>
<div class="quarto-title-meta">
</div>
</header>
<p>The <a href="https://github.com/JuliaPlots/Makie.jl">Makie.jl webpage</a> says</p>
<blockquote class="blockquote">
<p>From the Japanese word Maki-e, which is a technique to sprinkle lacquer with gold and silver powder. Data is basically the gold and silver of our age, so lets spread it out beautifully on the screen!</p>
</blockquote>
<p><code>Makie</code> itself is a metapackage for a rich ecosystem. We show how to use the interface provided by the <code>GLMakie</code> backend to produce the familiar graphics of calculus.</p>
<div class="callout-note callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
Examples and tutorials
</div>
</div>
<div class="callout-body-container callout-body">
<p><code>Makie</code> is a sophisticated plotting package, and capable of an enormous range of plots (cf.&nbsp;<a href="https://makie.juliaplots.org/stable/examples/plotting_functions/">examples</a>.) <code>Makie</code> also has numerous <a href="https://makie.juliaplots.org/stable/tutorials/">tutorials</a> to learn from. These are far more extensive that what is described herein, as this section focuses just on the graphics from calculus.</p>
</div>
</div>
<section id="figures" class="level2" data-number="65.1">
<h2 data-number="65.1" class="anchored" data-anchor-id="figures"><span class="header-section-number">65.1</span> Figures</h2>
<p>Makie draws graphics onto a canvas termed a “scene” in the Makie documentation. A scene is an implementation detail, the basic (non-mutating) plotting commands described below return a <code>FigureAxisPlot</code> object, a compound object that combines a figure, an axes, and a plot object. The <code>show</code> method for these objects display the figure.</p>
<p>For <code>Makie</code> there are the <code>GLMakie</code>, <code>WGLMakie</code>, and <code>CairoMakie</code> backends for different types of canvases. In the following, we have used <code>GLMakie</code>. <code>WGLMakie</code> is useful for incorporating <code>Makie</code> plots into web-based technologies.</p>
<p>We begin by loading the main package and the <code>norm</code> function from the standard <code>LinearAlgebra</code> package:</p>
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="im">using</span> <span class="bu">GLMakie</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> <span class="bu">LinearAlgebra</span>: norm</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<p>The <code>Makie</code> developers have workarounds for the delayed time to first plot, but without utilizing these the time to load the package is lengthy.</p>
</section>
<section id="points-scatter" class="level2" data-number="65.2">
<h2 data-number="65.2" class="anchored" data-anchor-id="points-scatter"><span class="header-section-number">65.2</span> Points (<code>scatter</code>)</h2>
<p>The task of plotting the points, say <span class="math inline">\((1,2)\)</span>, <span class="math inline">\((2,3)\)</span>, <span class="math inline">\((3,2)\)</span> can be done different ways. Most plotting packages, and <code>Makie</code> is no exception, allow the following: form vectors of the <span class="math inline">\(x\)</span> and <span class="math inline">\(y\)</span> values then plot those with <code>scatter</code>:</p>
<div class="cell" data-execution_count="3">
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> [<span class="fl">1</span>,<span class="fl">2</span>,<span class="fl">3</span>]</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>ys <span class="op">=</span> [<span class="fl">2</span>,<span class="fl">3</span>,<span class="fl">2</span>]</span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="fu">scatter</span>(xs, ys)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="4">
<p><img src="makie_plotting_files/figure-html/cell-4-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>The <code>scatter</code> function creates and returns an object, which when displayed shows the plot.</p>
<section id="point2-point3" class="level3" data-number="65.2.1">
<h3 data-number="65.2.1" class="anchored" data-anchor-id="point2-point3"><span class="header-section-number">65.2.1</span> <code>Point2</code>, <code>Point3</code></h3>
<p>When learning about points on the Cartesian plane, a “<code>t</code>”-chart is often produced:</p>
<pre><code>x | y
-----
1 | 2
2 | 3
3 | 2</code></pre>
<p>The <code>scatter</code> usage above used the columns. The rows are associated with the points, and these too can be used to produce the same graphic. Rather than make vectors of <span class="math inline">\(x\)</span> and <span class="math inline">\(y\)</span> (and optionally <span class="math inline">\(z\)</span>) coordinates, it is more idiomatic to create a vector of “points.” <code>Makie</code> utilizes a <code>Point</code> type to store a 2 or 3 dimensional point. The <code>Point2</code> and <code>Point3</code> constructors will be utilized.</p>
<p><code>Makie</code> uses a GPU, when present, to accelerate the graphic rendering. GPUs employ 32-bit numbers. Julia uses an <code>f0</code> to indicate 32-bit floating points. Hence the alternate types <code>Point2f0</code> to store 2D points as 32-bit numbers and <code>Points3f0</code> to store 3D points as 32-bit numbers are seen in the documentation for Makie.</p>
<p>We can plot a vector of points in as direct manner as vectors of their coordinates:</p>
<div class="cell" data-execution_count="4">
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>pts <span class="op">=</span> [<span class="fu">Point2</span>(<span class="fl">1</span>,<span class="fl">2</span>), <span class="fu">Point2</span>(<span class="fl">2</span>,<span class="fl">3</span>), <span class="fu">Point2</span>(<span class="fl">3</span>,<span class="fl">2</span>)]</span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="fu">scatter</span>(pts)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="5">
<p><img src="makie_plotting_files/figure-html/cell-5-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>A typical usage is to generate points from some vector-valued function. Say we have a parameterized function <code>r</code> taking <span class="math inline">\(R\)</span> into <span class="math inline">\(R^2\)</span> defined by:</p>
<div class="cell" data-execution_count="5">
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="fu">r</span>(t) <span class="op">=</span> [<span class="fu">sin</span>(t), <span class="fu">cos</span>(t)]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="6">
<pre><code>r (generic function with 1 method)</code></pre>
</div>
</div>
<p>Then broadcasting values gives a vector of vectors, each identified with a point:</p>
<div class="cell" data-execution_count="6">
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a>ts <span class="op">=</span> [<span class="fl">1</span>,<span class="fl">2</span>,<span class="fl">3</span>]</span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a><span class="fu">r</span>.(ts)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="7">
<pre><code>3-element Vector{Vector{Float64}}:
[0.8414709848078965, 0.5403023058681398]
[0.9092974268256817, -0.4161468365471424]
[0.1411200080598672, -0.9899924966004454]</code></pre>
</div>
</div>
<p>We can broadcast <code>Point2</code> over this to create a vector of <code>Point</code> objects:</p>
<div class="cell" data-execution_count="7">
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>pts <span class="op">=</span> <span class="fu">Point2</span>.(<span class="fu">r</span>.(ts))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="8">
<pre><code>3-element Vector{Point2{Float64}}:
[0.8414709848078965, 0.5403023058681398]
[0.9092974268256817, -0.4161468365471424]
[0.1411200080598672, -0.9899924966004454]</code></pre>
</div>
</div>
<p>These then can be plotted directly:</p>
<div class="cell" data-execution_count="8">
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="fu">scatter</span>(pts)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="9">
<p><img src="makie_plotting_files/figure-html/cell-9-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>The ploting of points in three dimesions is essentially the same, save the use of <code>Point3</code> instead of <code>Point2</code>.</p>
<div class="cell" data-execution_count="9">
<div class="sourceCode cell-code" id="cb12"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="fu">r</span>(t) <span class="op">=</span> [<span class="fu">sin</span>(t), <span class="fu">cos</span>(t), t]</span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a>ts <span class="op">=</span> <span class="fu">range</span>(<span class="fl">0</span>, <span class="fl">4</span>pi, length<span class="op">=</span><span class="fl">100</span>)</span>
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a>pts <span class="op">=</span> <span class="fu">Point3</span>.(<span class="fu">r</span>.(ts))</span>
<span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a><span class="fu">scatter</span>(pts; markersize<span class="op">=</span><span class="fl">5</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="10">
<p><img src="makie_plotting_files/figure-html/cell-10-output-1.png" class="img-fluid"></p>
</div>
</div>
<hr>
<p>To plot points generated in terms of vectors of coordinates, the component vectors must be created. The “<code>t</code>”-table shows how, simply loop over each column and add the corresponding <span class="math inline">\(x\)</span> or <span class="math inline">\(y\)</span> (or <span class="math inline">\(z\)</span>) value. This utility function does exactly that, returning the vectors in a tuple.</p>
<div class="cell" data-execution_count="10">
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="fu">unzip</span>(vs) <span class="op">=</span> <span class="fu">Tuple</span>([vs[j][i] for j <span class="kw">in</span> <span class="fu">eachindex</span>(vs)] <span class="cf">for</span> i <span class="kw">in</span> <span class="fu">eachindex</span>(vs[<span class="fl">1</span>]))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="11">
<pre><code>unzip (generic function with 1 method)</code></pre>
</div>
</div>
<div class="callout-note callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>In the <code>CalculusWithJulia</code> package, <code>unzip</code> is implemented using <code>SplitApplyCombine.invert</code>.</p>
</div>
</div>
<p>We might have then:</p>
<div class="cell" data-execution_count="11">
<div class="sourceCode cell-code" id="cb15"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="fu">scatter</span>(<span class="fu">unzip</span>(<span class="fu">r</span>.(ts))<span class="op">...</span>; markersize<span class="op">=</span><span class="fl">5</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="12">
<p><img src="makie_plotting_files/figure-html/cell-12-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>where splatting is used to specify the <code>xs</code>, <code>ys</code>, and <code>zs</code> to <code>scatter</code>.</p>
<p>(Compare to <code>scatter(Point3.(r.(ts)))</code> or <code>scatter(Point3∘r).(ts))</code>.)</p>
</section>
<section id="attributes" class="level3" data-number="65.2.2">
<h3 data-number="65.2.2" class="anchored" data-anchor-id="attributes"><span class="header-section-number">65.2.2</span> Attributes</h3>
<p>A point is drawn with a “marker” with a certain size and color. These attributes can be adjusted, as in the following:</p>
<div class="cell" data-execution_count="12">
<div class="sourceCode cell-code" id="cb16"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="fu">scatter</span>(xs, ys;</span>
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a> marker<span class="op">=</span>[<span class="op">:</span>x,<span class="op">:</span>cross, <span class="op">:</span>circle], markersize<span class="op">=</span><span class="fl">25</span>,</span>
<span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a> color<span class="op">=:</span>blue)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="13">
<p><img src="makie_plotting_files/figure-html/cell-13-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>Marker attributes include</p>
<ul>
<li><code>marker</code> a symbol, shape.</li>
<li><code>marker_offset</code> offset coordinates</li>
<li><code>markersize</code> size (radius pixels) of marker</li>
</ul>
<p>A single value will be repeated. A vector of values of a matching size will specify the attribute on a per point basis.</p>
</section>
</section>
<section id="curves" class="level2" data-number="65.3">
<h2 data-number="65.3" class="anchored" data-anchor-id="curves"><span class="header-section-number">65.3</span> Curves</h2>
<p>The curves of calculus are lines. The <code>lines</code> command of <code>Makie</code> will render a curve by connecting a series of points with straight-line segments. By taking a sufficient number of points the connect-the-dot figure can appear curved.</p>
<section id="plots-of-univariate-functions" class="level3" data-number="65.3.1">
<h3 data-number="65.3.1" class="anchored" data-anchor-id="plots-of-univariate-functions"><span class="header-section-number">65.3.1</span> Plots of univariate functions</h3>
<p>The basic plot of univariate calculus is the graph of a function <span class="math inline">\(f\)</span> over an interval <span class="math inline">\([a,b]\)</span>. This is implemented using a familiar strategy: produce a series of representative values between <span class="math inline">\(a\)</span> and <span class="math inline">\(b\)</span>; produce the corresponding <span class="math inline">\(f(x)\)</span> values; plot these as points and connect the points with straight lines.</p>
<p>To create regular values between <code>a</code> and <code>b</code> typically the <code>range</code> function or the range operator (<code>a:h:b</code>) are employed. The the related <code>LinRange</code> function is also an option.</p>
<p>For example:</p>
<div class="cell" data-execution_count="13">
<div class="sourceCode cell-code" id="cb17"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x) <span class="op">=</span> <span class="fu">sin</span>(x)</span>
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a>a, b <span class="op">=</span> <span class="fl">0</span>, <span class="fl">2</span>pi</span>
<span id="cb17-3"><a href="#cb17-3" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> <span class="fu">range</span>(a, b, length<span class="op">=</span><span class="fl">250</span>)</span>
<span id="cb17-4"><a href="#cb17-4" aria-hidden="true" tabindex="-1"></a><span class="fu">lines</span>(xs, <span class="fu">f</span>.(xs))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="14">
<p><img src="makie_plotting_files/figure-html/cell-14-output-1.png" class="img-fluid"></p>
</div>
</div>
<p><code>Makie</code> also will read the interval notation of <code>IntervalSets</code> and select its own set of intermediate points:</p>
<div class="cell" data-execution_count="14">
<div class="sourceCode cell-code" id="cb18"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="fu">lines</span>(a<span class="op">..</span>b, f)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="15">
<p><img src="makie_plotting_files/figure-html/cell-15-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>As with <code>scatter</code>, <code>lines</code> returns an object that produces a graphic when displayed.</p>
<p>As with <code>scatter</code>, <code>lines</code> can can also be drawn using a vector of points:</p>
<div class="cell" data-execution_count="15">
<div class="sourceCode cell-code" id="cb19"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a>pts <span class="op">=</span> [<span class="fu">Point2</span>(x, <span class="fu">f</span>(x)) for x <span class="op"></span> xs]</span>
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a><span class="fu">lines</span>(pts)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="16">
<p><img src="makie_plotting_files/figure-html/cell-16-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>(Though the advantage isnt clear here, this will be useful when the points are generated in different manners.)</p>
<p>When a <code>y</code> value is <code>NaN</code> or infinite, the connecting lines are not drawn:</p>
<div class="cell" data-execution_count="16">
<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> <span class="fl">1</span><span class="op">:</span><span class="fl">5</span></span>
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a>ys <span class="op">=</span> [<span class="fl">1</span>,<span class="fl">2</span>,<span class="cn">NaN</span>, <span class="fl">4</span>, <span class="fl">5</span>]</span>
<span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></a><span class="fu">lines</span>(xs, ys)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="17">
<p><img src="makie_plotting_files/figure-html/cell-17-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>As with other plotting packages, this is useful to represent discontinuous functions, such as what occurs at a vertical asymptote or a step function.</p>
<section id="adding-to-a-figure-lines-scatter" class="level4">
<h4 class="anchored" data-anchor-id="adding-to-a-figure-lines-scatter">Adding to a figure (<code>lines!</code>, <code>scatter!</code>, …)</h4>
<p>To <em>add</em> or <em>modify</em> a scene can be done using a mutating version of a plotting primitive, such as <code>lines!</code> or <code>scatter!</code>. The names follow <code>Julia</code>s convention of using an <code>!</code> to indicate that a function modifies an argument, in this case the underlying figure.</p>
<p>Here is one way to show two plots at once:</p>
<div class="cell" data-execution_count="17">
<div class="sourceCode cell-code" id="cb21"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> <span class="fu">range</span>(<span class="fl">0</span>, <span class="fl">2</span>pi, length<span class="op">=</span><span class="fl">100</span>)</span>
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a><span class="fu">lines</span>(xs, <span class="fu">sin</span>.(xs))</span>
<span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></a><span class="fu">lines!</span>(xs, <span class="fu">cos</span>.(xs))</span>
<span id="cb21-4"><a href="#cb21-4" aria-hidden="true" tabindex="-1"></a><span class="fu">current_figure</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="18">
<p><img src="makie_plotting_files/figure-html/cell-18-output-1.png" class="img-fluid"></p>
</div>
</div>
<div class="callout-note callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
Current figure
</div>
</div>
<div class="callout-body-container callout-body">
<p>The <code>current_figure</code> call is needed to have the figure display, as the returned value of <code>lines!</code> is not a figure object. (Figure objects display when shown as the output of a cell.)</p>
</div>
</div>
<p>We will see soon how to modify the line attributes so that the curves can be distinguished.</p>
<p>The following shows the construction details in the graphic:</p>
<div class="cell" data-execution_count="18">
<div class="sourceCode cell-code" id="cb22"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> <span class="fu">range</span>(<span class="fl">0</span>, <span class="fl">2</span>pi, length<span class="op">=</span><span class="fl">10</span>)</span>
<span id="cb22-2"><a href="#cb22-2" aria-hidden="true" tabindex="-1"></a><span class="fu">lines</span>(xs, <span class="fu">sin</span>.(xs))</span>
<span id="cb22-3"><a href="#cb22-3" aria-hidden="true" tabindex="-1"></a><span class="fu">scatter!</span>(xs, <span class="fu">sin</span>.(xs);</span>
<span id="cb22-4"><a href="#cb22-4" aria-hidden="true" tabindex="-1"></a> markersize<span class="op">=</span><span class="fl">10</span>)</span>
<span id="cb22-5"><a href="#cb22-5" aria-hidden="true" tabindex="-1"></a><span class="fu">current_figure</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="19">
<p><img src="makie_plotting_files/figure-html/cell-19-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>As an example, this shows how to add the tangent line to a graph. The slope of the tangent line being computed by <code>ForwardDiff.derivative</code>.</p>
<div class="cell" data-execution_count="19">
<div class="sourceCode cell-code" id="cb23"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> <span class="bu">ForwardDiff</span></span>
<span id="cb23-2"><a href="#cb23-2" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x) <span class="op">=</span> x<span class="op">^</span>x</span>
<span id="cb23-3"><a href="#cb23-3" aria-hidden="true" tabindex="-1"></a>a, b<span class="op">=</span> <span class="fl">0</span>, <span class="fl">2</span></span>
<span id="cb23-4"><a href="#cb23-4" aria-hidden="true" tabindex="-1"></a>c <span class="op">=</span> <span class="fl">0.5</span></span>
<span id="cb23-5"><a href="#cb23-5" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> <span class="fu">range</span>(a, b, length<span class="op">=</span><span class="fl">200</span>)</span>
<span id="cb23-6"><a href="#cb23-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb23-7"><a href="#cb23-7" aria-hidden="true" tabindex="-1"></a><span class="fu">tl</span>(x) <span class="op">=</span> <span class="fu">f</span>(c) <span class="op">+</span> ForwardDiff.<span class="fu">derivative</span>(f, c) <span class="op">*</span> (x<span class="op">-</span>c)</span>
<span id="cb23-8"><a href="#cb23-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb23-9"><a href="#cb23-9" aria-hidden="true" tabindex="-1"></a><span class="fu">lines</span>(xs, <span class="fu">f</span>.(xs))</span>
<span id="cb23-10"><a href="#cb23-10" aria-hidden="true" tabindex="-1"></a><span class="fu">lines!</span>(xs, <span class="fu">tl</span>.(xs), color<span class="op">=:</span>blue)</span>
<span id="cb23-11"><a href="#cb23-11" aria-hidden="true" tabindex="-1"></a><span class="fu">current_figure</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="20">
<p><img src="makie_plotting_files/figure-html/cell-20-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>This example, modified from a <a href="https://discourse.julialang.org/t/how-to-plot-step-functions-x-correctly-in-julia/84087/5">discourse</a> post by user <code>@rafael.guerra</code>, shows how to plot a step function (<code>floor</code>) using <code>NaN</code>s to create line breaks. The marker colors set for <code>scatter!</code> use <code>:white</code> to match the background color.</p>
<div class="cell" data-execution_count="20">
<div class="sourceCode cell-code" id="cb24"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a>x <span class="op">=</span> <span class="op">-</span><span class="fl">5</span><span class="op">:</span><span class="fl">5</span></span>
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a>δ <span class="op">=</span> <span class="fl">5</span><span class="fu">eps</span>() <span class="co"># for rounding purposes; our interval is [i,i+1) ≈ [i, i+1-δ]</span></span>
<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a>xx <span class="op">=</span> <span class="dt">Float64</span>[]</span>
<span id="cb24-4"><a href="#cb24-4" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> i <span class="op"></span> x[<span class="fl">1</span><span class="op">:</span><span class="kw">end</span><span class="op">-</span><span class="fl">1</span>]</span>
<span id="cb24-5"><a href="#cb24-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">append!</span>(xx, (i, i<span class="op">+</span><span class="fl">1</span> <span class="op">-</span> δ, <span class="cn">NaN</span>))</span>
<span id="cb24-6"><a href="#cb24-6" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
<span id="cb24-7"><a href="#cb24-7" aria-hidden="true" tabindex="-1"></a>yy <span class="op">=</span> <span class="fu">floor</span>.(xx)</span>
<span id="cb24-8"><a href="#cb24-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb24-9"><a href="#cb24-9" aria-hidden="true" tabindex="-1"></a><span class="fu">lines</span>(xx, yy)</span>
<span id="cb24-10"><a href="#cb24-10" aria-hidden="true" tabindex="-1"></a><span class="fu">scatter!</span>(xx, yy, color<span class="op">=</span><span class="fu">repeat</span>([<span class="op">:</span>black, <span class="op">:</span>white, <span class="op">:</span>white], <span class="fu">length</span>(xx)<span class="op">÷</span><span class="fl">3</span>))</span>
<span id="cb24-11"><a href="#cb24-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb24-12"><a href="#cb24-12" aria-hidden="true" tabindex="-1"></a><span class="fu">current_figure</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="21">
<p><img src="makie_plotting_files/figure-html/cell-21-output-1.png" class="img-fluid"></p>
</div>
</div>
</section>
</section>
<section id="text-annotations" class="level3" data-number="65.3.2">
<h3 data-number="65.3.2" class="anchored" data-anchor-id="text-annotations"><span class="header-section-number">65.3.2</span> Text (<code>annotations</code>)</h3>
<p>Text can be placed at a point, as a marker is. To place text, the desired text and a position need to be specified along with any adjustments to the default attributes.</p>
<p>For example:</p>
<div class="cell" data-execution_count="21">
<div class="sourceCode cell-code" id="cb25"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> <span class="fl">1</span><span class="op">:</span><span class="fl">5</span></span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a>pts <span class="op">=</span> <span class="fu">Point2</span>.(xs, xs)</span>
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a><span class="fu">scatter</span>(pts)</span>
<span id="cb25-4"><a href="#cb25-4" aria-hidden="true" tabindex="-1"></a><span class="fu">annotations!</span>(<span class="st">"Point "</span> <span class="op">.*</span> <span class="fu">string</span>.(xs), pts;</span>
<span id="cb25-5"><a href="#cb25-5" aria-hidden="true" tabindex="-1"></a> textsize <span class="op">=</span> <span class="fl">50</span> <span class="op">.-</span> <span class="fl">2</span><span class="op">*</span>xs,</span>
<span id="cb25-6"><a href="#cb25-6" aria-hidden="true" tabindex="-1"></a> rotation <span class="op">=</span> <span class="fl">2</span>pi <span class="op">./</span> xs)</span>
<span id="cb25-7"><a href="#cb25-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb25-8"><a href="#cb25-8" aria-hidden="true" tabindex="-1"></a><span class="fu">current_figure</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="22">
<p><img src="makie_plotting_files/figure-html/cell-22-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>The graphic shows that <code>textsize</code> adjusts the displayed size and <code>rotation</code> adjusts the orientation. (The graphic also shows a need to manually override the limits of the <code>y</code> axis, as the <code>Point 5</code> is chopped off; the <code>ylims!</code> function to do so will be shown later.)</p>
<p>Attributes for <code>text</code>, among many others, include:</p>
<ul>
<li><code>align</code> Specify the text alignment through <code>(:pos, :pos)</code>, where <code>:pos</code> can be <code>:left</code>, <code>:center</code>, or <code>:right</code>.</li>
<li><code>rotation</code> to indicate how the text is to be rotated</li>
<li><code>textsize</code> the font point size for the text</li>
<li><code>font</code> to indicate the desired font</li>
</ul>
<section id="line-attributes" class="level4">
<h4 class="anchored" data-anchor-id="line-attributes">Line attributes</h4>
<p>In a previous example, we added the argument <code>color=:blue</code> to the <code>lines!</code> call. This was to set an attribute for the line being drawn. Lines have other attributes that allow different ones to be distinguished, as above where colors indicate the different graphs.</p>
<p>Other attributes can be seen from the help page for <code>lines</code>, and include:</p>
<ul>
<li><code>color</code> set with a symbol, as above, or a string</li>
<li><code>label</code> a label for the line to display in a legend</li>
<li><code>linestyle</code> available styles are set by a symbol, one of <code>:dash</code>, <code>:dot</code>, <code>:dashdot</code>, or <code>:dashdotdot</code>.</li>
<li><code>linewidth</code> width of line</li>
<li><code>transparency</code> the <code>alpha</code> value, a number between <span class="math inline">\(0\)</span> and <span class="math inline">\(1\)</span>, smaller numbers for more transparent.</li>
</ul>
</section>
<section id="simple-legends" class="level4">
<h4 class="anchored" data-anchor-id="simple-legends">Simple legends</h4>
<p>A simple legend displaying labels given to each curve can be produced by <code>axislegend</code>. For example:</p>
<div class="cell" data-execution_count="22">
<div class="sourceCode cell-code" id="cb26"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> <span class="fl">0</span><span class="op">..</span><span class="cn">pi</span></span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a><span class="fu">lines</span>(xs, x <span class="op">-&gt;</span> <span class="fu">sin</span>(x<span class="op">^</span><span class="fl">2</span>), label<span class="op">=</span><span class="st">"sin(x^2)"</span>)</span>
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a><span class="fu">lines!</span>(xs, x <span class="op">-&gt;</span> <span class="fu">sin</span>(x)<span class="op">^</span><span class="fl">2</span>, label <span class="op">=</span> <span class="st">"sin(x)^2"</span>)</span>
<span id="cb26-4"><a href="#cb26-4" aria-hidden="true" tabindex="-1"></a><span class="fu">axislegend</span>()</span>
<span id="cb26-5"><a href="#cb26-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb26-6"><a href="#cb26-6" aria-hidden="true" tabindex="-1"></a><span class="fu">current_figure</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="23">
<p><img src="makie_plotting_files/figure-html/cell-23-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>Later, we will see how to control the placement of a legend within a figure.</p>
</section>
<section id="titles-axis-labels-axis-ticks" class="level4">
<h4 class="anchored" data-anchor-id="titles-axis-labels-axis-ticks">Titles, axis labels, axis ticks</h4>
<p>The basic plots we have seen are of type <code>FigureAxisPlot</code>. The “axis” part controls attributes of the plot such as titles, labels, tick positions, etc. These values can be set in different manners. On construction we can pass values to a named argument <code>axis</code> using a named tuple.</p>
<p>For example:</p>
<div class="cell" data-execution_count="23">
<div class="sourceCode cell-code" id="cb27"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> <span class="fl">0</span><span class="op">..</span><span class="fl">2</span>pi</span>
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a><span class="fu">lines</span>(xs, sin;</span>
<span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></a> axis<span class="op">=</span>(title<span class="op">=</span><span class="st">"Plot of sin(x)"</span>, xlabel<span class="op">=</span><span class="st">"x"</span>, ylabel<span class="op">=</span><span class="st">"sin(x)"</span>)</span>
<span id="cb27-4"><a href="#cb27-4" aria-hidden="true" tabindex="-1"></a> )</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="24">
<p><img src="makie_plotting_files/figure-html/cell-24-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>To access the <code>axis</code> element of a plot <strong>after</strong> the plot is constructed, values can be assigned to the <code>axis</code> property of the <code>FigureAxisPlot</code> object. For example:</p>
<div class="cell" data-execution_count="24">
<div class="sourceCode cell-code" id="cb28"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> <span class="fl">0</span><span class="op">..</span><span class="fl">2</span>pi</span>
<span id="cb28-2"><a href="#cb28-2" aria-hidden="true" tabindex="-1"></a>p <span class="op">=</span> <span class="fu">lines</span>(xs, sin;</span>
<span id="cb28-3"><a href="#cb28-3" aria-hidden="true" tabindex="-1"></a> axis<span class="op">=</span>(title<span class="op">=</span><span class="st">"Plot of sin(x)"</span>, xlabel<span class="op">=</span><span class="st">"x"</span>, ylabel<span class="op">=</span><span class="st">"sin(x)"</span>)</span>
<span id="cb28-4"><a href="#cb28-4" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb28-5"><a href="#cb28-5" aria-hidden="true" tabindex="-1"></a>p.axis.xticks <span class="op">=</span> <span class="fu">MultiplesTicks</span>(<span class="fl">5</span>, <span class="cn">pi</span>, <span class="st">"π"</span>) <span class="co"># label 5 times using `pi`</span></span>
<span id="cb28-6"><a href="#cb28-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb28-7"><a href="#cb28-7" aria-hidden="true" tabindex="-1"></a><span class="fu">current_figure</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="25">
<p><img src="makie_plotting_files/figure-html/cell-25-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>The ticks are most easily set as a collection of values. Above, the <code>MultiplesTicks</code> function was used to label with multiples of <span class="math inline">\(\pi\)</span>.</p>
<p>Later we will discuss how <code>Makie</code> allows for subsequent modification of several parts of the plot (not just the ticks) including the data.</p>
</section>
<section id="figure-resolution-x-and-y-limits" class="level4">
<h4 class="anchored" data-anchor-id="figure-resolution-x-and-y-limits">Figure resolution, <span class="math inline">\(x\)</span> and <span class="math inline">\(y\)</span> limits</h4>
<p>As just mentioned, the basic plots we have seen are of type <code>FigureAxisPlot</code>. The “figure” part can be used to adjust the background color or the resolution. As with attributes for the axis, these too can be passed to a simple constructor:</p>
<div class="cell" data-execution_count="25">
<div class="sourceCode cell-code" id="cb29"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a><span class="fu">lines</span>(xs, sin;</span>
<span id="cb29-2"><a href="#cb29-2" aria-hidden="true" tabindex="-1"></a> axis<span class="op">=</span>(title<span class="op">=</span><span class="st">"Plot of sin(x)"</span>, xlabel<span class="op">=</span><span class="st">"x"</span>, ylabel<span class="op">=</span><span class="st">"sin(x)"</span>),</span>
<span id="cb29-3"><a href="#cb29-3" aria-hidden="true" tabindex="-1"></a> figure<span class="op">=</span>(;resolution<span class="op">=</span>(<span class="fl">300</span>, <span class="fl">300</span>))</span>
<span id="cb29-4"><a href="#cb29-4" aria-hidden="true" tabindex="-1"></a> )</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="26">
<p><img src="makie_plotting_files/figure-html/cell-26-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>The <code>;</code> in the tuple passed to <code>figure</code> is one way to create a <em>named</em> tuple with a single element. Alternatively, <code>(resolution=(300,300), )</code> with a trailing comma could have been used.</p>
<p>To set the limits of the graph there are shorthand functions <code>xlims!</code>, <code>ylims!</code>, and <code>zlims!</code>. This might prove useful if vertical asymptotes are encountered, as in this example:</p>
<div class="cell" data-execution_count="26">
<div class="sourceCode cell-code" id="cb30"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x) <span class="op">=</span> <span class="fl">1</span><span class="op">/</span>x</span>
<span id="cb30-2"><a href="#cb30-2" aria-hidden="true" tabindex="-1"></a>a,b <span class="op">=</span> <span class="op">-</span><span class="fl">1</span>, <span class="fl">1</span></span>
<span id="cb30-3"><a href="#cb30-3" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> <span class="fu">range</span>(<span class="op">-</span><span class="fl">1</span>, <span class="fl">1</span>, length<span class="op">=</span><span class="fl">200</span>)</span>
<span id="cb30-4"><a href="#cb30-4" aria-hidden="true" tabindex="-1"></a><span class="fu">lines</span>(xs, <span class="fu">f</span>.(xs))</span>
<span id="cb30-5"><a href="#cb30-5" aria-hidden="true" tabindex="-1"></a><span class="fu">ylims!</span>(<span class="op">-</span><span class="fl">10</span>, <span class="fl">10</span>)</span>
<span id="cb30-6"><a href="#cb30-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb30-7"><a href="#cb30-7" aria-hidden="true" tabindex="-1"></a><span class="fu">current_figure</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="27">
<p><img src="makie_plotting_files/figure-html/cell-27-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>This still leaves the artifact due to the vertical asymptote at <span class="math inline">\(0\)</span> having different values from the left and the right.</p>
</section>
</section>
<section id="plots-of-parametric-functions" class="level3" data-number="65.3.3">
<h3 data-number="65.3.3" class="anchored" data-anchor-id="plots-of-parametric-functions"><span class="header-section-number">65.3.3</span> Plots of parametric functions</h3>
<p>A space curve is a plot of a function <span class="math inline">\(f:R^2 \rightarrow R\)</span> or <span class="math inline">\(f:R^3 \rightarrow R\)</span>.</p>
<p>To construct a curve from a set of points, we have a similar pattern in both <span class="math inline">\(2\)</span> and <span class="math inline">\(3\)</span> dimensions:</p>
<div class="cell" data-execution_count="27">
<div class="sourceCode cell-code" id="cb31"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a><span class="fu">r</span>(t) <span class="op">=</span> [<span class="fu">sin</span>(<span class="fl">2</span>t), <span class="fu">cos</span>(<span class="fl">3</span>t)]</span>
<span id="cb31-2"><a href="#cb31-2" aria-hidden="true" tabindex="-1"></a>ts <span class="op">=</span> <span class="fu">range</span>(<span class="fl">0</span>, <span class="fl">2</span>pi, length<span class="op">=</span><span class="fl">200</span>)</span>
<span id="cb31-3"><a href="#cb31-3" aria-hidden="true" tabindex="-1"></a>pts <span class="op">=</span> <span class="fu">Point2</span>.(<span class="fu">r</span>.(ts)) <span class="co"># or (Point2∘r).(ts)</span></span>
<span id="cb31-4"><a href="#cb31-4" aria-hidden="true" tabindex="-1"></a><span class="fu">lines</span>(pts)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="28">
<p><img src="makie_plotting_files/figure-html/cell-28-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>Or</p>
<div class="cell" data-execution_count="28">
<div class="sourceCode cell-code" id="cb32"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a><span class="fu">r</span>(t) <span class="op">=</span> [<span class="fu">sin</span>(<span class="fl">2</span>t), <span class="fu">cos</span>(<span class="fl">3</span>t), t]</span>
<span id="cb32-2"><a href="#cb32-2" aria-hidden="true" tabindex="-1"></a>ts <span class="op">=</span> <span class="fu">range</span>(<span class="fl">0</span>, <span class="fl">2</span>pi, length<span class="op">=</span><span class="fl">200</span>)</span>
<span id="cb32-3"><a href="#cb32-3" aria-hidden="true" tabindex="-1"></a>pts <span class="op">=</span> <span class="fu">Point3</span>.(<span class="fu">r</span>.(ts))</span>
<span id="cb32-4"><a href="#cb32-4" aria-hidden="true" tabindex="-1"></a><span class="fu">lines</span>(pts)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="29">
<p><img src="makie_plotting_files/figure-html/cell-29-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>Alternatively, vectors of the <span class="math inline">\(x\)</span>, <span class="math inline">\(y\)</span>, and <span class="math inline">\(z\)</span> components can be produced and then plotted using the pattern <code>lines(xs, ys)</code> or <code>lines(xs, ys, zs)</code>. For example, using <code>unzip</code>, as above, we might have done the prior example with:</p>
<div class="cell" data-execution_count="29">
<div class="sourceCode cell-code" id="cb33"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a>xs, ys, zs <span class="op">=</span> <span class="fu">unzip</span>(<span class="fu">r</span>.(ts))</span>
<span id="cb33-2"><a href="#cb33-2" aria-hidden="true" tabindex="-1"></a><span class="fu">lines</span>(xs, ys, zs)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="30">
<p><img src="makie_plotting_files/figure-html/cell-30-output-1.png" class="img-fluid"></p>
</div>
</div>
<section id="aspect-ratio" class="level4">
<h4 class="anchored" data-anchor-id="aspect-ratio">Aspect ratio</h4>
<p>A simple plot of a parametrically defined circle will show an ellipse, as the aspect ratio of the <span class="math inline">\(x\)</span> and <span class="math inline">\(y\)</span> axis is not <span class="math inline">\(1\)</span>. To enforce this, we can pass a value of <code>aspect=1</code> to the underlying “Axis” object. For example:</p>
<div class="cell" data-execution_count="30">
<div class="sourceCode cell-code" id="cb34"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a>ts <span class="op">=</span> <span class="fu">range</span>(<span class="fl">0</span>, <span class="fl">2</span>pi, length<span class="op">=</span><span class="fl">100</span>)</span>
<span id="cb34-2"><a href="#cb34-2" aria-hidden="true" tabindex="-1"></a>xs, ys <span class="op">=</span> <span class="fu">sin</span>.(ts), <span class="fu">cos</span>.(ts)</span>
<span id="cb34-3"><a href="#cb34-3" aria-hidden="true" tabindex="-1"></a><span class="fu">lines</span>(xs, ys; axis<span class="op">=</span>(; aspect <span class="op">=</span> <span class="fl">1</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="31">
<p><img src="makie_plotting_files/figure-html/cell-31-output-1.png" class="img-fluid"></p>
</div>
</div>
</section>
<section id="tangent-vectors-arrows" class="level4">
<h4 class="anchored" data-anchor-id="tangent-vectors-arrows">Tangent vectors (<code>arrows</code>)</h4>
<p>A tangent vector along a curve can be drawn quite easily using the <code>arrows</code> function. There are different interfaces for <code>arrows</code>, but we show the one which uses a vector of positions and a vector of “vectors”. For the latter, we utilize the <code>derivative</code> function from <code>ForwardDiff</code>:</p>
<div class="cell" data-execution_count="31">
<div class="sourceCode cell-code" id="cb35"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a><span class="fu">r</span>(t) <span class="op">=</span> [<span class="fu">sin</span>(t), <span class="fu">cos</span>(t)] <span class="co"># vector, not tuple</span></span>
<span id="cb35-2"><a href="#cb35-2" aria-hidden="true" tabindex="-1"></a>ts <span class="op">=</span> <span class="fu">range</span>(<span class="fl">0</span>, <span class="fl">4</span>pi, length<span class="op">=</span><span class="fl">200</span>)</span>
<span id="cb35-3"><a href="#cb35-3" aria-hidden="true" tabindex="-1"></a><span class="fu">lines</span>(<span class="fu">Point2</span>.(<span class="fu">r</span>.(ts)))</span>
<span id="cb35-4"><a href="#cb35-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb35-5"><a href="#cb35-5" aria-hidden="true" tabindex="-1"></a>nts <span class="op">=</span> <span class="fl">0</span><span class="op">:</span><span class="cn">pi</span><span class="op">/</span><span class="fl">4</span><span class="op">:</span><span class="fl">2</span>pi</span>
<span id="cb35-6"><a href="#cb35-6" aria-hidden="true" tabindex="-1"></a>us <span class="op">=</span> <span class="fu">r</span>.(nts)</span>
<span id="cb35-7"><a href="#cb35-7" aria-hidden="true" tabindex="-1"></a>dus <span class="op">=</span> ForwardDiff.<span class="fu">derivative</span>.(r, nts)</span>
<span id="cb35-8"><a href="#cb35-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb35-9"><a href="#cb35-9" aria-hidden="true" tabindex="-1"></a><span class="fu">arrows!</span>(<span class="fu">Point2</span>.(us), <span class="fu">Point2</span>.(dus))</span>
<span id="cb35-10"><a href="#cb35-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb35-11"><a href="#cb35-11" aria-hidden="true" tabindex="-1"></a><span class="fu">current_figure</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="32">
<p><img src="makie_plotting_files/figure-html/cell-32-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>In 3 dimensions the differences are minor:</p>
<div class="cell" data-execution_count="32">
<div class="sourceCode cell-code" id="cb36"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb36-1"><a href="#cb36-1" aria-hidden="true" tabindex="-1"></a><span class="fu">r</span>(t) <span class="op">=</span> [<span class="fu">sin</span>(t), <span class="fu">cos</span>(t), t] <span class="co"># vector, not tuple</span></span>
<span id="cb36-2"><a href="#cb36-2" aria-hidden="true" tabindex="-1"></a>ts <span class="op">=</span> <span class="fu">range</span>(<span class="fl">0</span>, <span class="fl">4</span>pi, length<span class="op">=</span><span class="fl">200</span>)</span>
<span id="cb36-3"><a href="#cb36-3" aria-hidden="true" tabindex="-1"></a><span class="fu">lines</span>(<span class="fu">Point3</span>.(<span class="fu">r</span>.(ts)))</span>
<span id="cb36-4"><a href="#cb36-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb36-5"><a href="#cb36-5" aria-hidden="true" tabindex="-1"></a>nts <span class="op">=</span> <span class="fl">0</span><span class="op">:</span><span class="cn">pi</span><span class="op">/</span><span class="fl">2</span><span class="op">:</span>(<span class="fl">4</span>pi<span class="op">-</span><span class="cn">pi</span><span class="op">/</span><span class="fl">2</span>)</span>
<span id="cb36-6"><a href="#cb36-6" aria-hidden="true" tabindex="-1"></a>us <span class="op">=</span> <span class="fu">r</span>.(nts)</span>
<span id="cb36-7"><a href="#cb36-7" aria-hidden="true" tabindex="-1"></a>dus <span class="op">=</span> ForwardDiff.<span class="fu">derivative</span>.(r, nts)</span>
<span id="cb36-8"><a href="#cb36-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb36-9"><a href="#cb36-9" aria-hidden="true" tabindex="-1"></a><span class="fu">arrows!</span>(<span class="fu">Point3</span>.(us), <span class="fu">Point3</span>.(dus))</span>
<span id="cb36-10"><a href="#cb36-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb36-11"><a href="#cb36-11" aria-hidden="true" tabindex="-1"></a><span class="fu">current_figure</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="33">
<p><img src="makie_plotting_files/figure-html/cell-33-output-1.png" class="img-fluid"></p>
</div>
</div>
</section>
<section id="arrow-attributes" class="level4">
<h4 class="anchored" data-anchor-id="arrow-attributes">Arrow attributes</h4>
<p>Attributes for <code>arrows</code> include</p>
<ul>
<li><code>arrowsize</code> to adjust the size</li>
<li><code>lengthscale</code> to scale the size</li>
<li><code>arrowcolor</code> to set the color</li>
<li><code>arrowhead</code> to adjust the head</li>
<li><code>arrowtail</code> to adjust the tail</li>
</ul>
</section>
</section>
</section>
<section id="surfaces" class="level2" data-number="65.4">
<h2 data-number="65.4" class="anchored" data-anchor-id="surfaces"><span class="header-section-number">65.4</span> Surfaces</h2>
<p>Plots of surfaces in <span class="math inline">\(3\)</span> dimensions are useful to help understand the behavior of multivariate functions.</p>
<section id="surfaces-defined-through-zfxy" class="level4">
<h4 class="anchored" data-anchor-id="surfaces-defined-through-zfxy">Surfaces defined through <span class="math inline">\(z=f(x,y)\)</span></h4>
<p>The “<code>peaks</code>” function defined below has a few prominent peaks:</p>
<div class="cell" data-execution_count="33">
<div class="sourceCode cell-code" id="cb37"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb37-1"><a href="#cb37-1" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">peaks</span>(x, y)</span>
<span id="cb37-2"><a href="#cb37-2" aria-hidden="true" tabindex="-1"></a> p <span class="op">=</span> <span class="fl">3</span><span class="fu">*</span>(<span class="fl">1</span><span class="op">-</span>x)<span class="op">^</span><span class="fl">2</span><span class="fu">*exp</span>(<span class="op">-</span>x<span class="op">^</span><span class="fl">2</span> <span class="op">-</span> (y<span class="op">+</span><span class="fl">1</span>)<span class="op">^</span><span class="fl">2</span>)</span>
<span id="cb37-3"><a href="#cb37-3" aria-hidden="true" tabindex="-1"></a> p <span class="op">-=</span> <span class="fl">10</span>(x<span class="op">/</span><span class="fl">5</span><span class="op">-</span>x<span class="op">^</span><span class="fl">3</span><span class="op">-</span>y<span class="op">^</span><span class="fl">5</span>)<span class="fu">*exp</span>(<span class="op">-</span>x<span class="op">^</span><span class="fl">2</span><span class="op">-</span>y<span class="op">^</span><span class="fl">2</span>)</span>
<span id="cb37-4"><a href="#cb37-4" aria-hidden="true" tabindex="-1"></a> p <span class="op">-=</span> <span class="fl">1</span><span class="op">/</span><span class="fl">3</span><span class="fu">*exp</span>(<span class="fu">-</span>(x<span class="op">+</span><span class="fl">1</span>)<span class="op">^</span><span class="fl">2</span><span class="op">-</span>y<span class="op">^</span><span class="fl">2</span>)</span>
<span id="cb37-5"><a href="#cb37-5" aria-hidden="true" tabindex="-1"></a> p</span>
<span id="cb37-6"><a href="#cb37-6" aria-hidden="true" tabindex="-1"></a><span class="kw">end</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="34">
<pre><code>peaks (generic function with 1 method)</code></pre>
</div>
</div>
<p>Here we see how <code>peaks</code> can be visualized over the region <span class="math inline">\([-5,5]\times[-5,5]\)</span>:</p>
<div class="cell" data-execution_count="34">
<div class="sourceCode cell-code" id="cb39"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb39-1"><a href="#cb39-1" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> ys <span class="op">=</span> <span class="fu">range</span>(<span class="op">-</span><span class="fl">5</span>, <span class="fl">5</span>, length<span class="op">=</span><span class="fl">25</span>)</span>
<span id="cb39-2"><a href="#cb39-2" aria-hidden="true" tabindex="-1"></a><span class="fu">surface</span>(xs, ys, peaks)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="35">
<p><img src="makie_plotting_files/figure-html/cell-35-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>The calling pattern <code>surface(xs, ys, f)</code> implies a rectangular grid over the <span class="math inline">\(x\)</span>-<span class="math inline">\(y\)</span> plane defined by <code>xs</code> and <code>ys</code> with <span class="math inline">\(z\)</span> values given by <span class="math inline">\(f(x,y)\)</span>.</p>
<p>Alternatively a “matrix” of <span class="math inline">\(z\)</span> values can be specified. For a function <code>f</code>, this is conveniently generated by the pattern <code>f.(xs, ys')</code>, the <code>'</code> being important to get a matrix of all <span class="math inline">\(x\)</span>-<span class="math inline">\(y\)</span> pairs through <code>Julia</code>s broadcasting syntax.</p>
<div class="cell" data-execution_count="35">
<div class="sourceCode cell-code" id="cb40"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb40-1"><a href="#cb40-1" aria-hidden="true" tabindex="-1"></a>zs <span class="op">=</span> <span class="fu">peaks</span>.(xs, ys<span class="op">'</span>)</span>
<span id="cb40-2"><a href="#cb40-2" aria-hidden="true" tabindex="-1"></a><span class="fu">surface</span>(xs, ys, zs)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="36">
<p><img src="makie_plotting_files/figure-html/cell-36-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>To see how this graph is constructed, the points <span class="math inline">\((x,y,f(x,y))\)</span> are plotted over the grid and displayed.</p>
<p>Here we downsample to illustrate:</p>
<div class="cell" data-execution_count="36">
<div class="sourceCode cell-code" id="cb41"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb41-1"><a href="#cb41-1" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> ys <span class="op">=</span> <span class="fu">range</span>(<span class="op">-</span><span class="fl">5</span>, <span class="fl">5</span>, length<span class="op">=</span><span class="fl">5</span>)</span>
<span id="cb41-2"><a href="#cb41-2" aria-hidden="true" tabindex="-1"></a>pts <span class="op">=</span> [<span class="fu">Point3</span>(x, y, <span class="fu">peaks</span>(x,y)) for x <span class="kw">in</span> xs for y <span class="kw">in</span> ys]</span>
<span id="cb41-3"><a href="#cb41-3" aria-hidden="true" tabindex="-1"></a><span class="fu">scatter</span>(pts, markersize<span class="op">=</span><span class="fl">25</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="37">
<p><img src="makie_plotting_files/figure-html/cell-37-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>These points are then connected. The <code>wireframe</code> function illustrates just the frame:</p>
<div class="cell" data-execution_count="37">
<div class="sourceCode cell-code" id="cb42"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb42-1"><a href="#cb42-1" aria-hidden="true" tabindex="-1"></a><span class="fu">wireframe</span>(xs, ys, <span class="fu">peaks</span>.(xs, ys<span class="op">'</span>); linewidth<span class="op">=</span><span class="fl">5</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="38">
<p><img src="makie_plotting_files/figure-html/cell-38-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>The <code>surface</code> call triangulates the frame and fills in the shading:</p>
<div class="cell" data-execution_count="38">
<div class="sourceCode cell-code" id="cb43"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb43-1"><a href="#cb43-1" aria-hidden="true" tabindex="-1"></a><span class="fu">surface!</span>(xs, ys, <span class="fu">peaks</span>.(xs, ys<span class="op">'</span>))</span>
<span id="cb43-2"><a href="#cb43-2" aria-hidden="true" tabindex="-1"></a><span class="fu">current_figure</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="39">
<p><img src="makie_plotting_files/figure-html/cell-39-output-1.png" class="img-fluid"></p>
</div>
</div>
</section>
<section id="parametrically-defined-surfaces" class="level4">
<h4 class="anchored" data-anchor-id="parametrically-defined-surfaces">Parametrically defined surfaces</h4>
<p>A surface may be parametrically defined through a function <span class="math inline">\(r(u,v) = (x(u,v), y(u,v), z(u,v))\)</span>. For example, the surface generated by <span class="math inline">\(z=f(x,y)\)</span> is of the form with <span class="math inline">\(r(u,v) = (u,v,f(u,v))\)</span>.</p>
<p>The <code>surface</code> function and the <code>wireframe</code> function can be used to display such surfaces. In previous usages, the <code>x</code> and <code>y</code> values were vectors from which a 2-dimensional grid is formed. For parametric surfaces, a grid for the <code>x</code> and <code>y</code> values must be generated. This function will do so:</p>
<div class="cell" data-execution_count="39">
<div class="sourceCode cell-code" id="cb44"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb44-1"><a href="#cb44-1" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">parametric_grid</span>(us, vs, r)</span>
<span id="cb44-2"><a href="#cb44-2" aria-hidden="true" tabindex="-1"></a> n,m <span class="op">=</span> <span class="fu">length</span>(us), <span class="fu">length</span>(vs)</span>
<span id="cb44-3"><a href="#cb44-3" aria-hidden="true" tabindex="-1"></a> xs, ys, zs <span class="op">=</span> <span class="fu">zeros</span>(n,m), <span class="fu">zeros</span>(n,m), <span class="fu">zeros</span>(n,m)</span>
<span id="cb44-4"><a href="#cb44-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> (i, uᵢ) <span class="kw">in</span> <span class="fu">pairs</span>(us)</span>
<span id="cb44-5"><a href="#cb44-5" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> (j, vⱼ) <span class="kw">in</span> <span class="fu">pairs</span>(vs)</span>
<span id="cb44-6"><a href="#cb44-6" aria-hidden="true" tabindex="-1"></a> x,y,z <span class="op">=</span> <span class="fu">r</span>(uᵢ, vⱼ)</span>
<span id="cb44-7"><a href="#cb44-7" aria-hidden="true" tabindex="-1"></a> xs[i,j] <span class="op">=</span> x</span>
<span id="cb44-8"><a href="#cb44-8" aria-hidden="true" tabindex="-1"></a> ys[i,j] <span class="op">=</span> y</span>
<span id="cb44-9"><a href="#cb44-9" aria-hidden="true" tabindex="-1"></a> zs[i,j] <span class="op">=</span> z</span>
<span id="cb44-10"><a href="#cb44-10" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb44-11"><a href="#cb44-11" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb44-12"><a href="#cb44-12" aria-hidden="true" tabindex="-1"></a> (xs, ys, zs)</span>
<span id="cb44-13"><a href="#cb44-13" aria-hidden="true" tabindex="-1"></a><span class="kw">end</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="40">
<pre><code>parametric_grid (generic function with 1 method)</code></pre>
</div>
</div>
<p>With the data suitably massaged, we can directly plot either a <code>surface</code> or <code>wireframe</code> plot.</p>
<hr>
<p>As an aside, The above can be done more campactly with nested list comprehensions:</p>
<pre><code>xs, ys, zs = [[pt[i] for pt in r.(us, vs')] for i in 1:3]</code></pre>
<p>Or using the <code>unzip</code> function directly after broadcasting:</p>
<pre><code>xs, ys, zs = unzip(r.(us, vs'))</code></pre>
<hr>
<p>For example, a sphere can be parameterized by <span class="math inline">\(r(u,v) = (\sin(u)\cos(v), \sin(u)\sin(v), \cos(u))\)</span> and visualized through:</p>
<div class="cell" data-execution_count="40">
<div class="sourceCode cell-code" id="cb48"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb48-1"><a href="#cb48-1" aria-hidden="true" tabindex="-1"></a><span class="fu">r</span>(u,v) <span class="op">=</span> [<span class="fu">sin</span>(u)<span class="fu">*cos</span>(v), <span class="fu">sin</span>(u)<span class="fu">*sin</span>(v), <span class="fu">cos</span>(u)]</span>
<span id="cb48-2"><a href="#cb48-2" aria-hidden="true" tabindex="-1"></a>us <span class="op">=</span> <span class="fu">range</span>(<span class="fl">0</span>, <span class="cn">pi</span>, length<span class="op">=</span><span class="fl">25</span>)</span>
<span id="cb48-3"><a href="#cb48-3" aria-hidden="true" tabindex="-1"></a>vs <span class="op">=</span> <span class="fu">range</span>(<span class="fl">0</span>, <span class="cn">pi</span><span class="op">/</span><span class="fl">2</span>, length<span class="op">=</span><span class="fl">25</span>)</span>
<span id="cb48-4"><a href="#cb48-4" aria-hidden="true" tabindex="-1"></a>xs, ys, zs <span class="op">=</span> <span class="fu">parametric_grid</span>(us, vs, r)</span>
<span id="cb48-5"><a href="#cb48-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb48-6"><a href="#cb48-6" aria-hidden="true" tabindex="-1"></a><span class="fu">surface</span>(xs, ys, zs)</span>
<span id="cb48-7"><a href="#cb48-7" aria-hidden="true" tabindex="-1"></a><span class="fu">wireframe!</span>(xs, ys, zs)</span>
<span id="cb48-8"><a href="#cb48-8" aria-hidden="true" tabindex="-1"></a><span class="fu">current_figure</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="41">
<p><img src="makie_plotting_files/figure-html/cell-41-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>A surface of revolution for <span class="math inline">\(g(u)\)</span> revolved about the <span class="math inline">\(z\)</span> axis can be visualized through:</p>
<div class="cell" data-execution_count="41">
<div class="sourceCode cell-code" id="cb49"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb49-1"><a href="#cb49-1" aria-hidden="true" tabindex="-1"></a><span class="fu">g</span>(u) <span class="op">=</span> u<span class="op">^</span><span class="fl">2</span> <span class="op">*</span> <span class="fu">exp</span>(<span class="op">-</span>u)</span>
<span id="cb49-2"><a href="#cb49-2" aria-hidden="true" tabindex="-1"></a><span class="fu">r</span>(u,v) <span class="op">=</span> (<span class="fu">g</span>(u)<span class="fu">*sin</span>(v), <span class="fu">g</span>(u)<span class="fu">*cos</span>(v), u)</span>
<span id="cb49-3"><a href="#cb49-3" aria-hidden="true" tabindex="-1"></a>us <span class="op">=</span> <span class="fu">range</span>(<span class="fl">0</span>, <span class="fl">3</span>, length<span class="op">=</span><span class="fl">10</span>)</span>
<span id="cb49-4"><a href="#cb49-4" aria-hidden="true" tabindex="-1"></a>vs <span class="op">=</span> <span class="fu">range</span>(<span class="fl">0</span>, <span class="fl">2</span>pi, length<span class="op">=</span><span class="fl">10</span>)</span>
<span id="cb49-5"><a href="#cb49-5" aria-hidden="true" tabindex="-1"></a>xs, ys, zs <span class="op">=</span> <span class="fu">parametric_grid</span>(us, vs, r)</span>
<span id="cb49-6"><a href="#cb49-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb49-7"><a href="#cb49-7" aria-hidden="true" tabindex="-1"></a><span class="fu">surface</span>(xs, ys, zs)</span>
<span id="cb49-8"><a href="#cb49-8" aria-hidden="true" tabindex="-1"></a><span class="fu">wireframe!</span>(xs, ys, zs)</span>
<span id="cb49-9"><a href="#cb49-9" aria-hidden="true" tabindex="-1"></a><span class="fu">current_figure</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="42">
<p><img src="makie_plotting_files/figure-html/cell-42-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>A torus with big radius <span class="math inline">\(2\)</span> and inner radius <span class="math inline">\(1/2\)</span> can be visualized as follows</p>
<div class="cell" data-execution_count="42">
<div class="sourceCode cell-code" id="cb50"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb50-1"><a href="#cb50-1" aria-hidden="true" tabindex="-1"></a>r1, r2 <span class="op">=</span> <span class="fl">2</span>, <span class="fl">1</span><span class="op">/</span><span class="fl">2</span></span>
<span id="cb50-2"><a href="#cb50-2" aria-hidden="true" tabindex="-1"></a><span class="fu">r</span>(u,v) <span class="op">=</span> ((r1 <span class="op">+</span> <span class="fu">r2*cos</span>(v))<span class="fu">*cos</span>(u), (r1 <span class="op">+</span> <span class="fu">r2*cos</span>(v))<span class="fu">*sin</span>(u), <span class="fu">r2*sin</span>(v))</span>
<span id="cb50-3"><a href="#cb50-3" aria-hidden="true" tabindex="-1"></a>us <span class="op">=</span> vs <span class="op">=</span> <span class="fu">range</span>(<span class="fl">0</span>, <span class="fl">2</span>pi, length<span class="op">=</span><span class="fl">25</span>)</span>
<span id="cb50-4"><a href="#cb50-4" aria-hidden="true" tabindex="-1"></a>xs, ys, zs <span class="op">=</span> <span class="fu">parametric_grid</span>(us, vs, r)</span>
<span id="cb50-5"><a href="#cb50-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb50-6"><a href="#cb50-6" aria-hidden="true" tabindex="-1"></a><span class="fu">surface</span>(xs, ys, zs)</span>
<span id="cb50-7"><a href="#cb50-7" aria-hidden="true" tabindex="-1"></a><span class="fu">wireframe!</span>(xs, ys, zs)</span>
<span id="cb50-8"><a href="#cb50-8" aria-hidden="true" tabindex="-1"></a><span class="fu">current_figure</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="43">
<p><img src="makie_plotting_files/figure-html/cell-43-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>A Möbius strip can be produced with:</p>
<div class="cell" data-execution_count="43">
<div class="sourceCode cell-code" id="cb51"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb51-1"><a href="#cb51-1" aria-hidden="true" tabindex="-1"></a>ws <span class="op">=</span> <span class="fu">range</span>(<span class="op">-</span><span class="fl">1</span><span class="op">/</span><span class="fl">4</span>, <span class="fl">1</span><span class="op">/</span><span class="fl">4</span>, length<span class="op">=</span><span class="fl">8</span>)</span>
<span id="cb51-2"><a href="#cb51-2" aria-hidden="true" tabindex="-1"></a>thetas <span class="op">=</span> <span class="fu">range</span>(<span class="fl">0</span>, <span class="fl">2</span>pi, length<span class="op">=</span><span class="fl">30</span>)</span>
<span id="cb51-3"><a href="#cb51-3" aria-hidden="true" tabindex="-1"></a><span class="fu">r</span>(w, θ) <span class="op">=</span> ((<span class="fl">1</span><span class="fu">+w*cos</span><span class="op">/</span><span class="fl">2</span>))<span class="fu">*cos</span>(θ), (<span class="fl">1</span><span class="fu">+w*cos</span><span class="op">/</span><span class="fl">2</span>))<span class="fu">*sin</span>(θ), <span class="fu">w*sin</span><span class="op">/</span><span class="fl">2</span>))</span>
<span id="cb51-4"><a href="#cb51-4" aria-hidden="true" tabindex="-1"></a>xs, ys, zs <span class="op">=</span> <span class="fu">parametric_grid</span>(ws, thetas, r)</span>
<span id="cb51-5"><a href="#cb51-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb51-6"><a href="#cb51-6" aria-hidden="true" tabindex="-1"></a><span class="fu">surface</span>(xs, ys, zs)</span>
<span id="cb51-7"><a href="#cb51-7" aria-hidden="true" tabindex="-1"></a><span class="fu">wireframe!</span>(xs, ys, zs)</span>
<span id="cb51-8"><a href="#cb51-8" aria-hidden="true" tabindex="-1"></a><span class="fu">current_figure</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="44">
<p><img src="makie_plotting_files/figure-html/cell-44-output-1.png" class="img-fluid"></p>
</div>
</div>
</section>
</section>
<section id="contour-plots-contour-contourf-heatmap" class="level2" data-number="65.5">
<h2 data-number="65.5" class="anchored" data-anchor-id="contour-plots-contour-contourf-heatmap"><span class="header-section-number">65.5</span> Contour plots (<code>contour</code>, <code>contourf</code>, <code>heatmap</code>)</h2>
<p>For a function <span class="math inline">\(z = f(x,y)\)</span> an alternative to a surface plot, is a contour plot. That is, for different values of <span class="math inline">\(c\)</span> the level curves <span class="math inline">\(f(x,y)=c\)</span> are drawn.</p>
<p>For a function <span class="math inline">\(f(x,y)\)</span>, the syntax for generating a contour plot follows that for <code>surface</code>.</p>
<p>For example, using the <code>peaks</code> function, previously defined, we have a contour plot over the region <span class="math inline">\([-5,5]\times[-5,5]\)</span> is generated through:</p>
<div class="cell" data-execution_count="44">
<div class="sourceCode cell-code" id="cb52"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb52-1"><a href="#cb52-1" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> ys <span class="op">=</span> <span class="fu">range</span>(<span class="op">-</span><span class="fl">5</span>, <span class="fl">5</span>, length<span class="op">=</span><span class="fl">100</span>)</span>
<span id="cb52-2"><a href="#cb52-2" aria-hidden="true" tabindex="-1"></a><span class="fu">contour</span>(xs, ys, peaks)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="45">
<p><img src="makie_plotting_files/figure-html/cell-45-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>The default of <span class="math inline">\(5\)</span> levels can be adjusted using the <code>levels</code> keyword:</p>
<div class="cell" data-execution_count="45">
<div class="sourceCode cell-code" id="cb53"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb53-1"><a href="#cb53-1" aria-hidden="true" tabindex="-1"></a><span class="fu">contour</span>(xs, ys, peaks; levels <span class="op">=</span> <span class="fl">20</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="46">
<p><img src="makie_plotting_files/figure-html/cell-46-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>The <code>levels</code> argument can also specify precisely what levels are to be drawn.</p>
<p>The contour graph makes identification of peaks and valleys easy as the limits of patterns of nested contour lines.</p>
<p>A <em>filled</em> contour plot is produced by <code>contourf</code>:</p>
<div class="cell" data-execution_count="46">
<div class="sourceCode cell-code" id="cb54"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb54-1"><a href="#cb54-1" aria-hidden="true" tabindex="-1"></a><span class="fu">contourf</span>(xs, ys, peaks)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="47">
<p><img src="makie_plotting_files/figure-html/cell-47-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>A related, but alternative visualization, using color to represent magnitude is a heatmap, produced by the <code>heatmap</code> function. The calling syntax is similar to <code>contour</code> and <code>surface</code>:</p>
<div class="cell" data-execution_count="47">
<div class="sourceCode cell-code" id="cb55"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb55-1"><a href="#cb55-1" aria-hidden="true" tabindex="-1"></a><span class="fu">heatmap</span>(xs, ys, peaks)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="48">
<p><img src="makie_plotting_files/figure-html/cell-48-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>This graph shows peaks and valleys through “hotspots” on the graph.</p>
<p>The <code>MakieGallery</code> package includes an example of a surface plot with both a wireframe and 2D contour graph added. It is replicated here using the <code>peaks</code> function scaled by <span class="math inline">\(5\)</span>.</p>
<p>The function and domain to plot are described by:</p>
<div class="sourceCode cell-code" id="cb56"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb56-1"><a href="#cb56-1" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> ys <span class="op">=</span> <span class="fu">range</span>(<span class="op">-</span><span class="fl">5</span>, <span class="fl">5</span>, length<span class="op">=</span><span class="fl">51</span>)</span>
<span id="cb56-2"><a href="#cb56-2" aria-hidden="true" tabindex="-1"></a>zs <span class="op">=</span> <span class="fu">peaks</span>.(xs, ys<span class="op">'</span>) <span class="op">/</span> <span class="fl">5</span>;</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<p>The <code>zs</code> were generated, as <code>wireframe</code> does not provide the interface for passing a function.</p>
<p>The <code>surface</code> and <code>wireframe</code> are produced as follows. Here we manually create the figure and axis object so that we can set the viewing angle through the <code>elevation</code> argument to the axis object:</p>
<div class="cell" data-execution_count="49">
<div class="sourceCode cell-code" id="cb57"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb57-1"><a href="#cb57-1" aria-hidden="true" tabindex="-1"></a>fig <span class="op">=</span> <span class="fu">Figure</span>()</span>
<span id="cb57-2"><a href="#cb57-2" aria-hidden="true" tabindex="-1"></a>ax3 <span class="op">=</span> <span class="fu">Axis3</span>(fig[<span class="fl">1</span>,<span class="fl">1</span>];</span>
<span id="cb57-3"><a href="#cb57-3" aria-hidden="true" tabindex="-1"></a> elevation<span class="op">=</span><span class="cn">pi</span><span class="op">/</span><span class="fl">9</span>, azimuth<span class="op">=</span><span class="cn">pi</span><span class="op">/</span><span class="fl">16</span>)</span>
<span id="cb57-4"><a href="#cb57-4" aria-hidden="true" tabindex="-1"></a><span class="fu">surface!</span>(ax3, xs, ys, zs)</span>
<span id="cb57-5"><a href="#cb57-5" aria-hidden="true" tabindex="-1"></a><span class="fu">wireframe!</span>(ax3, xs, ys, zs;</span>
<span id="cb57-6"><a href="#cb57-6" aria-hidden="true" tabindex="-1"></a> overdraw <span class="op">=</span> <span class="cn">true</span>, transparency <span class="op">=</span> <span class="cn">true</span>,</span>
<span id="cb57-7"><a href="#cb57-7" aria-hidden="true" tabindex="-1"></a> color <span class="op">=</span> (<span class="op">:</span>black, <span class="fl">0.1</span>))</span>
<span id="cb57-8"><a href="#cb57-8" aria-hidden="true" tabindex="-1"></a><span class="fu">current_figure</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="50">
<p><img src="makie_plotting_files/figure-html/cell-50-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>To add the contour, a simple call via <code>contour!(scene, xs, ys, zs)</code> will place the contour at the <span class="math inline">\(z=0\)</span> level which will make it hard to read. Rather, placing at the “bottom” of the figure is desirable. To identify that the minimum value, is identified (and rounded) and the argument <code>transformation = (:xy, zmin)</code> is passed to <code>contour!</code>:</p>
<div class="cell" data-execution_count="50">
<div class="sourceCode cell-code" id="cb58"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb58-1"><a href="#cb58-1" aria-hidden="true" tabindex="-1"></a>ezs <span class="op">=</span> <span class="fu">extrema</span>(zs)</span>
<span id="cb58-2"><a href="#cb58-2" aria-hidden="true" tabindex="-1"></a>zmin, zmax <span class="op">=</span> <span class="fu">floor</span>(<span class="fu">first</span>(ezs)), <span class="fu">ceil</span>(<span class="fu">last</span>(ezs))</span>
<span id="cb58-3"><a href="#cb58-3" aria-hidden="true" tabindex="-1"></a><span class="fu">contour!</span>(ax3, xs, ys, zs;</span>
<span id="cb58-4"><a href="#cb58-4" aria-hidden="true" tabindex="-1"></a> levels <span class="op">=</span> <span class="fl">15</span>, linewidth <span class="op">=</span> <span class="fl">2</span>,</span>
<span id="cb58-5"><a href="#cb58-5" aria-hidden="true" tabindex="-1"></a> transformation <span class="op">=</span> (<span class="op">:</span>xy, zmin))</span>
<span id="cb58-6"><a href="#cb58-6" aria-hidden="true" tabindex="-1"></a><span class="fu">zlims!</span>(zmin, zmax)</span>
<span id="cb58-7"><a href="#cb58-7" aria-hidden="true" tabindex="-1"></a><span class="fu">current_figure</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="51">
<p><img src="makie_plotting_files/figure-html/cell-51-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>The <code>transformation</code> plot attribute sets the “plane” (one of <code>:xy</code>, <code>:yz</code>, or <code>:xz</code>) at a location, in this example <code>zmin</code>.</p>
<p>The manual construction of a figure and an axis object will be further discussed later.</p>
<section id="three-dimensional-contour-plots" class="level3" data-number="65.5.1">
<h3 data-number="65.5.1" class="anchored" data-anchor-id="three-dimensional-contour-plots"><span class="header-section-number">65.5.1</span> Three dimensional contour plots</h3>
<p>The <code>contour</code> function can also plot <span class="math inline">\(3\)</span>-dimensional contour plots. Concentric spheres, contours of <span class="math inline">\(x^2 + y^2 + z^2 = c\)</span> for <span class="math inline">\(c &gt; 0\)</span> are presented by the following:</p>
<div class="cell" data-execution_count="51">
<div class="sourceCode cell-code" id="cb59"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb59-1"><a href="#cb59-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x,y,z) <span class="op">=</span> x<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> y<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> z<span class="op">^</span><span class="fl">2</span></span>
<span id="cb59-2"><a href="#cb59-2" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> ys <span class="op">=</span> zs <span class="op">=</span> <span class="fu">range</span>(<span class="op">-</span><span class="fl">3</span>, <span class="fl">3</span>, length<span class="op">=</span><span class="fl">100</span>)</span>
<span id="cb59-3"><a href="#cb59-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb59-4"><a href="#cb59-4" aria-hidden="true" tabindex="-1"></a><span class="fu">contour</span>(xs, ys, zs, f)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="52">
<p><img src="makie_plotting_files/figure-html/cell-52-output-1.png" class="img-fluid"></p>
</div>
</div>
</section>
<section id="implicitly-defined-curves-and-surfaces" class="level3" data-number="65.5.2">
<h3 data-number="65.5.2" class="anchored" data-anchor-id="implicitly-defined-curves-and-surfaces"><span class="header-section-number">65.5.2</span> Implicitly defined curves and surfaces</h3>
<p>Suppose <span class="math inline">\(f\)</span> is a scalar-valued function. If <code>f</code> takes two variables for its input, then the equation <span class="math inline">\(f(x,y) = 0\)</span> implicitly defines <span class="math inline">\(y\)</span> as a function of <span class="math inline">\(x\)</span>; <span class="math inline">\(y\)</span> can be visualized <em>locally</em> with a curve. If <span class="math inline">\(f\)</span> takes three variables for its input, then the equation <span class="math inline">\(f(x,y,z)=0\)</span> implicitly defines <span class="math inline">\(z\)</span> as a function of <span class="math inline">\(x\)</span> and <span class="math inline">\(y\)</span>; <span class="math inline">\(z\)</span> can be visualized <em>locally</em> with a surface.</p>
<section id="implicitly-defined-curves" class="level4">
<h4 class="anchored" data-anchor-id="implicitly-defined-curves">Implicitly defined curves</h4>
<p>The graph of an equation is the collection of all <span class="math inline">\((x,y)\)</span> values satisfying the equation. This is more general than the graph of a function, which can be viewed as the graph of the equation <span class="math inline">\(y=f(x)\)</span>. An equation in <span class="math inline">\(x\)</span>-<span class="math inline">\(y\)</span> can be graphed if the set of solutions to a related equation <span class="math inline">\(f(x,y)=0\)</span> can be identified, as one can move all terms to one side of an equation and define <span class="math inline">\(f\)</span> as the rule of the side with the terms. The implicit function theorem ensures that under some conditions, <em>locally</em> near a point <span class="math inline">\((x, y)\)</span>, the value <span class="math inline">\(y\)</span> can be represented as a function of <span class="math inline">\(x\)</span>. So, the graph of the equation <span class="math inline">\(f(x,y)=0\)</span> can be produced by stitching together these local function representations.</p>
<p>The contour graph can produce these graphs by setting the <code>levels</code> argument to <code>[0]</code>.</p>
<div class="cell" data-execution_count="52">
<div class="sourceCode cell-code" id="cb60"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb60-1"><a href="#cb60-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x,y) <span class="op">=</span> x<span class="op">^</span><span class="fl">3</span> <span class="op">+</span> x<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> x <span class="op">+</span> <span class="fl">1</span> <span class="op">-</span> x<span class="op">*</span>y <span class="co"># solve x^3 + x^2 + x + 1 = x*y</span></span>
<span id="cb60-2"><a href="#cb60-2" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> <span class="fu">range</span>(<span class="op">-</span><span class="fl">5</span>, <span class="fl">5</span>, length<span class="op">=</span><span class="fl">100</span>)</span>
<span id="cb60-3"><a href="#cb60-3" aria-hidden="true" tabindex="-1"></a>ys <span class="op">=</span> <span class="fu">range</span>(<span class="op">-</span><span class="fl">10</span>, <span class="fl">10</span>, length<span class="op">=</span><span class="fl">100</span>)</span>
<span id="cb60-4"><a href="#cb60-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-5"><a href="#cb60-5" aria-hidden="true" tabindex="-1"></a><span class="fu">contour</span>(xs, ys, <span class="fu">f</span>.(xs, ys<span class="op">'</span>); levels<span class="op">=</span>[<span class="fl">0</span>])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="53">
<p><img src="makie_plotting_files/figure-html/cell-53-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>The <code>implicitPlots.jl</code> function uses the <code>Contour</code> package along with a <code>Plots</code> recipe to plot such graphs. Here we see how to use <code>Makie</code> in a similar manner:</p>
<div class="sourceCode cell-code" id="cb61"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb61-1"><a href="#cb61-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> <span class="bu">Contour</span></span>
<span id="cb61-2"><a href="#cb61-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb61-3"><a href="#cb61-3" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">implicit_plot</span>(xs, ys, f; kwargs<span class="op">...</span>)</span>
<span id="cb61-4"><a href="#cb61-4" aria-hidden="true" tabindex="-1"></a> fig <span class="op">=</span> <span class="fu">Figure</span>()</span>
<span id="cb61-5"><a href="#cb61-5" aria-hidden="true" tabindex="-1"></a> ax <span class="op">=</span> <span class="fu">Axis</span>(fig[<span class="fl">1</span>,<span class="fl">1</span>])</span>
<span id="cb61-6"><a href="#cb61-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">implicit_plot!</span>(ax, xs, ys, f; kwargs<span class="op">...</span>)</span>
<span id="cb61-7"><a href="#cb61-7" aria-hidden="true" tabindex="-1"></a> fig</span>
<span id="cb61-8"><a href="#cb61-8" aria-hidden="true" tabindex="-1"></a><span class="kw">end</span></span>
<span id="cb61-9"><a href="#cb61-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb61-10"><a href="#cb61-10" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">implicit_plot!</span>(ax, xs, ys, f; kwargs<span class="op">...</span>)</span>
<span id="cb61-11"><a href="#cb61-11" aria-hidden="true" tabindex="-1"></a> z <span class="op">=</span> [<span class="fu">f</span>(x, y) for x <span class="kw">in</span> xs, y <span class="kw">in</span> ys]</span>
<span id="cb61-12"><a href="#cb61-12" aria-hidden="true" tabindex="-1"></a> cs <span class="op">=</span> Contour.<span class="fu">contour</span>(<span class="fu">collect</span>(xs), <span class="fu">collect</span>(ys), z, <span class="fl">0.0</span>)</span>
<span id="cb61-13"><a href="#cb61-13" aria-hidden="true" tabindex="-1"></a> ls <span class="op">=</span> Contour.<span class="fu">lines</span>(cs)</span>
<span id="cb61-14"><a href="#cb61-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb61-15"><a href="#cb61-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">isempty</span>(ls) <span class="op">&amp;&amp;</span> <span class="fu">error</span>(<span class="st">"empty"</span>)</span>
<span id="cb61-16"><a href="#cb61-16" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb61-17"><a href="#cb61-17" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> l <span class="op"></span> ls</span>
<span id="cb61-18"><a href="#cb61-18" aria-hidden="true" tabindex="-1"></a> us, vs <span class="op">=</span> Contour.<span class="fu">coordinates</span>(l)</span>
<span id="cb61-19"><a href="#cb61-19" aria-hidden="true" tabindex="-1"></a> <span class="fu">lines!</span>(ax, us, vs; kwargs<span class="op">...</span>)</span>
<span id="cb61-20"><a href="#cb61-20" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb61-21"><a href="#cb61-21" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb61-22"><a href="#cb61-22" aria-hidden="true" tabindex="-1"></a><span class="kw">end</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="implicitly-defined-surfaces-fxyz0" class="level4">
<h4 class="anchored" data-anchor-id="implicitly-defined-surfaces-fxyz0">Implicitly defined surfaces, <span class="math inline">\(F(x,y,z)=0\)</span></h4>
<p>To plot the equation <span class="math inline">\(F(x,y,z)=0\)</span>, for <span class="math inline">\(F\)</span> a scalar-valued function, again the implicit function theorem says that, under conditions, near any solution <span class="math inline">\((x,y,z)\)</span>, <span class="math inline">\(z\)</span> can be represented as a function of <span class="math inline">\(x\)</span> and <span class="math inline">\(y\)</span>, so the graph will look likes surfaces stitched together. The <code>Implicit3DPlotting</code> package takes an approach like <code>ImplicitPlots</code> to represent these surfaces. It replaces the <code>Contour</code> package computation with a <span class="math inline">\(3\)</span>-dimensional alternative provided through the <code>Meshing</code> and <code>GeometryBasics</code> packages.</p>
<p>The <code>Implicit3DPlotting</code> package needs some maintenance, so we borrow the main functionality and wrap it into a function:</p>
<div class="cell" data-execution_count="54">
<div class="sourceCode cell-code" id="cb62"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb62-1"><a href="#cb62-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> <span class="bu">Meshing</span></span>
<span id="cb62-2"><a href="#cb62-2" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> <span class="bu">GeometryBasics</span></span>
<span id="cb62-3"><a href="#cb62-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb62-4"><a href="#cb62-4" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">make_mesh</span>(xlims, ylims, zlims, f,</span>
<span id="cb62-5"><a href="#cb62-5" aria-hidden="true" tabindex="-1"></a> M <span class="op">=</span> Meshing.<span class="fu">MarchingCubes</span>(); <span class="co"># or Meshing.MarchingTetrahedra()</span></span>
<span id="cb62-6"><a href="#cb62-6" aria-hidden="true" tabindex="-1"></a> samples<span class="op">=</span>(<span class="fl">35</span>, <span class="fl">35</span>, <span class="fl">35</span>),</span>
<span id="cb62-7"><a href="#cb62-7" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb62-8"><a href="#cb62-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb62-9"><a href="#cb62-9" aria-hidden="true" tabindex="-1"></a> lims <span class="op">=</span> <span class="fu">extrema</span>.((xlims, ylims, zlims))</span>
<span id="cb62-10"><a href="#cb62-10" aria-hidden="true" tabindex="-1"></a> Δ <span class="op">=</span> xs <span class="op">-&gt;</span> <span class="fu">last</span>(xs) <span class="op">-</span> <span class="fu">first</span>(xs)</span>
<span id="cb62-11"><a href="#cb62-11" aria-hidden="true" tabindex="-1"></a> xs <span class="op">=</span> <span class="fu">Vec</span>(<span class="fu">first</span>.(lims))</span>
<span id="cb62-12"><a href="#cb62-12" aria-hidden="true" tabindex="-1"></a> Δxs <span class="op">=</span> <span class="fu">Vec</span>(<span class="fu">Δ</span>.(lims))</span>
<span id="cb62-13"><a href="#cb62-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb62-14"><a href="#cb62-14" aria-hidden="true" tabindex="-1"></a> GeometryBasics.<span class="fu">Mesh</span>(f, <span class="fu">Rect</span>(xs, Δxs), M; samples <span class="op">=</span> samples)</span>
<span id="cb62-15"><a href="#cb62-15" aria-hidden="true" tabindex="-1"></a><span class="kw">end</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="54">
<pre><code>make_mesh (generic function with 2 methods)</code></pre>
</div>
</div>
<p>The <code>make_mesh</code> function creates a mesh that can be visualized with the <code>wireframe</code> or <code>mesh</code> plotting functions.</p>
<p>This example, plotting an implicitly defined sphere, comes from the documentation of <code>Implicit3DPlotting</code>. The <code>f</code> in <code>make_mesh</code> is a scalar-valued function of a vector:</p>
<div class="cell" data-execution_count="55">
<div class="sourceCode cell-code" id="cb64"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb64-1"><a href="#cb64-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x) <span class="op">=</span> <span class="fu">sum</span>(x<span class="op">.^</span><span class="fl">2</span>) <span class="op">-</span> <span class="fl">1</span></span>
<span id="cb64-2"><a href="#cb64-2" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> ys <span class="op">=</span> zs <span class="op">=</span> (<span class="op">-</span><span class="fl">5</span>, <span class="fl">5</span>)</span>
<span id="cb64-3"><a href="#cb64-3" aria-hidden="true" tabindex="-1"></a>m <span class="op">=</span> <span class="fu">make_mesh</span>(xs, ys, zs, f)</span>
<span id="cb64-4"><a href="#cb64-4" aria-hidden="true" tabindex="-1"></a><span class="fu">wireframe</span>(m)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="55">
<p><img src="makie_plotting_files/figure-html/cell-56-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>Here we visualize an intersection of a sphere with another figure:</p>
<div class="cell" data-execution_count="56">
<div class="sourceCode cell-code" id="cb65"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb65-1"><a href="#cb65-1" aria-hidden="true" tabindex="-1"></a><span class="fu">r₂</span>(x) <span class="op">=</span> <span class="fu">sum</span>(x<span class="op">.^</span><span class="fl">2</span>) <span class="op">-</span> <span class="fl">5</span><span class="op">/</span><span class="fl">4</span> <span class="co"># a sphere</span></span>
<span id="cb65-2"><a href="#cb65-2" aria-hidden="true" tabindex="-1"></a><span class="fu">r₄</span>(x) <span class="op">=</span> <span class="fu">sum</span>(x<span class="op">.^</span><span class="fl">4</span>) <span class="op">-</span> <span class="fl">1</span></span>
<span id="cb65-3"><a href="#cb65-3" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> ys <span class="op">=</span> zs <span class="op">=</span> <span class="op">-</span><span class="fl">2</span><span class="op">:</span><span class="fl">2</span></span>
<span id="cb65-4"><a href="#cb65-4" aria-hidden="true" tabindex="-1"></a>m2,m4 <span class="op">=</span> <span class="fu">make_mesh</span>(xs, ys, zs, r₂), <span class="fu">make_mesh</span>(xs, ys, zs, r₄)</span>
<span id="cb65-5"><a href="#cb65-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb65-6"><a href="#cb65-6" aria-hidden="true" tabindex="-1"></a><span class="fu">wireframe</span>(m4, color<span class="op">=:</span>yellow)</span>
<span id="cb65-7"><a href="#cb65-7" aria-hidden="true" tabindex="-1"></a><span class="fu">wireframe!</span>(m2, color<span class="op">=:</span>red)</span>
<span id="cb65-8"><a href="#cb65-8" aria-hidden="true" tabindex="-1"></a><span class="fu">current_figure</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="56">
<p><img src="makie_plotting_files/figure-html/cell-57-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>This example comes from <a href="https://en.wikipedia.org/wiki/Implicit_surface">Wikipedia</a> showing an implicit surface of genus <span class="math inline">\(2\)</span>:</p>
<div class="cell" data-execution_count="57">
<div class="sourceCode cell-code" id="cb66"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb66-1"><a href="#cb66-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x,y,z) <span class="op">=</span> <span class="fl">2</span><span class="fu">y*</span>(y<span class="op">^</span><span class="fl">2</span> <span class="op">-</span><span class="fl">3</span>x<span class="op">^</span><span class="fl">2</span>)<span class="fu">*</span>(<span class="fl">1</span><span class="op">-</span>z<span class="op">^</span><span class="fl">2</span>) <span class="op">+</span> (x<span class="op">^</span><span class="fl">2</span> <span class="op">+</span>y<span class="op">^</span><span class="fl">2</span>)<span class="op">^</span><span class="fl">2</span> <span class="op">-</span> (<span class="fl">9</span>z<span class="op">^</span><span class="fl">2</span><span class="op">-</span><span class="fl">1</span>)<span class="fu">*</span>(<span class="fl">1</span><span class="op">-</span>z<span class="op">^</span><span class="fl">2</span>)</span>
<span id="cb66-2"><a href="#cb66-2" aria-hidden="true" tabindex="-1"></a>zs <span class="op">=</span> ys <span class="op">=</span> xs <span class="op">=</span> <span class="fu">range</span>(<span class="op">-</span><span class="fl">5</span><span class="op">/</span><span class="fl">2</span>, <span class="fl">5</span><span class="op">/</span><span class="fl">2</span>, length<span class="op">=</span><span class="fl">100</span>)</span>
<span id="cb66-3"><a href="#cb66-3" aria-hidden="true" tabindex="-1"></a>m <span class="op">=</span> <span class="fu">make_mesh</span>(xs, ys, zs, x <span class="op">-&gt;</span> <span class="fu">f</span>(x<span class="op">...</span>))</span>
<span id="cb66-4"><a href="#cb66-4" aria-hidden="true" tabindex="-1"></a><span class="fu">wireframe</span>(m)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="57">
<p><img src="makie_plotting_files/figure-html/cell-58-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>(This figure does not render well through <code>contour(xs, ys, zs, f, levels=[0])</code>, as the hole is not shown.)</p>
<p>For one last example from Wikipedia, we have the Cassini oval which “can be defined as the point set for which the <em>product</em> of the distances to <span class="math inline">\(n\)</span> given points is constant.” That is:</p>
<div class="cell" data-execution_count="58">
<div class="sourceCode cell-code" id="cb67"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb67-1"><a href="#cb67-1" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">cassini</span>(λ, ps <span class="op">=</span> ((<span class="fl">1</span>,<span class="fl">0</span>,<span class="fl">0</span>), (<span class="op">-</span><span class="fl">1</span>, <span class="fl">0</span>, <span class="fl">0</span>)))</span>
<span id="cb67-2"><a href="#cb67-2" aria-hidden="true" tabindex="-1"></a> n <span class="op">=</span> <span class="fu">length</span>(ps)</span>
<span id="cb67-3"><a href="#cb67-3" aria-hidden="true" tabindex="-1"></a> x <span class="op">-&gt;</span> <span class="fu">prod</span>(<span class="fu">norm</span>(x <span class="op">.-</span> p) <span class="cf">for</span> p <span class="op"></span> ps) <span class="op">-</span> λ<span class="op">^</span>n</span>
<span id="cb67-4"><a href="#cb67-4" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
<span id="cb67-5"><a href="#cb67-5" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> ys <span class="op">=</span> zs <span class="op">=</span> <span class="fu">range</span>(<span class="op">-</span><span class="fl">2</span>, <span class="fl">2</span>, length<span class="op">=</span><span class="fl">100</span>)</span>
<span id="cb67-6"><a href="#cb67-6" aria-hidden="true" tabindex="-1"></a>m <span class="op">=</span> <span class="fu">make_mesh</span>(xs, ys, zs, <span class="fu">cassini</span>(<span class="fl">1.05</span>))</span>
<span id="cb67-7"><a href="#cb67-7" aria-hidden="true" tabindex="-1"></a><span class="fu">wireframe</span>(m)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="58">
<p><img src="makie_plotting_files/figure-html/cell-59-output-1.png" class="img-fluid"></p>
</div>
</div>
</section>
</section>
</section>
<section id="vector-fields.-visualizations-of-fr2-rightarrow-r2" class="level2" data-number="65.6">
<h2 data-number="65.6" class="anchored" data-anchor-id="vector-fields.-visualizations-of-fr2-rightarrow-r2"><span class="header-section-number">65.6</span> Vector fields. Visualizations of <span class="math inline">\(f:R^2 \rightarrow R^2\)</span></h2>
<p>The vector field <span class="math inline">\(f(x,y) = \langle y, -x \rangle\)</span> can be visualized as a set of vectors, <span class="math inline">\(f(x,y)\)</span>, positioned at a grid. These arrows can be visualized with the <code>arrows</code> function. The <code>arrows</code> function is passed a vector of points for the anchors and a vector of points representing the vectors.</p>
<p>We can generate these on a regular grid through:</p>
<div class="cell" data-execution_count="59">
<div class="sourceCode cell-code" id="cb68"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb68-1"><a href="#cb68-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x, y) <span class="op">=</span> [y, <span class="op">-</span>x]</span>
<span id="cb68-2"><a href="#cb68-2" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> ys <span class="op">=</span> <span class="op">-</span><span class="fl">5</span><span class="op">:</span><span class="fl">5</span></span>
<span id="cb68-3"><a href="#cb68-3" aria-hidden="true" tabindex="-1"></a>pts <span class="op">=</span> <span class="fu">vec</span>(<span class="fu">Point2</span>.(xs, ys<span class="op">'</span>))</span>
<span id="cb68-4"><a href="#cb68-4" aria-hidden="true" tabindex="-1"></a>dus <span class="op">=</span> <span class="fu">vec</span>(<span class="fu">Point2</span>.(<span class="fu">f</span>.(xs, ys<span class="op">'</span>)));</span>
<span id="cb68-5"><a href="#cb68-5" aria-hidden="true" tabindex="-1"></a><span class="fu">first</span>(pts), <span class="fu">first</span>(dus) <span class="co"># show an example</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="59">
<pre><code>([-5, -5], [-5, 5])</code></pre>
</div>
</div>
<p>Broadcasting over <code>(xs, ys')</code> ensures each pair of possible values is encountered. The <code>vec</code> call reshapes an array into a vector.</p>
<p>Calling <code>arrows</code> on the prepared data produces the graphic:</p>
<div class="cell" data-execution_count="60">
<div class="sourceCode cell-code" id="cb70"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb70-1"><a href="#cb70-1" aria-hidden="true" tabindex="-1"></a><span class="fu">arrows</span>(pts, dus)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="60">
<p><img src="makie_plotting_files/figure-html/cell-61-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>The grid seems rotated at first glance; but is also confusing. This is due to the length of the vectors as the <span class="math inline">\((x,y)\)</span> values get farther from the origin. Plotting the <em>normalized</em> values (each will have length <span class="math inline">\(1\)</span>) can be done easily using <code>norm</code> (which is found in the standard <code>LinearAlgebra</code> library):</p>
<div class="cell" data-execution_count="61">
<div class="sourceCode cell-code" id="cb71"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb71-1"><a href="#cb71-1" aria-hidden="true" tabindex="-1"></a>dvs <span class="op">=</span> dus <span class="op">./</span> <span class="fu">norm</span>.(dus)</span>
<span id="cb71-2"><a href="#cb71-2" aria-hidden="true" tabindex="-1"></a><span class="fu">arrows</span>(pts, dvs)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="61">
<p><img src="makie_plotting_files/figure-html/cell-62-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>The rotational pattern becomes much clearer now.</p>
<p>The <code>streamplot</code> function also illustrates this phenomenon. This implements an “algorithm [that] puts an arrow somewhere and extends the streamline in both directions from there. Then, it chooses a new position (from the remaining ones), repeating the the exercise until the streamline gets blocked, from which on a new starting point, the process repeats.”</p>
<p>The <code>streamplot</code> function expects a <code>Point</code> not a pair of values, so we adjust <code>f</code> slightly and call the function using the pattern <code>streamplot(g, xs, ys)</code>:</p>
<div class="cell" data-execution_count="62">
<div class="sourceCode cell-code" id="cb72"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb72-1"><a href="#cb72-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x, y) <span class="op">=</span> [y, <span class="op">-</span>x]</span>
<span id="cb72-2"><a href="#cb72-2" aria-hidden="true" tabindex="-1"></a><span class="fu">g</span>(xs) <span class="op">=</span> <span class="fu">Point2</span>(<span class="fu">f</span>(xs<span class="op">...</span>))</span>
<span id="cb72-3"><a href="#cb72-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb72-4"><a href="#cb72-4" aria-hidden="true" tabindex="-1"></a><span class="fu">streamplot</span>(g, <span class="op">-</span><span class="fl">5</span><span class="op">..</span><span class="fl">5</span>, <span class="op">-</span><span class="fl">5</span><span class="op">..</span><span class="fl">5</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="62">
<p><img src="makie_plotting_files/figure-html/cell-63-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>(We used interval notation to set the viewing range, a range could also be used.)</p>
<div class="callout-note callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>The calling pattern of <code>streamplot</code> is different than other functions, such as <code>surface</code>, in that the function comes first.</p>
</div>
</div>
</section>
<section id="layoutables-and-observables" class="level2" data-number="65.7">
<h2 data-number="65.7" class="anchored" data-anchor-id="layoutables-and-observables"><span class="header-section-number">65.7</span> Layoutables and Observables</h2>
<section id="layoutables" class="level3" data-number="65.7.1">
<h3 data-number="65.7.1" class="anchored" data-anchor-id="layoutables"><span class="header-section-number">65.7.1</span> Layoutables</h3>
<p><code>Makie</code> makes it really easy to piece together figures from individual plots. To illustrate, we create a graphic consisting of a plot of a function, its derivative, and its second derivative. In our graphic, we also leave space for a label.</p>
<div class="callout-note callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>The Layout <a href="https://makie.juliaplots.org/stable/tutorials/layout-tutorial/">Tutorial</a> has <em>much</em> more detail on this subject.</p>
</div>
</div>
<p>The basic plotting commands, like <code>lines</code>, return a <code>FigureAxisPlot</code> object. For laying out our own graphic, we manage the figure and axes manually. The commands below create a figure, then assign axes to portions of the figure:</p>
<div class="cell" data-execution_count="63">
<div class="sourceCode cell-code" id="cb73"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb73-1"><a href="#cb73-1" aria-hidden="true" tabindex="-1"></a>F <span class="op">=</span> <span class="fu">Figure</span>()</span>
<span id="cb73-2"><a href="#cb73-2" aria-hidden="true" tabindex="-1"></a>af <span class="op">=</span> F[<span class="fl">2</span>,<span class="fl">1</span><span class="op">:</span><span class="fl">2</span>] <span class="op">=</span> <span class="fu">Axis</span>(F)</span>
<span id="cb73-3"><a href="#cb73-3" aria-hidden="true" tabindex="-1"></a>afp <span class="op">=</span> F[<span class="fl">3</span>,<span class="fl">1</span><span class="op">:</span><span class="kw">end</span>] <span class="op">=</span> <span class="fu">Axis</span>(F)</span>
<span id="cb73-4"><a href="#cb73-4" aria-hidden="true" tabindex="-1"></a>afpp <span class="op">=</span> F[<span class="fl">4</span>,<span class="op">:</span>] <span class="op">=</span> <span class="fu">Axis</span>(F)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="63">
<pre><code>Axis with 1 plots:
┗━ Mesh{Tuple{GeometryBasics.Mesh{3, Float32, GeometryBasics.TriangleP{3, Float32, GeometryBasics.PointMeta{3, Float32, Point{3, Float32}, (:normals,), Tuple{Vec{3, Float32}}}}, GeometryBasics.FaceView{GeometryBasics.TriangleP{3, Float32, GeometryBasics.PointMeta{3, Float32, Point{3, Float32}, (:normals,), Tuple{Vec{3, Float32}}}}, GeometryBasics.PointMeta{3, Float32, Point{3, Float32}, (:normals,), Tuple{Vec{3, Float32}}}, GeometryBasics.NgonFace{3, GeometryBasics.OffsetInteger{-1, UInt32}}, StructArrays.StructVector{GeometryBasics.PointMeta{3, Float32, Point{3, Float32}, (:normals,), Tuple{Vec{3, Float32}}}, NamedTuple{(:position, :normals), Tuple{Vector{Point{3, Float32}}, Vector{Vec{3, Float32}}}}, Int64}, Vector{GeometryBasics.NgonFace{3, GeometryBasics.OffsetInteger{-1, UInt32}}}}}}}</code></pre>
</div>
</div>
<p>The axes are named <code>af</code>, <code>afp</code> and <code>afpp</code>, as they will hold the respective graphs. The key here is the use of matrix notation to layout the graphic in a grid. The first one is row 2 and columns 1 through 2; the second row 3 and again all columns, the third is row 4 and all columns.</p>
<p>In this figure, we want the <span class="math inline">\(x\)</span>-axis for each of the three graphics to be linked. This command ensures that:</p>
<div class="sourceCode cell-code" id="cb75"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb75-1"><a href="#cb75-1" aria-hidden="true" tabindex="-1"></a><span class="fu">linkxaxes!</span>(af, afp, afpp);</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<p>By linking axes, if one is updated, say through <code>xlims!</code>, the others will be as well.</p>
<p>We now plot our functions. The key here is the mutating form of <code>lines!</code> takes an axis object to mutate as its first argument:</p>
<div class="sourceCode cell-code" id="cb76"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb76-1"><a href="#cb76-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x) <span class="op">=</span> <span class="fl">8</span>x<span class="op">^</span><span class="fl">4</span> <span class="op">-</span> <span class="fl">8</span>x<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> <span class="fl">1</span></span>
<span id="cb76-2"><a href="#cb76-2" aria-hidden="true" tabindex="-1"></a><span class="fu">fp</span>(x) <span class="op">=</span> <span class="fl">32</span>x<span class="op">^</span><span class="fl">3</span> <span class="op">-</span> <span class="fl">16</span>x</span>
<span id="cb76-3"><a href="#cb76-3" aria-hidden="true" tabindex="-1"></a><span class="fu">fpp</span>(x) <span class="op">=</span> <span class="fl">96</span>x<span class="op">^</span><span class="fl">2</span> <span class="op">-</span> <span class="fl">16</span></span>
<span id="cb76-4"><a href="#cb76-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb76-5"><a href="#cb76-5" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> <span class="op">-</span><span class="fl">1</span><span class="op">..</span><span class="fl">1</span></span>
<span id="cb76-6"><a href="#cb76-6" aria-hidden="true" tabindex="-1"></a><span class="fu">lines!</span>(af, xs, f)</span>
<span id="cb76-7"><a href="#cb76-7" aria-hidden="true" tabindex="-1"></a><span class="fu">lines!</span>(afp, xs, fp)</span>
<span id="cb76-8"><a href="#cb76-8" aria-hidden="true" tabindex="-1"></a><span class="fu">lines!</span>(afp, xs, zero, color<span class="op">=:</span>blue)</span>
<span id="cb76-9"><a href="#cb76-9" aria-hidden="true" tabindex="-1"></a><span class="fu">lines!</span>(afpp, xs, fpp)</span>
<span id="cb76-10"><a href="#cb76-10" aria-hidden="true" tabindex="-1"></a><span class="fu">lines!</span>(afpp, xs, zero, color<span class="op">=:</span>blue);</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<p>We can give title information to each axis:</p>
<div class="sourceCode cell-code" id="cb77"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb77-1"><a href="#cb77-1" aria-hidden="true" tabindex="-1"></a>af.title <span class="op">=</span> <span class="st">"f"</span></span>
<span id="cb77-2"><a href="#cb77-2" aria-hidden="true" tabindex="-1"></a>afp.title <span class="op">=</span> <span class="st">"fp"</span></span>
<span id="cb77-3"><a href="#cb77-3" aria-hidden="true" tabindex="-1"></a>afpp.title <span class="op">=</span> <span class="st">"fpp"</span>;</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<p>Finally, we add a label in the first row, but for illustration purposes, only use the first column.</p>
<div class="sourceCode cell-code" id="cb78"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb78-1"><a href="#cb78-1" aria-hidden="true" tabindex="-1"></a><span class="fu">Label</span>(F[<span class="fl">1</span>,<span class="fl">1</span>], <span class="st">"""</span></span>
<span id="cb78-2"><a href="#cb78-2" aria-hidden="true" tabindex="-1"></a><span class="st">Plots of f and its first and second derivatives.</span></span>
<span id="cb78-3"><a href="#cb78-3" aria-hidden="true" tabindex="-1"></a><span class="st">When the first derivative is zero, the function</span></span>
<span id="cb78-4"><a href="#cb78-4" aria-hidden="true" tabindex="-1"></a><span class="st">f has relative extrema. When the second derivative</span></span>
<span id="cb78-5"><a href="#cb78-5" aria-hidden="true" tabindex="-1"></a><span class="st">is zero, the function f has an inflection point.</span></span>
<span id="cb78-6"><a href="#cb78-6" aria-hidden="true" tabindex="-1"></a><span class="st">"""</span>);</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<p>Finally we display the figure:</p>
<div class="cell" data-execution_count="68">
<div class="sourceCode cell-code" id="cb79"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb79-1"><a href="#cb79-1" aria-hidden="true" tabindex="-1"></a>F</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="68">
<p><img src="makie_plotting_files/figure-html/cell-69-output-1.png" class="img-fluid"></p>
</div>
</div>
</section>
<section id="observables" class="level3" data-number="65.7.2">
<h3 data-number="65.7.2" class="anchored" data-anchor-id="observables"><span class="header-section-number">65.7.2</span> Observables</h3>
<p>The basic components of a plot in <code>Makie</code> can be updated <a href="https://makie.juliaplots.org/stable/documentation/nodes/index.html#observables_interaction">interactively</a>. <code>Makie</code> uses the <code>Observables</code> package which allows complicated interactions to be modeled quite naturally. In the following we give a simple example.</p>
<p>In Makie, an <code>Observable</code> is a structure that allows its value to be updated, similar to an array. When changed, observables can trigger an event. Observables can rely on other observables, so events can be cascaded.</p>
<p>This simple example shows how an observable <code>h</code> can be used to create a collection of points representing a secant line. The figure shows the value for <code>h=3/2</code>.</p>
<div class="cell" data-execution_count="69">
<div class="sourceCode cell-code" id="cb80"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb80-1"><a href="#cb80-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x) <span class="op">=</span> <span class="fu">sqrt</span>(x)</span>
<span id="cb80-2"><a href="#cb80-2" aria-hidden="true" tabindex="-1"></a>c <span class="op">=</span> <span class="fl">1</span></span>
<span id="cb80-3"><a href="#cb80-3" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> <span class="fl">0</span><span class="op">..</span><span class="fl">3</span></span>
<span id="cb80-4"><a href="#cb80-4" aria-hidden="true" tabindex="-1"></a>h <span class="op">=</span> <span class="fu">Observable</span>(<span class="fl">3</span><span class="op">/</span><span class="fl">2</span>)</span>
<span id="cb80-5"><a href="#cb80-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb80-6"><a href="#cb80-6" aria-hidden="true" tabindex="-1"></a>points <span class="op">=</span> <span class="fu">lift</span>(h) <span class="cf">do</span> h</span>
<span id="cb80-7"><a href="#cb80-7" aria-hidden="true" tabindex="-1"></a> xs <span class="op">=</span> [<span class="fl">0</span>,c,c<span class="op">+</span>h,<span class="fl">3</span>]</span>
<span id="cb80-8"><a href="#cb80-8" aria-hidden="true" tabindex="-1"></a> tl <span class="op">=</span> x <span class="op">-&gt;</span> <span class="fu">f</span>(c) <span class="op">+</span> (<span class="fu">f</span>(c<span class="op">+</span>h)<span class="fu">-f</span>(c))<span class="op">/</span>h <span class="op">*</span> (x<span class="op">-</span>c)</span>
<span id="cb80-9"><a href="#cb80-9" aria-hidden="true" tabindex="-1"></a> [<span class="fu">Point2</span>(x, <span class="fu">tl</span>(x)) for x <span class="op"></span> xs]</span>
<span id="cb80-10"><a href="#cb80-10" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
<span id="cb80-11"><a href="#cb80-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb80-12"><a href="#cb80-12" aria-hidden="true" tabindex="-1"></a><span class="fu">lines</span>(xs, f)</span>
<span id="cb80-13"><a href="#cb80-13" aria-hidden="true" tabindex="-1"></a><span class="fu">lines!</span>(points)</span>
<span id="cb80-14"><a href="#cb80-14" aria-hidden="true" tabindex="-1"></a><span class="fu">current_figure</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="69">
<p><img src="makie_plotting_files/figure-html/cell-70-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>We can update the value of <code>h</code> using <code>setindex!</code> notation (square brackets). For example, to see that the secant line is a good approximation to the tangent line as <span class="math inline">\(h \rightarrow 0\)</span> we can set <code>h</code> to be <code>1/4</code> and replot:</p>
<div class="cell" data-execution_count="70">
<div class="sourceCode cell-code" id="cb81"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb81-1"><a href="#cb81-1" aria-hidden="true" tabindex="-1"></a>h[] <span class="op">=</span> <span class="fl">1</span><span class="op">/</span><span class="fl">4</span></span>
<span id="cb81-2"><a href="#cb81-2" aria-hidden="true" tabindex="-1"></a><span class="fu">current_figure</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="70">
<p><img src="makie_plotting_files/figure-html/cell-71-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>The line <code>h[] = 1/4</code> updated <code>h</code> which then updated <code>points</code> (a points is lifted up from <code>h</code>) which updated the graphic. (In these notes, we replot to see the change, but in an interactive session, the current <em>displayed</em> figure would be updated; no replotting would be necessary.)</p>
<p>Finally, this example shows how to add a slider to adjust the value of <code>h</code> with a mouse. The slider object is positioned along with a label using the grid reference, as before.</p>
<div class="cell" data-execution_count="71">
<div class="sourceCode cell-code" id="cb82"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb82-1"><a href="#cb82-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x) <span class="op">=</span> <span class="fu">sqrt</span>(x)</span>
<span id="cb82-2"><a href="#cb82-2" aria-hidden="true" tabindex="-1"></a>c <span class="op">=</span> <span class="fl">1</span></span>
<span id="cb82-3"><a href="#cb82-3" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> <span class="fl">0</span><span class="op">..</span><span class="fl">3</span></span>
<span id="cb82-4"><a href="#cb82-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb82-5"><a href="#cb82-5" aria-hidden="true" tabindex="-1"></a>F <span class="op">=</span> <span class="fu">Figure</span>()</span>
<span id="cb82-6"><a href="#cb82-6" aria-hidden="true" tabindex="-1"></a>ax <span class="op">=</span> <span class="fu">Axis</span>(F[<span class="fl">1</span>,<span class="fl">1</span><span class="op">:</span><span class="fl">2</span>])</span>
<span id="cb82-7"><a href="#cb82-7" aria-hidden="true" tabindex="-1"></a>h <span class="op">=</span> <span class="fu">Slider</span>(F[<span class="fl">2</span>,<span class="fl">2</span>], range <span class="op">=</span> <span class="fl">0.01</span><span class="op">:</span><span class="fl">0.01</span><span class="op">:</span><span class="fl">1.5</span>, startvalue <span class="op">=</span> <span class="fl">1.5</span>)</span>
<span id="cb82-8"><a href="#cb82-8" aria-hidden="true" tabindex="-1"></a><span class="fu">Label</span>(F[<span class="fl">2</span>,<span class="fl">1</span>], <span class="st">"Adjust slider to change `h`"</span>;</span>
<span id="cb82-9"><a href="#cb82-9" aria-hidden="true" tabindex="-1"></a> justification <span class="op">=</span> <span class="op">:</span>left)</span>
<span id="cb82-10"><a href="#cb82-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb82-11"><a href="#cb82-11" aria-hidden="true" tabindex="-1"></a>points <span class="op">=</span> <span class="fu">lift</span>(h.value) <span class="cf">do</span> h</span>
<span id="cb82-12"><a href="#cb82-12" aria-hidden="true" tabindex="-1"></a> xs <span class="op">=</span> [<span class="fl">0</span>,c,c<span class="op">+</span>h,<span class="fl">3</span>]</span>
<span id="cb82-13"><a href="#cb82-13" aria-hidden="true" tabindex="-1"></a> tl <span class="op">=</span> x<span class="op">-&gt;</span> <span class="fu">f</span>(c) <span class="op">+</span> (<span class="fu">f</span>(c<span class="op">+</span>h)<span class="fu">-f</span>(c))<span class="op">/</span>h <span class="op">*</span> (x<span class="op">-</span>c)</span>
<span id="cb82-14"><a href="#cb82-14" aria-hidden="true" tabindex="-1"></a> [<span class="fu">Point2</span>(x, <span class="fu">tl</span>(x)) for x <span class="op"></span> xs]</span>
<span id="cb82-15"><a href="#cb82-15" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
<span id="cb82-16"><a href="#cb82-16" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb82-17"><a href="#cb82-17" aria-hidden="true" tabindex="-1"></a><span class="fu">lines!</span>(ax, xs, f)</span>
<span id="cb82-18"><a href="#cb82-18" aria-hidden="true" tabindex="-1"></a><span class="fu">lines!</span>(ax, points)</span>
<span id="cb82-19"><a href="#cb82-19" aria-hidden="true" tabindex="-1"></a><span class="fu">current_figure</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="71">
<p><img src="makie_plotting_files/figure-html/cell-72-output-1.png" class="img-fluid"></p>
</div>
</div>
<p>The slider value is “lifted” by its <code>value</code> component, as shown. Otherwise, the above is fairly similar to just using an observable for <code>h</code>.</p>
</section>
</section>
</main> <!-- /main -->
<script id="quarto-html-after-body" type="application/javascript">
window.document.addEventListener("DOMContentLoaded", function (event) {
const toggleBodyColorMode = (bsSheetEl) => {
const mode = bsSheetEl.getAttribute("data-mode");
const bodyEl = window.document.querySelector("body");
if (mode === "dark") {
bodyEl.classList.add("quarto-dark");
bodyEl.classList.remove("quarto-light");
} else {
bodyEl.classList.add("quarto-light");
bodyEl.classList.remove("quarto-dark");
}
}
const toggleBodyColorPrimary = () => {
const bsSheetEl = window.document.querySelector("link#quarto-bootstrap");
if (bsSheetEl) {
toggleBodyColorMode(bsSheetEl);
}
}
toggleBodyColorPrimary();
const icon = "";
const anchorJS = new window.AnchorJS();
anchorJS.options = {
placement: 'right',
icon: icon
};
anchorJS.add('.anchored');
const clipboard = new window.ClipboardJS('.code-copy-button', {
target: function(trigger) {
return trigger.previousElementSibling;
}
});
clipboard.on('success', function(e) {
// button target
const button = e.trigger;
// don't keep focus
button.blur();
// flash "checked"
button.classList.add('code-copy-button-checked');
var currentTitle = button.getAttribute("title");
button.setAttribute("title", "Copied!");
setTimeout(function() {
button.setAttribute("title", currentTitle);
button.classList.remove('code-copy-button-checked');
}, 1000);
// clear code selection
e.clearSelection();
});
function tippyHover(el, contentFn) {
const config = {
allowHTML: true,
content: contentFn,
maxWidth: 500,
delay: 100,
arrow: false,
appendTo: function(el) {
return el.parentElement;
},
interactive: true,
interactiveBorder: 10,
theme: 'quarto',
placement: 'bottom-start'
};
window.tippy(el, config);
}
const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
for (var i=0; i<noterefs.length; i++) {
const ref = noterefs[i];
tippyHover(ref, function() {
let href = ref.getAttribute('href');
try { href = new URL(href).hash; } catch {}
const id = href.replace(/^#\/?/, "");
const note = window.document.getElementById(id);
return note.innerHTML;
});
}
var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]');
for (var i=0; i<bibliorefs.length; i++) {
const ref = bibliorefs[i];
const cites = ref.parentNode.getAttribute('data-cites').split(' ');
tippyHover(ref, function() {
var popup = window.document.createElement('div');
cites.forEach(function(cite) {
var citeDiv = window.document.createElement('div');
citeDiv.classList.add('hanging-indent');
citeDiv.classList.add('csl-entry');
var biblioDiv = window.document.getElementById('ref-' + cite);
if (biblioDiv) {
citeDiv.innerHTML = biblioDiv.innerHTML;
}
popup.appendChild(citeDiv);
});
return popup.innerHTML;
});
}
var localhostRegex = new RegExp(/^(?:http|https):\/\/localhost\:?[0-9]*\//);
var filterRegex = new RegExp('/' + window.location.host + '/');
var isInternal = (href) => {
return filterRegex.test(href) || localhostRegex.test(href);
}
// Inspect non-navigation links and adorn them if external
var links = window.document.querySelectorAll('a:not(.nav-link):not(.navbar-brand):not(.toc-action):not(.sidebar-link):not(.sidebar-item-toggle):not(.pagination-link):not(.no-external)');
for (var i=0; i<links.length; i++) {
const link = links[i];
if (!isInternal(link.href)) {
// target, if specified
link.setAttribute("target", "_blank");
}
}
});
</script>
<nav class="page-navigation">
<div class="nav-page nav-page-previous">
<a href="../alternatives/plotly_plotting.html" class="pagination-link">
<i class="bi bi-arrow-left-short"></i> <span class="nav-page-text"><span class="chapter-number">64</span>&nbsp; <span class="chapter-title">JavaScript based plotting libraries</span></span>
</a>
</div>
<div class="nav-page nav-page-next">
<a href="../misc/getting_started_with_julia.html" class="pagination-link">
<span class="nav-page-text"><span class="chapter-number">66</span>&nbsp; <span class="chapter-title">Getting started with Julia</span></span> <i class="bi bi-arrow-right-short"></i>
</a>
</div>
</nav>
</div> <!-- /content -->
<footer class="footer">
<div class="nav-footer">
<div class="nav-footer-center">Copyright 2022, John Verzani</div>
</div>
</footer>
</body></html>