# Plotting on BornAgain

file:///usr/share/doc/bornagain/html/ref/plot/index.html Recent content in Plotting on BornAgain Hugo en-us

# Functions

file:///usr/share/doc/bornagain/html/ref/plot/functions.html Mon, 01 Jan 0001 00:00:00 +0000 file:///usr/share/doc/bornagain/html/ref/plot/functions.html <h1 id="plot-functions">Plot functions</h1> <p>The following plot functions are provided by the module <code>ba_plot</code> in the <code>bornagain</code> Python package.</p> <h3 id="plot-single-datafield">Plot single Datafield</h3> <p>A single Datafield can be plotted with</p> <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="kn">from</span> <span class="nn">bornagain</span> <span class="kn">import</span> <span class="n">ba_plot</span> <span class="k">as</span> <span class="n">bp</span> </span></span><span class="line"><span class="cl"><span class="n">simulation</span> <span class="o">=</span> <span class="o">...</span> </span></span><span class="line"><span class="cl"><span class="n">result</span> <span class="o">=</span> <span class="n">simulation</span><span class="o">.</span><span class="n">simulate</span><span class="p">()</span> </span></span><span class="line"><span class="cl"><span class="n">bp</span><span class="o">.</span><span class="n">plot_simulation_result</span><span class="p">(</span><span class="n">result</span><span class="p">)</span> </span></span><span class="line"><span class="cl"><span class="n">bp</span><span class="o">.</span><span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span> </span></span></code></pre></div><p>This function is used in a majority of our scripting examples, for instance in the following short, basic examples:</p> <ul> <li><a href="file:///usr/share/doc/bornagain/html/ref/sim/class/specular/ex">specular reflectivity</a></li> <li><a href="file:///usr/share/doc/bornagain/html/ref/sim/class/scattering/ex/gisas">GISAS</a></li> </ul> <h3 id="plot-several-curves">Plot several curves</h3> <p>Specular simulations yield one-dimensional Datafields that are plotted as curves y(x). Several such curves can be plotted in one frame. This is often used to demonstrate the effect of varying one parameter. See e.g. the examples</p>

# Configuration

file:///usr/share/doc/bornagain/html/ref/plot/config.html Mon, 01 Jan 0001 00:00:00 +0000 file:///usr/share/doc/bornagain/html/ref/plot/config.html <h1 id="plot-configuration">Plot configuration</h1> <p>Images in BornAgain examples are generated using the Python library <a href="https://matplotlib.org">MatPlotLib</a>.</p> <p>Default settings can be overridden by function arguments or MatPlotLib ressources.</p> <p>Exceptionally, <a href="heatmap">heat map</a> supports an environment variable.</p> <h3 id="parameterization-with-plotargs">Parameterization with plotargs</h3> <p>In the functions</p> <pre tabindex="0"><code>plot_simulation_result(result, **plotargs) plot_multicurve(results, **plotargs) make_plot(results, ncol, **plotargs) make_plot_row(results, **plotargs) </code></pre><p>the placeholder <code>**plotargs</code> stands for optional keyword arguments. These can be supplied in three ways:</p> <p>(1) Directly like in</p> <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"> <span class="n">bp</span><span class="o">.</span><span class="n">plot_simulation_result</span><span class="p">(</span><span class="n">result</span><span class="p">,</span> <span class="n">cmap</span><span class="o">=</span><span class="s1">&#39;viridis&#39;</span><span class="p">,</span> <span class="n">legendloc</span><span class="o">=</span><span class="s1">&#39;lower right&#39;</span><span class="p">)</span> </span></span><span class="line"><span class="cl"> <span class="n">bp</span><span class="o">.</span><span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span> </span></span></code></pre></div><p>(2) Through a dictionary variable like in</p>

# Heat map

file:///usr/share/doc/bornagain/html/ref/plot/heatmap.html Mon, 01 Jan 0001 00:00:00 +0000 file:///usr/share/doc/bornagain/html/ref/plot/heatmap.html <h1 id="heat-map">Heat map</h1> <p>MatPlotLib supports various color schemes for plotting color-coded pixel values in 2d, see the <a href="https://matplotlib.org/stable/tutorials/colors/colormaps.html">MatPlotLib color schemes tutorial</a>.</p> <p>It is generally recommended to use one of the five &ldquo;perceptually uniform sequential&rdquo; color schemes: <img src="file:///usr/share/doc/bornagain/html/img/matplotlib_pus_colormaps.png" alt="MatPlotLib perceptually uniform sequential color schemes" title="Perceptually uniform sequential color schemes from MatPlotLib."></p> <p>When plotting through <code>ba_plot</code>, the color scheme is determined in this order:</p> <ul> <li>from the optional <code>plotargs</code> arguments if there is one with keyword <code>cmap</code>, like <code>cmap='jet'</code>;</li> <li>otherwise from the environment variable <code>CMAP</code>, if defined;</li> <li>otherwise from the hard-coded default &ldquo;inferno&rdquo;.</li> </ul> <p>Note that the variable <code>image.cmap</code> from <a href="https://matplotlib.org/stable/users/explain/customizing.html">matplotlib.pyplot.rcParams</a> is ignored. This is a concious choice, intended to facilitate configuration for BornAgain users who are no MatPlotLib experts.</p>

