mirror of
https://github.com/AllenDowney/AstronomicalData.git
synced 2025-12-22 23:26:35 -08:00
1047 lines
72 KiB
HTML
1047 lines
72 KiB
HTML
|
||
<!DOCTYPE html>
|
||
|
||
<html>
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||
<title>Visualization — 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="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">
|
||
Queries
|
||
</a>
|
||
</li>
|
||
<li class="toctree-l1">
|
||
<a class="reference internal" href="02_coords.html">
|
||
Coordinates and units
|
||
</a>
|
||
</li>
|
||
<li class="toctree-l1">
|
||
<a class="reference internal" href="03_motion.html">
|
||
Proper Motion
|
||
</a>
|
||
</li>
|
||
<li class="toctree-l1">
|
||
<a class="reference internal" href="04_select.html">
|
||
Transformation and Selection
|
||
</a>
|
||
</li>
|
||
<li class="toctree-l1">
|
||
<a class="reference internal" href="05_join.html">
|
||
Joining Tables
|
||
</a>
|
||
</li>
|
||
<li class="toctree-l1">
|
||
<a class="reference internal" href="06_photo.html">
|
||
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>
|
||
</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>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#customization">
|
||
Customization
|
||
</a>
|
||
</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>
|
||
</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>
|
||
</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, we’ll 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, we’ll add annotations to present the results more clearly.</p></li>
|
||
<li><p>The we’ll see several ways to customize figures to make them more appealing and effective.</p></li>
|
||
<li><p>Finally, we’ll 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 we’ve made have been “quick and dirty”. Mostly we have used Matplotlib’s 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, it’s 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>Let’s start by reviewing Figure 1 from the original paper. We’ve seen the individual panels, but now let’s 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>
|
||
<p><strong>Exercise:</strong> 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>
|
||
<p>Some topics that might come up in this discussion:</p>
|
||
<ol class="simple">
|
||
<li><p>The primary result is that the multiple stages of selection make it possible to separate likely candidates from the background more effectively than in previous work, which makes it possible to see the structure of GD-1 in “unprecedented detail”.</p></li>
|
||
<li><p>The figure documents the selection process as a sequence of steps. Reading right-to-left, top-to-bottom, we see selection based on proper motion, the results of the first selection, selection based on color and magnitude, and the results of the second selection. So this figure documents the methodology and presents the primary result.</p></li>
|
||
<li><p>It’s mostly black and white, with minimal use of color, so it will work well in print. The annotations in the bottom left panel guide the reader to the most important results. It contains enough technical detail for a professional audience, but most of it is also comprehensible to a more general audience. The two left panels have the same dimensions and their axes are aligned.</p></li>
|
||
<li><p>Since the panels represent a sequence, it might be better to arrange them left-to-right. The placement and size of the axis labels could be tweaked. The entire figure could be a little bigger to match the width and proportion of the caption. The top left panel has unnused white space (but that leaves space for the annotations in the bottom left).</p></li>
|
||
</ol>
|
||
</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>Let’s 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">'gd1_merged.hdf5'</span>
|
||
<span class="n">path</span> <span class="o">=</span> <span class="s1">'https://github.com/AllenDowney/AstronomicalData/raw/main/data/'</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">selected</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">'selected'</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">'phi1'</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">'phi2'</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">'ko'</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">'$\phi_1$ [deg]'</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">'$\phi_2$ [deg]'</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">'Proper motion + photometry selection'</span><span class="p">,</span> <span class="n">fontsize</span><span class="o">=</span><span class="s1">'medium'</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">'equal'</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>And here’s 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">selected</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>
|
||
<p>As an exercise, 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='--', color='gray', </span>
|
||
<span class="c1"># alpha=0.4, dashes=(6,4), lw=2)</span>
|
||
<span class="c1"># plt.text(-60, 5.5, 'Previously\nundetected', </span>
|
||
<span class="c1"># fontsize='small', ha='right', va='top');</span>
|
||
|
||
<span class="c1"># arrowprops=dict(color='gray', shrink=0.05, width=1.5, </span>
|
||
<span class="c1"># headwidth=6, headlength=8, alpha=0.4)</span>
|
||
|
||
<span class="c1"># plt.annotate('Spur', xy=(-33, 2), xytext=(-35, 5.5),</span>
|
||
<span class="c1"># arrowprops=arrowprops,</span>
|
||
<span class="c1"># fontsize='small')</span>
|
||
|
||
<span class="c1"># plt.annotate('Gap', xy=(-22, -1), xytext=(-25, -5.5),</span>
|
||
<span class="c1"># arrowprops=arrowprops,</span>
|
||
<span class="c1"># fontsize='small')</span>
|
||
</pre></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 we’ll 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>Here’s 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">'in'</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p><strong>Exercise:</strong> 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 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>Here’s 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">'font.size'</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">'font.size'</span><span class="p">]</span> <span class="o">=</span> <span class="mi">14</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p><strong>Exercise:</strong> 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>['Solarize_Light2',
|
||
'_classic_test_patch',
|
||
'bmh',
|
||
'classic',
|
||
'dark_background',
|
||
'fast',
|
||
'fivethirtyeight',
|
||
'ggplot',
|
||
'grayscale',
|
||
'seaborn',
|
||
'seaborn-bright',
|
||
'seaborn-colorblind',
|
||
'seaborn-dark',
|
||
'seaborn-dark-palette',
|
||
'seaborn-darkgrid',
|
||
'seaborn-deep',
|
||
'seaborn-muted',
|
||
'seaborn-notebook',
|
||
'seaborn-paper',
|
||
'seaborn-pastel',
|
||
'seaborn-poster',
|
||
'seaborn-talk',
|
||
'seaborn-ticks',
|
||
'seaborn-white',
|
||
'seaborn-whitegrid',
|
||
'tableau-colorblind10']
|
||
</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">'fivethirtyeight'</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><strong>Exercise:</strong> 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 can’t find a style sheet that’s 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">'az-paper-twocol.mplstyle'</span>
|
||
<span class="n">path</span> <span class="o">=</span> <span class="s1">'https://github.com/AllenDowney/AstronomicalData/raw/main/'</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">'./az-paper-twocol.mplstyle'</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">'text.usetex'</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">'text.usetex'</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' 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>
|
||
</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 we’ve 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 we’ll 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</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="kn">import</span> <span class="nn">os</span>
|
||
|
||
<span class="n">filename</span> <span class="o">=</span> <span class="s1">'gd1_dataframe.hdf5'</span>
|
||
<span class="n">path</span> <span class="o">=</span> <span class="s1">'https://github.com/AllenDowney/AstronomicalData/raw/main/data/'</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">centerline</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">'centerline'</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, we’ll 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">'pm_phi1'</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">'pm_phi2'</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">'ko'</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">'C1'</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">'$\mu_{\phi_1} [\mathrm{mas~yr}^{-1}]$'</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">'$\mu_{\phi_2} [\mathrm{mas~yr}^{-1}]$'</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>Here’s what the new version of the figure looks like. We’ve 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">plt</span><span class="o">.</span><span class="n">rcParams</span><span class="p">[</span><span class="s1">'text.usetex'</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">'default'</span><span class="p">)</span>
|
||
|
||
<span class="n">plot_proper_motion</span><span class="p">(</span><span class="n">centerline</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="cell_output docutils container">
|
||
<img alt="_images/07_plot_51_0.png" src="_images/07_plot_51_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 let’s 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="kn">import</span> <span class="nn">os</span>
|
||
|
||
<span class="n">filename</span> <span class="o">=</span> <span class="s1">'gd1_candidates.hdf5'</span>
|
||
<span class="n">path</span> <span class="o">=</span> <span class="s1">'https://github.com/AllenDowney/AstronomicalData/raw/main/data/'</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">filename</span> <span class="o">=</span> <span class="s1">'gd1_candidates.hdf5'</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">'candidate_df'</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>Here’s 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">'phi1'</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">'phi2'</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">'ko'</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">'$\phi_1$ [deg]'</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">'$\phi_2$ [deg]'</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">'Proper motion selection'</span><span class="p">,</span> <span class="n">fontsize</span><span class="o">=</span><span class="s1">'medium'</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">'equal'</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>And here’s 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_58_0.png" src="_images/07_plot_58_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, we need to reload the merged <code class="docutils literal notranslate"><span class="pre">DataFrame</span></code>, which contains data from Gaia and photometry data from Pan-STARRS.</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">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
|
||
|
||
<span class="n">filename</span> <span class="o">=</span> <span class="s1">'gd1_merged.hdf5'</span>
|
||
|
||
<span class="n">merged</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">'merged'</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>From the previous notebook, here’s the function that 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">"""Plot a color magnitude diagram.</span>
|
||
<span class="sd"> </span>
|
||
<span class="sd"> table: Table or DataFrame with photometry data</span>
|
||
<span class="sd"> """</span>
|
||
<span class="n">y</span> <span class="o">=</span> <span class="n">table</span><span class="p">[</span><span class="s1">'g_mean_psf_mag'</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">'g_mean_psf_mag'</span><span class="p">]</span> <span class="o">-</span> <span class="n">table</span><span class="p">[</span><span class="s1">'i_mean_psf_mag'</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">'ko'</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">'$g_0$'</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">'$(g-i)_0$'</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>And here’s 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">merged</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><strong>Exercise:</strong> 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>Run these cells to get the polygon coordinates we saved in the previous notebook.</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="n">filename</span> <span class="o">=</span> <span class="s1">'gd1_polygon.hdf5'</span>
|
||
<span class="n">path</span> <span class="o">=</span> <span class="s1">'https://github.com/AllenDowney/AstronomicalData/raw/main/data/'</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="n">coords_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">'coords_df'</span><span class="p">)</span>
|
||
<span class="n">coords</span> <span class="o">=</span> <span class="n">coords_df</span><span class="o">.</span><span class="n">to_numpy</span><span class="p">()</span>
|
||
<span class="n">coords</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([[ 0.26433692, 17.84253127],
|
||
[ 0.35394265, 18.799117 ],
|
||
[ 0.47491039, 19.68211921],
|
||
[ 0.63172043, 20.45474614],
|
||
[ 0.76612903, 20.78587196],
|
||
[ 0.80645161, 21.41133186],
|
||
[ 0.58691756, 21.30095659],
|
||
[ 0.39426523, 20.56512141],
|
||
[ 0.22401434, 19.2406181 ],
|
||
[ 0.19713262, 18.02649007]])
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<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(coords, closed=True, </span>
|
||
<span class="c1"># facecolor='C1', alpha=0.4)</span>
|
||
<span class="c1">#plt.gca().add_patch(poly)</span>
|
||
</pre></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 we’re ready to put it all together. To make a figure with four subplots, we’ll 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 we’re 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>Here’s 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</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">selected</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">merged</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">coords</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">'C1'</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 don’t overlap.</p>
|
||
<p><strong>Exercise:</strong> 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, we’d like to stretch the panels on the left and compress the ones on the right.</p>
|
||
<p>To do that, we’ll 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</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">selected</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">merged</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">coords</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">'C1'</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>
|
||
<p><strong>Exercise:</strong> 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>
|
||
<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 we’ve 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">Photometry</a>
|
||
|
||
</div>
|
||
<footer class="footer mt-5 mt-md-0">
|
||
<div class="container">
|
||
<p>
|
||
|
||
By Allen B. Downey<br/>
|
||
|
||
© Copyright 2020.<br/>
|
||
</p>
|
||
</div>
|
||
</footer>
|
||
</main>
|
||
|
||
|
||
</div>
|
||
</div>
|
||
|
||
|
||
<script src="_static/js/index.30270b6e4c972e43c488.js"></script>
|
||
|
||
|
||
|
||
</body>
|
||
</html> |