Files
AstronomicalData/07_plot.html
2021-03-17 11:26:14 -04:00

1143 lines
72 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>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Visualization &#8212; Astronomical Data in Python</title>
<link rel="stylesheet" href="_static/css/index.d431a4ee1c1efae0e38bdfebc22debff.css">
<link rel="stylesheet"
href="_static/vendor/fontawesome/5.13.0/css/all.min.css">
<link rel="preload" as="font" type="font/woff2" crossorigin
href="_static/vendor/fontawesome/5.13.0/webfonts/fa-solid-900.woff2">
<link rel="preload" as="font" type="font/woff2" crossorigin
href="_static/vendor/fontawesome/5.13.0/webfonts/fa-brands-400.woff2">
<link rel="stylesheet"
href="_static/vendor/open-sans_all/1.44.1/index.css">
<link rel="stylesheet"
href="_static/vendor/lato_latin-ext/1.44.1/index.css">
<link rel="stylesheet" href="_static/sphinx-book-theme.bfb7730f9caf2ec0b46a44615585038c.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" type="text/css" href="_static/togglebutton.css" />
<link rel="stylesheet" type="text/css" href="_static/copybutton.css" />
<link rel="stylesheet" type="text/css" href="_static/mystnb.css" />
<link rel="stylesheet" type="text/css" href="_static/sphinx-thebe.css" />
<link rel="stylesheet" type="text/css" href="_static/panels-main.c949a650a448cc0ae9fd3441c0e17fb0.css" />
<link rel="stylesheet" type="text/css" href="_static/panels-variables.06eb56fa6e07937060861dad626602ad.css" />
<link rel="preload" as="script" href="_static/js/index.30270b6e4c972e43c488.js">
<script id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
<script src="_static/jquery.js"></script>
<script src="_static/underscore.js"></script>
<script src="_static/doctools.js"></script>
<script src="_static/language_data.js"></script>
<script src="_static/togglebutton.js"></script>
<script src="_static/clipboard.min.js"></script>
<script src="_static/copybutton.js"></script>
<script >var togglebuttonSelector = '.toggle, .admonition.dropdown, .tag_hide_input div.cell_input, .tag_hide-input div.cell_input, .tag_hide_output div.cell_output, .tag_hide-output div.cell_output, .tag_hide_cell.cell, .tag_hide-cell.cell';</script>
<script src="_static/sphinx-book-theme.be0a4a0c39cd630af62a2fcf693f3f06.js"></script>
<script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["\\(", "\\)"]], "displayMath": [["\\[", "\\]"]], "processRefs": false, "processEnvironments": false}})</script>
<script async="async" src="https://unpkg.com/thebelab@latest/lib/index.js"></script>
<script >
const thebe_selector = ".thebe"
const thebe_selector_input = "pre"
const thebe_selector_output = ".output"
</script>
<script async="async" src="_static/sphinx-thebe.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="prev" title="6. Photometry" href="06_photo.html" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="docsearch:language" content="en" />
</head>
<body data-spy="scroll" data-target="#bd-toc-nav" data-offset="80">
<div class="container-xl">
<div class="row">
<div class="col-12 col-md-3 bd-sidebar site-navigation show" id="site-navigation">
<div class="navbar-brand-box">
<a class="navbar-brand text-wrap" href="index.html">
<h1 class="site-logo" id="site-title">Astronomical Data in Python</h1>
</a>
</div>
<form class="bd-search d-flex align-items-center" action="search.html" method="get">
<i class="icon fas fa-search"></i>
<input type="search" class="form-control" name="q" id="search-input" placeholder="Search this book..." aria-label="Search this book..." autocomplete="off" >
</form>
<nav class="bd-links" id="bd-docs-nav" aria-label="Main navigation">
<ul class="nav sidenav_l1">
<li class="toctree-l1">
<a class="reference internal" href="README.html">
Astronomical Data in Python
</a>
</li>
</ul>
<ul class="current nav sidenav_l1">
<li class="toctree-l1">
<a class="reference internal" href="01_query.html">
1. Queries
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="02_coords.html">
2. Coordinates and Units
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="03_motion.html">
3. Proper Motion
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="04_select.html">
4. Transformation and Selection
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="05_join.html">
5. Joining Tables
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="06_photo.html">
6. Photometry
</a>
</li>
<li class="toctree-l1 current active">
<a class="current reference internal" href="#">
Visualization
</a>
</li>
</ul>
</nav>
<!-- To handle the deprecated key -->
<div class="navbar_extra_footer">
Powered by <a href="https://jupyterbook.org">Jupyter Book</a>
</div>
</div>
<main class="col py-md-3 pl-md-4 bd-content overflow-auto" role="main">
<div class="row topbar fixed-top container-xl">
<div class="col-12 col-md-3 bd-topbar-whitespace site-navigation show">
</div>
<div class="col pl-2 topbar-main">
<button id="navbar-toggler" class="navbar-toggler ml-0" type="button" data-toggle="collapse"
data-toggle="tooltip" data-placement="bottom" data-target=".site-navigation" aria-controls="navbar-menu"
aria-expanded="true" aria-label="Toggle navigation" aria-controls="site-navigation"
title="Toggle navigation" data-toggle="tooltip" data-placement="left">
<i class="fas fa-bars"></i>
<i class="fas fa-arrow-left"></i>
<i class="fas fa-arrow-up"></i>
</button>
<div class="dropdown-buttons-trigger">
<button id="dropdown-buttons-trigger" class="btn btn-secondary topbarbtn" aria-label="Download this page"><i
class="fas fa-download"></i></button>
<div class="dropdown-buttons">
<!-- ipynb file if we had a myst markdown file -->
<!-- Download raw file -->
<a class="dropdown-buttons" href="_sources/07_plot.ipynb"><button type="button"
class="btn btn-secondary topbarbtn" title="Download source file" data-toggle="tooltip"
data-placement="left">.ipynb</button></a>
<!-- Download PDF via print -->
<button type="button" id="download-print" class="btn btn-secondary topbarbtn" title="Print to PDF"
onClick="window.print()" data-toggle="tooltip" data-placement="left">.pdf</button>
</div>
</div>
<!-- Source interaction buttons -->
<div class="dropdown-buttons-trigger">
<button id="dropdown-buttons-trigger" class="btn btn-secondary topbarbtn"
aria-label="Connect with source repository"><i class="fab fa-github"></i></button>
<div class="dropdown-buttons sourcebuttons">
<a class="repository-button"
href="https://github.com/AllenDowney/AstronomicalData"><button type="button" class="btn btn-secondary topbarbtn"
data-toggle="tooltip" data-placement="left" title="Source repository"><i
class="fab fa-github"></i>repository</button></a>
</div>
</div>
<!-- Full screen (wrap in <a> to have style consistency -->
<a class="full-screen-button"><button type="button" class="btn btn-secondary topbarbtn" data-toggle="tooltip"
data-placement="bottom" onclick="toggleFullScreen()" title="Fullscreen mode"><i
class="fas fa-expand"></i></button></a>
<!-- Launch buttons -->
<div class="dropdown-buttons-trigger">
<button id="dropdown-buttons-trigger" class="btn btn-secondary topbarbtn"
aria-label="Launch interactive content"><i class="fas fa-rocket"></i></button>
<div class="dropdown-buttons">
<a class="binder-button" href="https://mybinder.org/v2/gh/AllenDowney/AstronomicalData/master?urlpath=tree/07_plot.ipynb"><button type="button"
class="btn btn-secondary topbarbtn" title="Launch Binder" data-toggle="tooltip"
data-placement="left"><img class="binder-button-logo"
src="_static/images/logo_binder.svg"
alt="Interact on binder">Binder</button></a>
<a class="colab-button" href="https://colab.research.google.com/github/AllenDowney/AstronomicalData/blob/master/07_plot.ipynb"><button type="button" class="btn btn-secondary topbarbtn"
title="Launch Colab" data-toggle="tooltip" data-placement="left"><img class="colab-button-logo"
src="_static/images/logo_colab.png"
alt="Interact on Colab">Colab</button></a>
</div>
</div>
</div>
<!-- Table of contents -->
<div class="d-none d-md-block col-md-2 bd-toc show">
<div class="tocsection onthispage pt-5 pb-3">
<i class="fas fa-list"></i> Contents
</div>
<nav id="bd-toc-nav">
<ul class="nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#outline">
Outline
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#making-figures-that-tell-a-story">
Making Figures That Tell a Story
</a>
<ul class="nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry">
<a class="reference internal nav-link" href="#exercise">
Exercise
</a>
</li>
</ul>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#plotting-gd-1">
Plotting GD-1
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#annotations">
Annotations
</a>
<ul class="nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry">
<a class="reference internal nav-link" href="#id1">
Exercise
</a>
</li>
</ul>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#customization">
Customization
</a>
<ul class="nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry">
<a class="reference internal nav-link" href="#id2">
Exercise
</a>
</li>
</ul>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#rcparams">
rcParams
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#style-sheets">
Style sheets
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#latex-fonts">
LaTeX fonts
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#multiple-panels">
Multiple panels
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#upper-right">
Upper right
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#upper-left">
Upper left
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#lower-right">
Lower right
</a>
<ul class="nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry">
<a class="reference internal nav-link" href="#id3">
Exercise
</a>
</li>
</ul>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#subplots">
Subplots
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#adjusting-proportions">
Adjusting proportions
</a>
<ul class="nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry">
<a class="reference internal nav-link" href="#id4">
Exercise
</a>
</li>
</ul>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#summary">
Summary
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#best-practices">
Best practices
</a>
</li>
</ul>
</nav>
</div>
</div>
<div id="main-content" class="row">
<div class="col-12 col-md-9 pl-md-3 pr-md-0">
<div>
<div class="section" id="visualization">
<h1>Visualization<a class="headerlink" href="#visualization" title="Permalink to this headline"></a></h1>
<p>This is the seventh in a series of notebooks related to astronomy data.</p>
<p>As a continuing example, we will replicate part of the analysis in a recent paper, “<a class="reference external" href="https://arxiv.org/abs/1805.00425">Off the beaten path: Gaia reveals GD-1 stars outside of the main stream</a>” by Adrian M. Price-Whelan and Ana Bonaca.</p>
<p>In the previous notebook we selected photometry data from Pan-STARRS and used it to identify stars we think are likely to be in GD-1</p>
<p>In this notebook, well take the results from previous lessons and use them to make a figure that tells a compelling scientific story.</p>
<div class="section" id="outline">
<h2>Outline<a class="headerlink" href="#outline" title="Permalink to this headline"></a></h2>
<p>Here are the steps in this notebook:</p>
<ol class="simple">
<li><p>Starting with the figure from the previous notebook, well add annotations to present the results more clearly.</p></li>
<li><p>The well see several ways to customize figures to make them more appealing and effective.</p></li>
<li><p>Finally, well see how to make a figure with multiple panels or subplots.</p></li>
</ol>
<p>After completing this lesson, you should be able to</p>
<ul class="simple">
<li><p>Design a figure that tells a compelling story.</p></li>
<li><p>Use Matplotlib features to customize the appearance of figures.</p></li>
<li><p>Generate a figure with multiple subplots.</p></li>
</ul>
</div>
<div class="section" id="making-figures-that-tell-a-story">
<h2>Making Figures That Tell a Story<a class="headerlink" href="#making-figures-that-tell-a-story" title="Permalink to this headline"></a></h2>
<p>So far the figure weve made have been “quick and dirty”. Mostly we have used Matplotlibs default style, although we have adjusted a few parameters, like <code class="docutils literal notranslate"><span class="pre">markersize</span></code> and <code class="docutils literal notranslate"><span class="pre">alpha</span></code>, to improve legibility.</p>
<p>Now that the analysis is done, its time to think more about:</p>
<ol class="simple">
<li><p>Making professional-looking figures that are ready for publication, and</p></li>
<li><p>Making figures that communicate a scientific result clearly and compellingly.</p></li>
</ol>
<p>Not necessarily in that order.</p>
<p>Lets start by reviewing Figure 1 from the original paper. Weve seen the individual panels, but now lets look at the whole thing, along with the caption:</p>
<a class="reference internal image-reference" href="https://github.com/datacarpentry/astronomy-python/raw/gh-pages/fig/gd1-5.png"><img alt="https://github.com/datacarpentry/astronomy-python/raw/gh-pages/fig/gd1-5.png" src="https://github.com/datacarpentry/astronomy-python/raw/gh-pages/fig/gd1-5.png" style="width: 500px;" /></a>
<div class="section" id="exercise">
<h3>Exercise<a class="headerlink" href="#exercise" title="Permalink to this headline"></a></h3>
<p>Think about the following questions:</p>
<ol class="simple">
<li><p>What is the primary scientific result of this work?</p></li>
<li><p>What story is this figure telling?</p></li>
<li><p>In the design of this figure, can you identify 1-2 choices the authors made that you think are effective? Think about big-picture elements, like the number of panels and how they are arranged, as well as details like the choice of typeface.</p></li>
<li><p>Can you identify 1-2 elements that could be improved, or that you might have done differently?</p></li>
</ol>
<div class="cell tag_hide-cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Solution</span>
<span class="c1"># Some topics that might come up in this discussion:</span>
<span class="c1"># 1. The primary result is that the multiple stages of selection </span>
<span class="c1"># make it possible to separate likely candidates from the </span>
<span class="c1"># background more effectively than in previous work, which makes </span>
<span class="c1"># it possible to see the structure of GD-1 in &quot;unprecedented detail&quot;.</span>
<span class="c1"># 2. The figure documents the selection process as a sequence of </span>
<span class="c1"># steps. Reading right-to-left, top-to-bottom, we see selection </span>
<span class="c1"># based on proper motion, the results of the first selection, </span>
<span class="c1"># selection based on color and magnitude, and the results of the </span>
<span class="c1"># second selection. So this figure documents the methodology and </span>
<span class="c1"># presents the primary result.</span>
<span class="c1"># 3. It&#39;s mostly black and white, with minimal use of color, so </span>
<span class="c1"># it will work well in print. The annotations in the bottom </span>
<span class="c1"># left panel guide the reader to the most important results. </span>
<span class="c1"># It contains enough technical detail for a professional audience, </span>
<span class="c1"># but most of it is also comprehensible to a more general audience. </span>
<span class="c1"># The two left panels have the same dimensions and their axes are </span>
<span class="c1"># aligned.</span>
<span class="c1"># 4. Since the panels represent a sequence, it might be better to </span>
<span class="c1"># arrange them left-to-right. The placement and size of the axis </span>
<span class="c1"># labels could be tweaked. The entire figure could be a little </span>
<span class="c1"># bigger to match the width and proportion of the caption. </span>
<span class="c1"># The top left panel has unnused white space (but that leaves </span>
<span class="c1"># space for the annotations in the bottom left).</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="section" id="plotting-gd-1">
<h2>Plotting GD-1<a class="headerlink" href="#plotting-gd-1" title="Permalink to this headline"></a></h2>
<p>Lets start with the panel in the lower left. The following cell reloads the data.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">wget</span> <span class="kn">import</span> <span class="n">download</span>
<span class="n">filename</span> <span class="o">=</span> <span class="s1">&#39;gd1_data.hdf&#39;</span>
<span class="n">path</span> <span class="o">=</span> <span class="s1">&#39;https://github.com/AllenDowney/AstronomicalData/raw/main/data/&#39;</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">filename</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="n">download</span><span class="p">(</span><span class="n">path</span><span class="o">+</span><span class="n">filename</span><span class="p">))</span>
</pre></div>
</div>
</div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="n">winner_df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_hdf</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s1">&#39;winner_df&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
<span class="k">def</span> <span class="nf">plot_second_selection</span><span class="p">(</span><span class="n">df</span><span class="p">):</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s1">&#39;phi1&#39;</span><span class="p">]</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s1">&#39;phi2&#39;</span><span class="p">]</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s1">&#39;ko&#39;</span><span class="p">,</span> <span class="n">markersize</span><span class="o">=</span><span class="mf">0.7</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.9</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s1">&#39;$\phi_1$ [deg]&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s1">&#39;$\phi_2$ [deg]&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s1">&#39;Proper motion + photometry selection&#39;</span><span class="p">,</span> <span class="n">fontsize</span><span class="o">=</span><span class="s1">&#39;medium&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">axis</span><span class="p">(</span><span class="s1">&#39;equal&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<p>And heres what it looks like.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span><span class="mf">2.5</span><span class="p">))</span>
<span class="n">plot_second_selection</span><span class="p">(</span><span class="n">winner_df</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<img alt="_images/07_plot_14_0.png" src="_images/07_plot_14_0.png" />
</div>
</div>
</div>
<div class="section" id="annotations">
<h2>Annotations<a class="headerlink" href="#annotations" title="Permalink to this headline"></a></h2>
<p>The figure in the paper uses three other features to present the results more clearly and compellingly:</p>
<ul class="simple">
<li><p>A vertical dashed line to distinguish the previously undetected region of GD-1,</p></li>
<li><p>A label that identifies the new region, and</p></li>
<li><p>Several annotations that combine text and arrows to identify features of GD-1.</p></li>
</ul>
<div class="section" id="id1">
<h3>Exercise<a class="headerlink" href="#id1" title="Permalink to this headline"></a></h3>
<p>Choose any or all of these features and add them to the figure:</p>
<ul class="simple">
<li><p>To draw vertical lines, see <a class="reference external" href="https://matplotlib.org/3.3.1/api/_as_gen/matplotlib.pyplot.vlines.html"><code class="docutils literal notranslate"><span class="pre">plt.vlines</span></code></a> and <a class="reference external" href="https://matplotlib.org/3.3.1/api/_as_gen/matplotlib.pyplot.axvline.html#matplotlib.pyplot.axvline"><code class="docutils literal notranslate"><span class="pre">plt.axvline</span></code></a>.</p></li>
<li><p>To add text, see <a class="reference external" href="https://matplotlib.org/3.3.1/api/_as_gen/matplotlib.pyplot.text.html"><code class="docutils literal notranslate"><span class="pre">plt.text</span></code></a>.</p></li>
<li><p>To add an annotation with text and an arrow, see <span class="xref myst">plt.annotate</span>.</p></li>
</ul>
<p>And here is some <a class="reference external" href="https://matplotlib.org/3.3.1/tutorials/text/annotations.html#plotting-guide-annotation">additional information about text and arrows</a>.</p>
<div class="cell tag_hide-cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Solution</span>
<span class="c1"># plt.axvline(-55, ls=&#39;--&#39;, color=&#39;gray&#39;, </span>
<span class="c1"># alpha=0.4, dashes=(6,4), lw=2)</span>
<span class="c1"># plt.text(-60, 5.5, &#39;Previously\nundetected&#39;, </span>
<span class="c1"># fontsize=&#39;small&#39;, ha=&#39;right&#39;, va=&#39;top&#39;);</span>
<span class="c1"># arrowprops=dict(color=&#39;gray&#39;, shrink=0.05, width=1.5, </span>
<span class="c1"># headwidth=6, headlength=8, alpha=0.4)</span>
<span class="c1"># plt.annotate(&#39;Spur&#39;, xy=(-33, 2), xytext=(-35, 5.5),</span>
<span class="c1"># arrowprops=arrowprops,</span>
<span class="c1"># fontsize=&#39;small&#39;)</span>
<span class="c1"># plt.annotate(&#39;Gap&#39;, xy=(-22, -1), xytext=(-25, -5.5),</span>
<span class="c1"># arrowprops=arrowprops,</span>
<span class="c1"># fontsize=&#39;small&#39;)</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="section" id="customization">
<h2>Customization<a class="headerlink" href="#customization" title="Permalink to this headline"></a></h2>
<p>Matplotlib provides a default style that determines things like the colors of lines, the placement of labels and ticks on the axes, and many other properties.</p>
<p>There are several ways to override these defaults and customize your figures:</p>
<ul class="simple">
<li><p>To customize only the current figure, you can call functions like <code class="docutils literal notranslate"><span class="pre">tick_params</span></code>, which well demonstrate below.</p></li>
<li><p>To customize all figures in a notebook, you use <code class="docutils literal notranslate"><span class="pre">rcParams</span></code>.</p></li>
<li><p>To override more than a few defaults at the same time, you can use a style sheet.</p></li>
</ul>
<p>As a simple example, notice that Matplotlib puts ticks on the outside of the figures by default, and only on the left and bottom sides of the axes.</p>
<p>To change this behavior, you can use <code class="docutils literal notranslate"><span class="pre">gca()</span></code> to get the current axes and <code class="docutils literal notranslate"><span class="pre">tick_params</span></code> to change the settings.</p>
<p>Heres how you can put the ticks on the inside of the figure:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">plt</span><span class="o">.</span><span class="n">gca</span><span class="p">()</span><span class="o">.</span><span class="n">tick_params</span><span class="p">(</span><span class="n">direction</span><span class="o">=</span><span class="s1">&#39;in&#39;</span><span class="p">)</span>
</pre></div>
</div>
<div class="section" id="id2">
<h3>Exercise<a class="headerlink" href="#id2" title="Permalink to this headline"></a></h3>
<p>Read the documentation of <a class="reference external" href="https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.axes.Axes.tick_params.html"><code class="docutils literal notranslate"><span class="pre">tick_params</span></code></a> and use it to put ticks on the top and right sides of the axes.</p>
<div class="cell tag_hide-cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Solution</span>
<span class="c1"># plt.gca().tick_params(top=True, right=True)</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="section" id="rcparams">
<h2>rcParams<a class="headerlink" href="#rcparams" title="Permalink to this headline"></a></h2>
<p>If you want to make a customization that applies to all figures in a notebook, you can use <code class="docutils literal notranslate"><span class="pre">rcParams</span></code>.</p>
<p>Heres an example that reads the current font size from <code class="docutils literal notranslate"><span class="pre">rcParams</span></code>:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">plt</span><span class="o">.</span><span class="n">rcParams</span><span class="p">[</span><span class="s1">&#39;font.size&#39;</span><span class="p">]</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>10.0
</pre></div>
</div>
</div>
</div>
<p>And sets it to a new value:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">plt</span><span class="o">.</span><span class="n">rcParams</span><span class="p">[</span><span class="s1">&#39;font.size&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">14</span>
</pre></div>
</div>
</div>
</div>
<p>As an exercise, plot the previous figure again, and see what font sizes have changed. Look up any other element of <code class="docutils literal notranslate"><span class="pre">rcParams</span></code>, change its value, and check the effect on the figure.</p>
<p>If you find yourself making the same customizations in several notebooks, you can put changes to <code class="docutils literal notranslate"><span class="pre">rcParams</span></code> in a <code class="docutils literal notranslate"><span class="pre">matplotlibrc</span></code> file, <a class="reference external" href="https://matplotlib.org/3.3.1/tutorials/introductory/customizing.html#customizing-with-matplotlibrc-files">which you can read about here</a>.</p>
</div>
<div class="section" id="style-sheets">
<h2>Style sheets<a class="headerlink" href="#style-sheets" title="Permalink to this headline"></a></h2>
<p>The <code class="docutils literal notranslate"><span class="pre">matplotlibrc</span></code> file is read when you import Matplotlib, so it is not easy to switch from one set of options to another.</p>
<p>The solution to this problem is style sheets, <a class="reference external" href="https://matplotlib.org/3.1.1/tutorials/introductory/customizing.html">which you can read about here</a>.</p>
<p>Matplotlib provides a set of predefined style sheets, or you can make your own.</p>
<p>The following cell displays a list of style sheets installed on your system.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">plt</span><span class="o">.</span><span class="n">style</span><span class="o">.</span><span class="n">available</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[&#39;Solarize_Light2&#39;,
&#39;_classic_test_patch&#39;,
&#39;bmh&#39;,
&#39;classic&#39;,
&#39;dark_background&#39;,
&#39;fast&#39;,
&#39;fivethirtyeight&#39;,
&#39;ggplot&#39;,
&#39;grayscale&#39;,
&#39;seaborn&#39;,
&#39;seaborn-bright&#39;,
&#39;seaborn-colorblind&#39;,
&#39;seaborn-dark&#39;,
&#39;seaborn-dark-palette&#39;,
&#39;seaborn-darkgrid&#39;,
&#39;seaborn-deep&#39;,
&#39;seaborn-muted&#39;,
&#39;seaborn-notebook&#39;,
&#39;seaborn-paper&#39;,
&#39;seaborn-pastel&#39;,
&#39;seaborn-poster&#39;,
&#39;seaborn-talk&#39;,
&#39;seaborn-ticks&#39;,
&#39;seaborn-white&#39;,
&#39;seaborn-whitegrid&#39;,
&#39;tableau-colorblind10&#39;]
</pre></div>
</div>
</div>
</div>
<p>Note that <code class="docutils literal notranslate"><span class="pre">seaborn-paper</span></code>, <code class="docutils literal notranslate"><span class="pre">seaborn-talk</span></code> and <code class="docutils literal notranslate"><span class="pre">seaborn-poster</span></code> are particularly intended to prepare versions of a figure with text sizes and other features that work well in papers, talks, and posters.</p>
<p>To use any of these style sheets, run <code class="docutils literal notranslate"><span class="pre">plt.style.use</span></code> like this:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">plt</span><span class="o">.</span><span class="n">style</span><span class="o">.</span><span class="n">use</span><span class="p">(</span><span class="s1">&#39;fivethirtyeight&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>The style sheet you choose will affect the appearance of all figures you plot after calling <code class="docutils literal notranslate"><span class="pre">use</span></code>, unless you override any of the options or call <code class="docutils literal notranslate"><span class="pre">use</span></code> again.</p>
<p>As an exercise, choose one of the styles on the list and select it by calling <code class="docutils literal notranslate"><span class="pre">use</span></code>. Then go back and plot one of the figures above and see what effect it has.</p>
<p>If you cant find a style sheet thats exactly what you want, you can make your own. This repository includes a style sheet called <code class="docutils literal notranslate"><span class="pre">az-paper-twocol.mplstyle</span></code>, with customizations chosen by Azalee Bostroem for publication in astronomy journals.</p>
<p>The following cell downloads the style sheet.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">wget</span> <span class="kn">import</span> <span class="n">download</span>
<span class="n">filename</span> <span class="o">=</span> <span class="s1">&#39;az-paper-twocol.mplstyle&#39;</span>
<span class="n">path</span> <span class="o">=</span> <span class="s1">&#39;https://github.com/AllenDowney/AstronomicalData/raw/main/&#39;</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">filename</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="n">download</span><span class="p">(</span><span class="n">path</span><span class="o">+</span><span class="n">filename</span><span class="p">))</span>
</pre></div>
</div>
</div>
</div>
<p>You can use it like this:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">plt</span><span class="o">.</span><span class="n">style</span><span class="o">.</span><span class="n">use</span><span class="p">(</span><span class="s1">&#39;./az-paper-twocol.mplstyle&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>The prefix <code class="docutils literal notranslate"><span class="pre">./</span></code> tells Matplotlib to look for the file in the current directory.</p>
<p>As an alternative, you can install a style sheet for your own use by putting it in your configuration directory. To find out where that is, you can run the following command:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">matplotlib</span> <span class="k">as</span> <span class="nn">mpl</span>
<span class="n">mpl</span><span class="o">.</span><span class="n">get_configdir</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="latex-fonts">
<h2>LaTeX fonts<a class="headerlink" href="#latex-fonts" title="Permalink to this headline"></a></h2>
<p>When you include mathematical expressions in titles, labels, and annotations, Matplotlib uses <a class="reference external" href="https://matplotlib.org/3.1.0/tutorials/text/mathtext.html"><code class="docutils literal notranslate"><span class="pre">mathtext</span></code></a> to typeset them. <code class="docutils literal notranslate"><span class="pre">mathtext</span></code> uses the same syntax as LaTeX, but it provides only a subset of its features.</p>
<p>If you need features that are not provided by <code class="docutils literal notranslate"><span class="pre">mathtext</span></code>, or you prefer the way LaTeX typesets mathematical expressions, you can customize Matplotlib to use LaTeX.</p>
<p>In <code class="docutils literal notranslate"><span class="pre">matplotlibrc</span></code> or in a style sheet, you can add the following line:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">text</span><span class="o">.</span><span class="n">usetex</span> <span class="p">:</span> <span class="n">true</span>
</pre></div>
</div>
<p>Or in a notebook you can run the following code.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">plt</span><span class="o">.</span><span class="n">rcParams</span><span class="p">[</span><span class="s1">&#39;text.usetex&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
</pre></div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">plt</span><span class="o">.</span><span class="n">rcParams</span><span class="p">[</span><span class="s1">&#39;text.usetex&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
</pre></div>
</div>
</div>
</div>
<p>If you go back and draw the figure again, you should see the difference.</p>
<p>If you get an error message like</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>LaTeX Error: File `type1cm.sty&#39; not found.
</pre></div>
</div>
<p>You might have to install a package that contains the fonts LaTeX needs. On some systems, the packages <code class="docutils literal notranslate"><span class="pre">texlive-latex-extra</span></code> or <code class="docutils literal notranslate"><span class="pre">cm-super</span></code> might be what you need. <a class="reference external" href="https://stackoverflow.com/questions/11354149/python-unable-to-render-tex-in-matplotlib">See here for more help with this</a>.</p>
<p>In case you are curious, <code class="docutils literal notranslate"><span class="pre">cm</span></code> stands for <a class="reference external" href="https://en.wikipedia.org/wiki/Computer_Modern">Computer Modern</a>, the font LaTeX uses to typeset math.</p>
<p>Before we go on, lets put things back where we found them.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">plt</span><span class="o">.</span><span class="n">rcParams</span><span class="p">[</span><span class="s1">&#39;text.usetex&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>
<span class="n">plt</span><span class="o">.</span><span class="n">style</span><span class="o">.</span><span class="n">use</span><span class="p">(</span><span class="s1">&#39;default&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="section" id="multiple-panels">
<h2>Multiple panels<a class="headerlink" href="#multiple-panels" title="Permalink to this headline"></a></h2>
<p>So far weve been working with one figure at a time, but the figure we are replicating contains multiple panels, also known as “subplots”.</p>
<p>Confusingly, Matplotlib provides <em>three</em> functions for making figures like this: <code class="docutils literal notranslate"><span class="pre">subplot</span></code>, <code class="docutils literal notranslate"><span class="pre">subplots</span></code>, and <code class="docutils literal notranslate"><span class="pre">subplot2grid</span></code>.</p>
<ul class="simple">
<li><p><a class="reference external" href="https://matplotlib.org/3.3.1/api/_as_gen/matplotlib.pyplot.subplot.html"><code class="docutils literal notranslate"><span class="pre">subplot</span></code></a> is simple and similar to MATLAB, so if you are familiar with that interface, you might like <code class="docutils literal notranslate"><span class="pre">subplot</span></code></p></li>
<li><p><a class="reference external" href="https://matplotlib.org/3.3.1/api/_as_gen/matplotlib.pyplot.subplots.html"><code class="docutils literal notranslate"><span class="pre">subplots</span></code></a> is more object-oriented, which some people prefer.</p></li>
<li><p><a class="reference external" href="https://matplotlib.org/3.3.1/api/_as_gen/matplotlib.pyplot.subplot2grid.html"><code class="docutils literal notranslate"><span class="pre">subplot2grid</span></code></a> is most convenient if you want to control the relative sizes of the subplots.</p></li>
</ul>
<p>So well use <code class="docutils literal notranslate"><span class="pre">subplot2grid</span></code>.</p>
<p>All of these functions are easier to use if we put the code that generates each panel in a function.</p>
</div>
<div class="section" id="upper-right">
<h2>Upper right<a class="headerlink" href="#upper-right" title="Permalink to this headline"></a></h2>
<p>To make the panel in the upper right, we have to reload <code class="docutils literal notranslate"><span class="pre">centerline_df</span></code>.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">filename</span> <span class="o">=</span> <span class="s1">&#39;gd1_data.hdf&#39;</span>
<span class="n">centerline_df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_hdf</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s1">&#39;centerline_df&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<p>And define the coordinates of the rectangle we selected.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">pm1_min</span> <span class="o">=</span> <span class="o">-</span><span class="mf">8.9</span>
<span class="n">pm1_max</span> <span class="o">=</span> <span class="o">-</span><span class="mf">6.9</span>
<span class="n">pm2_min</span> <span class="o">=</span> <span class="o">-</span><span class="mf">2.2</span>
<span class="n">pm2_max</span> <span class="o">=</span> <span class="mf">1.0</span>
<span class="n">pm1_rect</span> <span class="o">=</span> <span class="p">[</span><span class="n">pm1_min</span><span class="p">,</span> <span class="n">pm1_min</span><span class="p">,</span> <span class="n">pm1_max</span><span class="p">,</span> <span class="n">pm1_max</span><span class="p">]</span>
<span class="n">pm2_rect</span> <span class="o">=</span> <span class="p">[</span><span class="n">pm2_min</span><span class="p">,</span> <span class="n">pm2_max</span><span class="p">,</span> <span class="n">pm2_max</span><span class="p">,</span> <span class="n">pm2_min</span><span class="p">]</span>
</pre></div>
</div>
</div>
</div>
<p>To plot this rectangle, well use a feature we have not seen before: <code class="docutils literal notranslate"><span class="pre">Polygon</span></code>, which is provided by Matplotlib.</p>
<p>To create a <code class="docutils literal notranslate"><span class="pre">Polygon</span></code>, we have to put the coordinates in an array with <code class="docutils literal notranslate"><span class="pre">x</span></code> values in the first column and <code class="docutils literal notranslate"><span class="pre">y</span></code> values in the second column.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="n">vertices</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">transpose</span><span class="p">([</span><span class="n">pm1_rect</span><span class="p">,</span> <span class="n">pm2_rect</span><span class="p">])</span>
<span class="n">vertices</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>array([[-8.9, -2.2],
[-8.9, 1. ],
[-6.9, 1. ],
[-6.9, -2.2]])
</pre></div>
</div>
</div>
</div>
<p>The following function takes a <code class="docutils literal notranslate"><span class="pre">DataFrame</span></code> as a parameter, plots the proper motion for each star, and adds a shaded <code class="docutils literal notranslate"><span class="pre">Polygon</span></code> to show the region we selected.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">matplotlib.patches</span> <span class="kn">import</span> <span class="n">Polygon</span>
<span class="k">def</span> <span class="nf">plot_proper_motion</span><span class="p">(</span><span class="n">df</span><span class="p">):</span>
<span class="n">pm1</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s1">&#39;pm_phi1&#39;</span><span class="p">]</span>
<span class="n">pm2</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s1">&#39;pm_phi2&#39;</span><span class="p">]</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">pm1</span><span class="p">,</span> <span class="n">pm2</span><span class="p">,</span> <span class="s1">&#39;ko&#39;</span><span class="p">,</span> <span class="n">markersize</span><span class="o">=</span><span class="mf">0.3</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.3</span><span class="p">)</span>
<span class="n">poly</span> <span class="o">=</span> <span class="n">Polygon</span><span class="p">(</span><span class="n">vertices</span><span class="p">,</span> <span class="n">closed</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
<span class="n">facecolor</span><span class="o">=</span><span class="s1">&#39;C1&#39;</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.4</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">gca</span><span class="p">()</span><span class="o">.</span><span class="n">add_patch</span><span class="p">(</span><span class="n">poly</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s1">&#39;$\mu_{\phi_1} [\mathrm{mas~yr}^{-1}]$&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s1">&#39;$\mu_{\phi_2} [\mathrm{mas~yr}^{-1}]$&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">xlim</span><span class="p">(</span><span class="o">-</span><span class="mi">12</span><span class="p">,</span> <span class="mi">8</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylim</span><span class="p">(</span><span class="o">-</span><span class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<p>Notice that <code class="docutils literal notranslate"><span class="pre">add_patch</span></code> is like <code class="docutils literal notranslate"><span class="pre">invert_yaxis</span></code>; in order to call it, we have to use <code class="docutils literal notranslate"><span class="pre">gca</span></code> to get the current axes.</p>
<p>Heres what the new version of the figure looks like. Weve changed the labels on the axes to be consistent with the paper.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">plot_proper_motion</span><span class="p">(</span><span class="n">centerline_df</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<img alt="_images/07_plot_54_0.png" src="_images/07_plot_54_0.png" />
</div>
</div>
</div>
<div class="section" id="upper-left">
<h2>Upper left<a class="headerlink" href="#upper-left" title="Permalink to this headline"></a></h2>
<p>Now lets work on the panel in the upper left. We have to reload <code class="docutils literal notranslate"><span class="pre">candidates</span></code>.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">filename</span> <span class="o">=</span> <span class="s1">&#39;gd1_data.hdf&#39;</span>
<span class="n">candidate_df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_hdf</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s1">&#39;candidate_df&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<p>Heres a function that takes a <code class="docutils literal notranslate"><span class="pre">DataFrame</span></code> of candidate stars and plots their positions in GD-1 coordindates.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">plot_first_selection</span><span class="p">(</span><span class="n">df</span><span class="p">):</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s1">&#39;phi1&#39;</span><span class="p">]</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s1">&#39;phi2&#39;</span><span class="p">]</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s1">&#39;ko&#39;</span><span class="p">,</span> <span class="n">markersize</span><span class="o">=</span><span class="mf">0.3</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.3</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s1">&#39;$\phi_1$ [deg]&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s1">&#39;$\phi_2$ [deg]&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s1">&#39;Proper motion selection&#39;</span><span class="p">,</span> <span class="n">fontsize</span><span class="o">=</span><span class="s1">&#39;medium&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">axis</span><span class="p">(</span><span class="s1">&#39;equal&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<p>And heres what it looks like.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">plot_first_selection</span><span class="p">(</span><span class="n">candidate_df</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<img alt="_images/07_plot_60_0.png" src="_images/07_plot_60_0.png" />
</div>
</div>
</div>
<div class="section" id="lower-right">
<h2>Lower right<a class="headerlink" href="#lower-right" title="Permalink to this headline"></a></h2>
<p>For the figure in the lower right, well use this function to plots the color-magnitude diagram.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
<span class="k">def</span> <span class="nf">plot_cmd</span><span class="p">(</span><span class="n">table</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Plot a color magnitude diagram.</span>
<span class="sd"> </span>
<span class="sd"> table: Table or DataFrame with photometry data</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">table</span><span class="p">[</span><span class="s1">&#39;g_mean_psf_mag&#39;</span><span class="p">]</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">table</span><span class="p">[</span><span class="s1">&#39;g_mean_psf_mag&#39;</span><span class="p">]</span> <span class="o">-</span> <span class="n">table</span><span class="p">[</span><span class="s1">&#39;i_mean_psf_mag&#39;</span><span class="p">]</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s1">&#39;ko&#39;</span><span class="p">,</span> <span class="n">markersize</span><span class="o">=</span><span class="mf">0.3</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.3</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">xlim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mf">1.5</span><span class="p">])</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylim</span><span class="p">([</span><span class="mi">14</span><span class="p">,</span> <span class="mi">22</span><span class="p">])</span>
<span class="n">plt</span><span class="o">.</span><span class="n">gca</span><span class="p">()</span><span class="o">.</span><span class="n">invert_yaxis</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s1">&#39;$Magnitude (g)$&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s1">&#39;$Color (g-i)$&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<p>Heres what it looks like.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">plot_cmd</span><span class="p">(</span><span class="n">candidate_df</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<img alt="_images/07_plot_64_0.png" src="_images/07_plot_64_0.png" />
</div>
</div>
<p>And heres how we read it back.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">filename</span> <span class="o">=</span> <span class="s1">&#39;gd1_data.hdf&#39;</span>
<span class="n">loop_df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_hdf</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s1">&#39;loop_df&#39;</span><span class="p">)</span>
<span class="n">loop_df</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_html"><div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>color_loop</th>
<th>mag_loop</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>0.632171</td>
<td>21.411746</td>
</tr>
<tr>
<th>1</th>
<td>0.610238</td>
<td>21.322466</td>
</tr>
<tr>
<th>2</th>
<td>0.588449</td>
<td>21.233380</td>
</tr>
<tr>
<th>3</th>
<td>0.566924</td>
<td>21.144427</td>
</tr>
<tr>
<th>4</th>
<td>0.545461</td>
<td>21.054549</td>
</tr>
</tbody>
</table>
</div></div></div>
</div>
<div class="section" id="id3">
<h3>Exercise<a class="headerlink" href="#id3" title="Permalink to this headline"></a></h3>
<p>Add a few lines to <code class="docutils literal notranslate"><span class="pre">plot_cmd</span></code> to show the polygon we selected as a shaded area.</p>
<p>Hint: pass <code class="docutils literal notranslate"><span class="pre">coords</span></code> as an argument to <code class="docutils literal notranslate"><span class="pre">Polygon</span></code> and plot it using <code class="docutils literal notranslate"><span class="pre">add_patch</span></code>.</p>
<div class="cell tag_hide-cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Solution</span>
<span class="c1"># poly = Polygon(loop_df, closed=True, </span>
<span class="c1"># facecolor=&#39;C1&#39;, alpha=0.4)</span>
<span class="c1"># plt.gca().add_patch(poly)</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="section" id="subplots">
<h2>Subplots<a class="headerlink" href="#subplots" title="Permalink to this headline"></a></h2>
<p>Now were ready to put it all together. To make a figure with four subplots, well use <code class="docutils literal notranslate"><span class="pre">subplot2grid</span></code>, <a class="reference external" href="https://matplotlib.org/3.3.1/api/_as_gen/matplotlib.pyplot.subplot2grid.html">which requires two arguments</a>:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">shape</span></code>, which is a tuple with the number of rows and columns in the grid, and</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">loc</span></code>, which is a tuple identifying the location in the grid were about to fill.</p></li>
</ul>
<p>In this example, <code class="docutils literal notranslate"><span class="pre">shape</span></code> is <code class="docutils literal notranslate"><span class="pre">(2,</span> <span class="pre">2)</span></code> to create two rows and two columns.</p>
<p>For the first panel, <code class="docutils literal notranslate"><span class="pre">loc</span></code> is <code class="docutils literal notranslate"><span class="pre">(0,</span> <span class="pre">0)</span></code>, which indicates row 0 and column 0, which is the upper-left panel.</p>
<p>Heres how we use it to draw the four panels.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">shape</span> <span class="o">=</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">subplot2grid</span><span class="p">(</span><span class="n">shape</span><span class="p">,</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span>
<span class="n">plot_first_selection</span><span class="p">(</span><span class="n">candidate_df</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">subplot2grid</span><span class="p">(</span><span class="n">shape</span><span class="p">,</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
<span class="n">plot_proper_motion</span><span class="p">(</span><span class="n">centerline_df</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">subplot2grid</span><span class="p">(</span><span class="n">shape</span><span class="p">,</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span>
<span class="n">plot_second_selection</span><span class="p">(</span><span class="n">winner_df</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">subplot2grid</span><span class="p">(</span><span class="n">shape</span><span class="p">,</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
<span class="n">plot_cmd</span><span class="p">(</span><span class="n">candidate_df</span><span class="p">)</span>
<span class="n">poly</span> <span class="o">=</span> <span class="n">Polygon</span><span class="p">(</span><span class="n">loop_df</span><span class="p">,</span> <span class="n">closed</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
<span class="n">facecolor</span><span class="o">=</span><span class="s1">&#39;C1&#39;</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.4</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">gca</span><span class="p">()</span><span class="o">.</span><span class="n">add_patch</span><span class="p">(</span><span class="n">poly</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">tight_layout</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<img alt="_images/07_plot_70_0.png" src="_images/07_plot_70_0.png" />
</div>
</div>
<p>We use <a class="reference external" href="https://matplotlib.org/3.3.1/tutorials/intermediate/tight_layout_guide.html"><code class="docutils literal notranslate"><span class="pre">plt.tight_layout</span></code></a> at the end, which adjusts the sizes of the panels to make sure the titles and axis labels dont overlap.</p>
<p>As an exercise, see what happens if you leave out <code class="docutils literal notranslate"><span class="pre">tight_layout</span></code>.</p>
</div>
<div class="section" id="adjusting-proportions">
<h2>Adjusting proportions<a class="headerlink" href="#adjusting-proportions" title="Permalink to this headline"></a></h2>
<p>In the previous figure, the panels are all the same size. To get a better view of GD-1, wed like to stretch the panels on the left and compress the ones on the right.</p>
<p>To do that, well use the <code class="docutils literal notranslate"><span class="pre">colspan</span></code> argument to make a panel that spans multiple columns in the grid.</p>
<p>In the following example, <code class="docutils literal notranslate"><span class="pre">shape</span></code> is <code class="docutils literal notranslate"><span class="pre">(2,</span> <span class="pre">4)</span></code>, which means 2 rows and 4 columns.</p>
<p>The panels on the left span three columns, so they are three times wider than the panels on the right.</p>
<p>At the same time, we use <code class="docutils literal notranslate"><span class="pre">figsize</span></code> to adjust the aspect ratio of the whole figure.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">9</span><span class="p">,</span> <span class="mf">4.5</span><span class="p">))</span>
<span class="n">shape</span> <span class="o">=</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">subplot2grid</span><span class="p">(</span><span class="n">shape</span><span class="p">,</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> <span class="n">colspan</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="n">plot_first_selection</span><span class="p">(</span><span class="n">candidate_df</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">subplot2grid</span><span class="p">(</span><span class="n">shape</span><span class="p">,</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
<span class="n">plot_proper_motion</span><span class="p">(</span><span class="n">centerline_df</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">subplot2grid</span><span class="p">(</span><span class="n">shape</span><span class="p">,</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> <span class="n">colspan</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="n">plot_second_selection</span><span class="p">(</span><span class="n">winner_df</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">subplot2grid</span><span class="p">(</span><span class="n">shape</span><span class="p">,</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
<span class="n">plot_cmd</span><span class="p">(</span><span class="n">candidate_df</span><span class="p">)</span>
<span class="n">poly</span> <span class="o">=</span> <span class="n">Polygon</span><span class="p">(</span><span class="n">loop_df</span><span class="p">,</span> <span class="n">closed</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
<span class="n">facecolor</span><span class="o">=</span><span class="s1">&#39;C1&#39;</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.4</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">gca</span><span class="p">()</span><span class="o">.</span><span class="n">add_patch</span><span class="p">(</span><span class="n">poly</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">tight_layout</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<img alt="_images/07_plot_73_0.png" src="_images/07_plot_73_0.png" />
</div>
</div>
<p>This is looking more and more like the figure in the paper.</p>
<div class="section" id="id4">
<h3>Exercise<a class="headerlink" href="#id4" title="Permalink to this headline"></a></h3>
<p>In this example, the ratio of the widths of the panels is 3:1. How would you adjust it if you wanted the ratio to be 3:2?</p>
<div class="cell tag_hide-cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Solution</span>
<span class="c1"># plt.figure(figsize=(9, 4.5))</span>
<span class="c1"># shape = (2, 5) # CHANGED</span>
<span class="c1"># plt.subplot2grid(shape, (0, 0), colspan=3)</span>
<span class="c1"># plot_first_selection(candidate_df)</span>
<span class="c1"># plt.subplot2grid(shape, (0, 3), colspan=2) # CHANGED</span>
<span class="c1"># plot_proper_motion(centerline_df)</span>
<span class="c1"># plt.subplot2grid(shape, (1, 0), colspan=3)</span>
<span class="c1"># plot_second_selection(winner_df)</span>
<span class="c1"># plt.subplot2grid(shape, (1, 3), colspan=2) # CHANGED</span>
<span class="c1"># plot_cmd(candidate_df)</span>
<span class="c1"># poly = Polygon(coords, closed=True, </span>
<span class="c1"># facecolor=&#39;C1&#39;, alpha=0.4)</span>
<span class="c1"># plt.gca().add_patch(poly)</span>
<span class="c1"># plt.tight_layout()</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="section" id="summary">
<h2>Summary<a class="headerlink" href="#summary" title="Permalink to this headline"></a></h2>
<p>In this notebook, we reverse-engineered the figure weve been replicating, identifying elements that seem effective and others that could be improved.</p>
<p>We explored features Matplotlib provides for adding annotations to figures including text, lines, arrows, and polygons and several ways to customize the appearance of figures. And we learned how to create figures that contain multiple panels.</p>
</div>
<div class="section" id="best-practices">
<h2>Best practices<a class="headerlink" href="#best-practices" title="Permalink to this headline"></a></h2>
<ul class="simple">
<li><p>The most effective figures focus on telling a single story clearly and compellingly.</p></li>
<li><p>Consider using annotations to guide the readers attention to the most important elements of a figure.</p></li>
<li><p>The default Matplotlib style generates good quality figures, but there are several ways you can override the defaults.</p></li>
<li><p>If you find yourself making the same customizations on several projects, you might want to create your own style sheet.</p></li>
</ul>
</div>
</div>
<script type="text/x-thebe-config">
{
requestKernel: true,
binderOptions: {
repo: "binder-examples/jupyter-stacks-datascience",
ref: "master",
},
codeMirrorConfig: {
theme: "abcdef",
mode: "python"
},
kernelOptions: {
kernelName: "python3",
path: "./."
},
predefinedOutput: true
}
</script>
<script>kernelName = 'python3'</script>
</div>
</div>
</div>
<div class='prev-next-bottom'>
<a class='left-prev' id="prev-link" href="06_photo.html" title="previous page">6. Photometry</a>
</div>
<footer class="footer mt-5 mt-md-0">
<div class="container">
<p>
By Allen B. Downey<br/>
&copy; Copyright 2020.<br/>
</p>
</div>
</footer>
</main>
</div>
</div>
<script src="_static/js/index.30270b6e4c972e43c488.js"></script>
</body>
</html>