Plotting a difference image

How to compute and plot a difference image. The example uses sunpy.map.MapSequence to compute a difference image and then plot it. This basic method works for base difference or running difference. Just change whether you’re subtracting the previous image or the first image in a sequence.

import matplotlib.pyplot as plt
import matplotlib.colors as colors
import astropy.units as u
from sunpy.net import Fido, attrs as a
import sunpy.map

First we’ll download a couple images and store them in a sunpy.map.MapSequence. This could be from any channel of any imager. Here, we use SDO/AIA 304 Å.

instrument = a.Instrument('AIA')
wave = a.Wavelength(30 * u.nm, 31 * u.nm)
result = Fido.search(a.Time('2015-06-18T00:00:00', '2015-06-18T00:00:10') |
                     a.Time('2015-06-18T01:03:30', '2015-06-18T01:03:35'),
                     instrument,
                     wave)
downloaded_files = Fido.fetch(result)
maps = sunpy.map.Map(downloaded_files, sequence=True)

Out:

Files Downloaded:   0%|          | 0/2 [00:00<?, ?file/s]


aia_lev1_304a_2015_06_18t01_03_31_13z_image_lev1.fits:   0%|          | 0.00/7.95M [00:00<?, ?B/s]

aia_lev1_304a_2015_06_18t00_00_07_12z_image_lev1.fits:   0%|          | 0.00/7.92M [00:00<?, ?B/s]


aia_lev1_304a_2015_06_18t01_03_31_13z_image_lev1.fits:   1%|          | 52.8k/7.95M [00:00<00:20, 381kB/s]

aia_lev1_304a_2015_06_18t00_00_07_12z_image_lev1.fits:   1%|          | 49.9k/7.92M [00:00<00:22, 354kB/s]


aia_lev1_304a_2015_06_18t01_03_31_13z_image_lev1.fits:   2%|1         | 130k/7.95M [00:00<00:18, 419kB/s] 

aia_lev1_304a_2015_06_18t00_00_07_12z_image_lev1.fits:   2%|1         | 127k/7.92M [00:00<00:19, 396kB/s] 


aia_lev1_304a_2015_06_18t01_03_31_13z_image_lev1.fits:   3%|2         | 227k/7.95M [00:00<00:16, 475kB/s]

aia_lev1_304a_2015_06_18t00_00_07_12z_image_lev1.fits:   3%|3         | 239k/7.92M [00:00<00:16, 466kB/s]


aia_lev1_304a_2015_06_18t01_03_31_13z_image_lev1.fits:   4%|4         | 356k/7.95M [00:00<00:13, 555kB/s]

aia_lev1_304a_2015_06_18t00_00_07_12z_image_lev1.fits:   5%|4         | 382k/7.92M [00:00<00:13, 556kB/s]


aia_lev1_304a_2015_06_18t01_03_31_13z_image_lev1.fits:   6%|6         | 502k/7.95M [00:00<00:11, 644kB/s]

aia_lev1_304a_2015_06_18t00_00_07_12z_image_lev1.fits:   7%|6         | 550k/7.92M [00:00<00:11, 662kB/s]


aia_lev1_304a_2015_06_18t01_03_31_13z_image_lev1.fits:   8%|8         | 665k/7.95M [00:00<00:09, 742kB/s]

aia_lev1_304a_2015_06_18t00_00_07_12z_image_lev1.fits:   9%|9         | 739k/7.92M [00:00<00:09, 780kB/s]

aia_lev1_304a_2015_06_18t00_00_07_12z_image_lev1.fits:  12%|#2        | 951k/7.92M [00:00<00:07, 963kB/s]


aia_lev1_304a_2015_06_18t01_03_31_13z_image_lev1.fits:  11%|#         | 854k/7.95M [00:00<00:08, 855kB/s]

aia_lev1_304a_2015_06_18t00_00_07_12z_image_lev1.fits:  14%|#4        | 1.11M/7.92M [00:01<00:06, 1.01MB/s]


aia_lev1_304a_2015_06_18t01_03_31_13z_image_lev1.fits:  13%|#3        | 1.07M/7.95M [00:01<00:06, 983kB/s]

aia_lev1_304a_2015_06_18t00_00_07_12z_image_lev1.fits:  17%|#7        | 1.37M/7.92M [00:01<00:05, 1.17MB/s]


aia_lev1_304a_2015_06_18t01_03_31_13z_image_lev1.fits:  16%|#6        | 1.31M/7.95M [00:01<00:05, 1.12MB/s]

aia_lev1_304a_2015_06_18t00_00_07_12z_image_lev1.fits:  21%|##1       | 1.67M/7.92M [00:01<00:04, 1.35MB/s]


aia_lev1_304a_2015_06_18t01_03_31_13z_image_lev1.fits:  20%|#9        | 1.59M/7.95M [00:01<00:04, 1.29MB/s]

aia_lev1_304a_2015_06_18t00_00_07_12z_image_lev1.fits:  25%|##5       | 2.01M/7.92M [00:01<00:03, 1.55MB/s]


aia_lev1_304a_2015_06_18t01_03_31_13z_image_lev1.fits:  24%|##3       | 1.90M/7.95M [00:01<00:04, 1.48MB/s]

aia_lev1_304a_2015_06_18t00_00_07_12z_image_lev1.fits:  30%|###       | 2.40M/7.92M [00:01<00:02, 1.90MB/s]