# LaTeX markup

file:///usr/share/doc/bornagain/html/ref/plot/latex.html Mon, 01 Jan 0001 00:00:00 +0000 file:///usr/share/doc/bornagain/html/ref/plot/latex.html <h1 id="latex-markup">LaTeX markup</h1> <p>Text enclosed between dollar signs <code>$...$</code> is interpreted as LaTeX markup. It allows us to plot greek letters (<code>$\alpha$</code>), superscripts (<code>$F^2$</code>), subscripts (<code>$\phi_{\rm i}</code>$), and special symbols (<code>$90^\circ$</code>).</p> <p>By default, it is processed by MatPlotLib&rsquo;s own LaTeX processor. However, if <em>usetex mode</em> is activated, then the original LaTeX text processor is used, and labels are typeset in LaTeX&rsquo;s computer modern fonts. For <em>usetex mode</em> to work, it may be necessary to install extra software. Under Debian, the required packages are <code>texlive-latex-extra</code> (and dependencies), <code>dvipng</code> and <code>cm-super-minimal</code>. We have no clue how to support <em>usetex mode</em> under Windows.</p>

# Export

file:///usr/share/doc/bornagain/html/ref/plot/export.html Mon, 01 Jan 0001 00:00:00 +0000 file:///usr/share/doc/bornagain/html/ref/plot/export.html <h1 id="export-plot-to-file">Export plot to file</h1> <h3 id="interactively">Interactively</h3> <p>To create image files interactively, run a script that contains the usual</p> <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">bp</span><span class="o">.</span><span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span> </span></span></code></pre></div><p>then click the &ldquo;diskette&rdquo; icon in the MatPlotLib window.</p> <h3 id="programmatically">Programmatically</h3> <p>To create image files programmatically, replace the Python statement</p> <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">bp</span><span class="o">.</span><span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span> </span></span></code></pre></div><p>by one of</p> <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">bp</span><span class="o">.</span><span class="n">export</span><span class="p">(</span><span class="o">**</span><span class="n">plotargs</span><span class="p">)</span> </span></span></code></pre></div><p>and use one of the three parameterization mechanisms described in the <a href="config">Configuration</a> page to pass a keyword argument <code>figfile=&lt;filename&gt;</code>. The image format is deduced from the extension of the filename.</p>

# Plot axes in different units

file:///usr/share/doc/bornagain/html/ref/plot/axes-in-different-units.html Mon, 01 Jan 0001 00:00:00 +0000 file:///usr/share/doc/bornagain/html/ref/plot/axes-in-different-units.html <h3 id="plotting-with-axes-in-different-units">Plotting with axes in different units</h3> <p><em>The functionality demonstrated in this example is currently broken.</em></p> <p>Repair is on our agenda: <a href="https://jugit.fz-juelich.de/mlz/bornagain/-/issues/716">https://jugit.fz-juelich.de/mlz/bornagain/-/issues/716</a>.</p> <p>&hellip;.</p> <p>In this example we demonstrate how modify coordinate axes in 2D plots.</p> <p>The example is based on the standard GISAS simulation <a href="file:///usr/share/doc/bornagain/html/ref/sim/class/scattering/ex/gisas">Cylinders in DWBA</a>.</p> <p>The simulated Datafield are transformed using the function calls</p> <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">result</span> <span class="o">=</span> <span class="n">simulation</span><span class="o">.</span><span class="n">simulate</span><span class="p">()</span> </span></span><span class="line"><span class="cl"><span class="n">result2</span> <span class="o">=</span> <span class="n">detector</span><span class="o">.</span><span class="n">field2bins</span><span class="p">(</span><span class="n">result</span><span class="p">)</span> <span class="c1"># coordinates are u, v as dimensionless bin numbers</span> </span></span><span class="line"><span class="cl"><span class="n">result3</span> <span class="o">=</span> <span class="n">detector</span><span class="o">.</span><span class="n">field2angles</span><span class="p">(</span><span class="n">result</span><span class="p">)</span> <span class="c1"># coordinates are phi_f, alpha_f in degrees</span> </span></span><span class="line"><span class="cl"><span class="n">result4</span> <span class="o">=</span> <span class="n">detector</span><span class="o">.</span><span class="n">field2q</span><span class="p">(</span><span class="n">result</span><span class="p">)</span> <span class="c1"># coordinates are q_y, q_z in 1/nm</span> </span></span></code></pre></div><p>The transforming functions are members of class FlatDetector because they require knowledge of detector geometry and incident wave vector.</p>