|
| 1 | +> ✏️ **This page is auto-generated from [`scripts/chapter_1_introduction/tutorial_0_visualization.py`](../../scripts/chapter_1_introduction/tutorial_0_visualization.py) — do not edit it directly.** |
| 2 | +> It shows the example fully executed, with its real output images. |
| 3 | +> Run it yourself via the [Python script](../../scripts/chapter_1_introduction/tutorial_0_visualization.py) or the [Jupyter notebook](../../notebooks/chapter_1_introduction/tutorial_0_visualization.ipynb). |
| 4 | +
|
| 5 | +Tutorial 0: Visualization |
| 6 | +========================= |
| 7 | + |
| 8 | +In this tutorial, we quickly cover visualization in **PyAutoGalaxy** and make sure images display clearly in your |
| 9 | +Jupyter notebook and on your computer screen. |
| 10 | + |
| 11 | +__Contents__ |
| 12 | + |
| 13 | +- **Directories:** Set the working directory so PyAutoGalaxy can find configs, data and output folders. |
| 14 | +- **Dataset:** Load an example imaging dataset of a galaxy. |
| 15 | +- **Plot Customization:** Customize matplotlib options like title, figure size and colormap. |
| 16 | +- **Subplots:** Plot all components of a dataset simultaneously using subplots. |
| 17 | +- **Visuals:** Add visual overlays like masks and grids to figures. |
| 18 | +- **Wrap Up:** Summary of visualization in PyAutoGalaxy. |
| 19 | + |
| 20 | + |
| 21 | +```python |
| 22 | + |
| 23 | +from autoconf import setup_notebook; setup_notebook() |
| 24 | +``` |
| 25 | + |
| 26 | + 2026-07-11 16:20:09,770 - matplotlib.font_manager - INFO - Failed to extract font properties from /usr/share/fonts/truetype/noto/NotoColorEmoji.ttf: Can not load face (unknown file format; error code 0x2) |
| 27 | + |
| 28 | + |
| 29 | + 2026-07-11 16:20:09,868 - matplotlib.font_manager - INFO - generated new fontManager |
| 30 | + |
| 31 | + |
| 32 | + Working Directory has been set to `HowToGalaxy` |
| 33 | + |
| 34 | + |
| 35 | +If the printed working directory does not match the workspace path on your computer, you can manually set it |
| 36 | +as follows (the example below shows the path I would use on my laptop. The code is commented out so you do not |
| 37 | +use this path in this tutorial! |
| 38 | + |
| 39 | + |
| 40 | +```python |
| 41 | +# workspace_path = "/Users/Jammy/Code/PyAuto/autogalaxy_workspace" |
| 42 | +# #%cd $workspace_path |
| 43 | +# print(f"Working Directory has been set to `{workspace_path}`") |
| 44 | +``` |
| 45 | + |
| 46 | +__Dataset__ |
| 47 | + |
| 48 | +The `dataset_path` specifies where the dataset is located, which is the |
| 49 | +directory `autogalaxy_workspace/dataset/imaging/simple__sersic`. |
| 50 | + |
| 51 | +There are many example simulated images of galaxies in this directory that will be used throughout the |
| 52 | +**HowToGalaxy** lectures. |
| 53 | + |
| 54 | + |
| 55 | +```python |
| 56 | +from pathlib import Path |
| 57 | + |
| 58 | +import autogalaxy as ag |
| 59 | +import autogalaxy.plot as aplt |
| 60 | + |
| 61 | +dataset_path = Path("dataset", "imaging", "simple__sersic") |
| 62 | +``` |
| 63 | + |
| 64 | +__Dataset Auto-Simulation__ |
| 65 | + |
| 66 | +If the dataset does not already exist on your system, it will be created by running the corresponding |
| 67 | +simulator script. This ensures that all example scripts can be run without manually simulating data first. |
| 68 | + |
| 69 | + |
| 70 | +```python |
| 71 | +if not dataset_path.exists(): |
| 72 | + import subprocess |
| 73 | + import sys |
| 74 | + |
| 75 | + subprocess.run( |
| 76 | + [sys.executable, "scripts/simulators/sersic.py"], |
| 77 | + check=True, |
| 78 | + ) |
| 79 | + |
| 80 | +``` |
| 81 | + |
| 82 | +We now load this dataset from .fits files and create an instance of an `imaging` object. |
| 83 | + |
| 84 | + |
| 85 | +```python |
| 86 | +dataset = ag.Imaging.from_fits( |
| 87 | + data_path=dataset_path / "data.fits", |
| 88 | + noise_map_path=dataset_path / "noise_map.fits", |
| 89 | + psf_path=dataset_path / "psf.fits", |
| 90 | + pixel_scales=0.1, |
| 91 | +) |
| 92 | +``` |
| 93 | + |
| 94 | +We can plot the data as follows: |
| 95 | + |
| 96 | + |
| 97 | +```python |
| 98 | +aplt.plot_array(array=dataset.data, title="Data") |
| 99 | +``` |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +__Plot Customization__ |
| 108 | + |
| 109 | +Does the figure display correctly on your computer screen? |
| 110 | + |
| 111 | +If not, you can customize common matplotlib options by passing them directly to `plot_array`: |
| 112 | + |
| 113 | + - `title=`: Set the figure title. |
| 114 | + - `figsize=`: Control the figure size as a `(width, height)` tuple. |
| 115 | + - `colormap=`: Set the matplotlib colormap name (e.g. `"jet"`, `"gray"`). |
| 116 | + - `xlabel=`, `ylabel=`: Override the default axis labels. |
| 117 | + |
| 118 | + |
| 119 | +```python |
| 120 | +aplt.plot_array( |
| 121 | + array=dataset.data, |
| 122 | + title="Data", |
| 123 | +) |
| 124 | +``` |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | +Many matplotlib options can be customized, but for now we're only concerned with making sure figures display clear in |
| 133 | +your Jupyter Notebooks. Nevertheless, a comprehensive API reference guide of all available plot arguments can |
| 134 | +be found in the `autogalaxy_workspace/*/guides/plot` package. You should check this out once you are more familiar with |
| 135 | +**PyAutoGalaxy**. |
| 136 | + |
| 137 | +Ideally, we would not specify a `figsize` every time we plot an image. Fortunately, default values can be fully |
| 138 | +customized via the config files. |
| 139 | + |
| 140 | +Checkout the `mat_wrap.yaml` file in `autogalaxy_workspace/config/visualize/mat_wrap`. |
| 141 | + |
| 142 | +All default matplotlib values are here. There are a lot of entries, so lets focus on whats important for displaying |
| 143 | +figures: |
| 144 | + |
| 145 | + - mat_wrap.yaml -> Figure -> figure: -> figsize |
| 146 | + - mat_wrap.yaml -> YLabel -> figure: -> fontsize |
| 147 | + - mat_wrap.yaml -> XLabel -> figure: -> fontsize |
| 148 | + - mat_wrap.yaml -> TickParams -> figure: -> labelsize |
| 149 | + - mat_wrap.yaml -> YTicks -> figure: -> labelsize |
| 150 | + - mat_wrap.yaml -> XTicks -> figure: -> labelsize |
| 151 | + |
| 152 | +Don't worry about all the other files or options listed for now, as they'll make a lot more sense once you are familiar |
| 153 | +with **PyAutoGalaxy**. |
| 154 | + |
| 155 | +If you had to change any of the above settings to get the figures to display clearly, you should update their values |
| 156 | +in the corresponding config files above (you will need to reset your Jupyter notebook server for these changes to |
| 157 | +take effect, so make sure you have the right values using the `figsize` argument in the cell above beforehand!). |
| 158 | + |
| 159 | +__Subplots__ |
| 160 | + |
| 161 | +In addition to plotting individual figures, **PyAutoGalaxy** can also plot subplots showing all components of a |
| 162 | +dataset simultaneously. |
| 163 | + |
| 164 | +Lets plot a subplot of our `Imaging` data: |
| 165 | + |
| 166 | + |
| 167 | +```python |
| 168 | +aplt.subplot_imaging_dataset(dataset=dataset) |
| 169 | +``` |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | + |
| 175 | + |
| 176 | + |
| 177 | +__Visuals__ |
| 178 | + |
| 179 | +Visuals can be added to any figure by passing them as keyword arguments directly to `plot_array`. |
| 180 | + |
| 181 | +For example, we can plot a mask on the image above by passing `mask=mask`. |
| 182 | + |
| 183 | +The `visuals` example illustrates every overlay argument, for example `mask=`, `grid=`, `positions=`, `lines=`, etc. |
| 184 | + |
| 185 | + |
| 186 | +```python |
| 187 | +mask = ag.Mask2D.circular_annular( |
| 188 | + shape_native=dataset.shape_native, |
| 189 | + pixel_scales=dataset.pixel_scales, |
| 190 | + inner_radius=0.3, |
| 191 | + outer_radius=3.0, |
| 192 | +) |
| 193 | + |
| 194 | +aplt.plot_array(array=dataset.data, title="Data") |
| 195 | +``` |
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | + |
| 201 | + |
| 202 | + |
| 203 | +__Wrap Up__ |
| 204 | + |
| 205 | +Throughout lectures you'll see lots more visuals that are plotted on figures and subplots. |
| 206 | + |
| 207 | +Great! Hopefully, visualization in **PyAutoGalaxy** is displaying nicely for us to get on with the **HowToGalaxy** |
| 208 | +lecture series. |
| 209 | + |
| 210 | + |
| 211 | +```python |
| 212 | + |
| 213 | +``` |
0 commit comments