aia_lev1_304a_2015_06_18t01_03_31_13z_image_lev1.fits:  29%|##8       | 2.27M/7.95M [00:01<00:03, 1.69MB/s]

aia_lev1_304a_2015_06_18t00_00_07_12z_image_lev1.fits:  34%|###4      | 2.72M/7.92M [00:01<00:02, 2.02MB/s]


aia_lev1_304a_2015_06_18t01_03_31_13z_image_lev1.fits:  34%|###3      | 2.70M/7.95M [00:01<00:02, 1.95MB/s]

aia_lev1_304a_2015_06_18t00_00_07_12z_image_lev1.fits:  41%|####      | 3.22M/7.92M [00:01<00:01, 2.45MB/s]


aia_lev1_304a_2015_06_18t01_03_31_13z_image_lev1.fits:  39%|###9      | 3.13M/7.95M [00:01<00:02, 2.34MB/s]

aia_lev1_304a_2015_06_18t00_00_07_12z_image_lev1.fits:  46%|####5     | 3.64M/7.92M [00:01<00:01, 2.62MB/s]


aia_lev1_304a_2015_06_18t01_03_31_13z_image_lev1.fits:  44%|####4     | 3.54M/7.95M [00:02<00:01, 2.49MB/s]

aia_lev1_304a_2015_06_18t00_00_07_12z_image_lev1.fits:  53%|#####3    | 4.22M/7.92M [00:02<00:01, 3.14MB/s]


aia_lev1_304a_2015_06_18t01_03_31_13z_image_lev1.fits:  51%|#####1    | 4.08M/7.95M [00:02<00:01, 2.98MB/s]

aia_lev1_304a_2015_06_18t00_00_07_12z_image_lev1.fits:  60%|######    | 4.78M/7.92M [00:02<00:00, 3.36MB/s]


aia_lev1_304a_2015_06_18t01_03_31_13z_image_lev1.fits:  58%|#####7    | 4.57M/7.95M [00:02<00:01, 3.12MB/s]

aia_lev1_304a_2015_06_18t00_00_07_12z_image_lev1.fits:  69%|######8   | 5.45M/7.92M [00:02<00:00, 3.96MB/s]


aia_lev1_304a_2015_06_18t01_03_31_13z_image_lev1.fits:  66%|######5   | 5.21M/7.95M [00:02<00:00, 3.69MB/s]

aia_lev1_304a_2015_06_18t00_00_07_12z_image_lev1.fits:  77%|#######7  | 6.12M/7.92M [00:02<00:00, 4.51MB/s]


aia_lev1_304a_2015_06_18t01_03_31_13z_image_lev1.fits:  73%|#######2  | 5.80M/7.95M [00:02<00:00, 3.87MB/s]

aia_lev1_304a_2015_06_18t00_00_07_12z_image_lev1.fits:  86%|########6 | 6.83M/7.92M [00:02<00:00, 5.06MB/s]


aia_lev1_304a_2015_06_18t01_03_31_13z_image_lev1.fits:  81%|########  | 6.41M/7.95M [00:02<00:00, 4.33MB/s]

aia_lev1_304a_2015_06_18t00_00_07_12z_image_lev1.fits:  95%|#########5| 7.55M/7.92M [00:02<00:00, 5.26MB/s]

                                                                                                           
Files Downloaded:  50%|#####     | 1/2 [00:03<00:03,  3.71s/file]


aia_lev1_304a_2015_06_18t01_03_31_13z_image_lev1.fits:  90%|########9 | 7.13M/7.95M [00:02<00:00, 4.91MB/s]


aia_lev1_304a_2015_06_18t01_03_31_13z_image_lev1.fits:  99%|#########9| 7.87M/7.95M [00:02<00:00, 5.47MB/s]


                                                                                                           
Files Downloaded: 100%|##########| 2/2 [00:03<00:00,  2.65s/file]
Files Downloaded: 100%|##########| 2/2 [00:03<00:00,  1.95s/file]
/home/docs/checkouts/readthedocs.org/user_builds/sunpy/conda/stable/lib/python3.7/asyncio/base_events.py:622: ResourceWarning: unclosed event loop <_UnixSelectorEventLoop running=False closed=False debug=False>
  source=self)

Now we’ll do a standard plot of the second image just to see it.

plt.figure()
ax = plt.subplot(projection=maps[1])
maps[1].plot()
../../../_images/sphx_glr_difference_images_001.png

Out:

<matplotlib.image.AxesImage object at 0x7f14081cb3d0>

And now we can do take the actual difference.

diff = maps[1].data - maps[0].data

But we have to decide what to do with the metadata. For example, what time does this difference image correspond to? The time of the first or second image? The mean time? You’ll have to decide what makes most sense for your application. Here we’ll just use the metadata from the second image. Then we can store the difference and header back in a Map.

meta = maps[1].meta
diff_map = sunpy.map.Map(diff, meta)

Finally, we’ll plot it. We’ll apply a colormap and renormalize the intensity so that it shows up well.

plt.figure()
ax_diff = plt.subplot(projection=diff_map)
diff_map.plot(cmap='Greys_r',
              norm=colors.Normalize(vmin=-50, vmax=50))
plt.show()

#sphinx_gallery_thumbnail_number = 2
../../../_images/sphx_glr_difference_images_002.png

Total running time of the script: ( 0 minutes 22.379 seconds)

Gallery generated by Sphinx-Gallery