File: plot_rectangle.py

package info (click to toggle)
sunpy 4.1.2-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 11,972 kB
  • sloc: python: 39,301; ansic: 1,780; makefile: 35
file content (105 lines) | stat: -rw-r--r-- 3,076 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""
============================
Drawing a rectangle on a map
============================

This example will demonstrate how to draw a rectangle on a map using :meth:`~sunpy.map.GenericMap.draw_quadrangle`.
"""
import matplotlib.pyplot as plt

import astropy.units as u
from astropy.coordinates import SkyCoord

import sunpy.data.sample
import sunpy.map

################################################################################
# Let's start with a sample AIA image.

aia_map = sunpy.map.Map(sunpy.data.sample.AIA_171_IMAGE)

################################################################################
# Here are four different ways to draw a rectangle. The first three ways
# directly calls the `~astropy.coordinates.SkyCoord` class. The fourth way
# converts pixel coordinates to the equivalent `~astropy.coordinates.SkyCoord`
# objects using the :meth:`~sunpy.map.GenericMap.pixel_to_world`.

# sphinx_gallery_defer_figures

fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(projection=aia_map)
aia_map.plot(axes=ax, clip_interval=(1, 99.99)*u.percent)

################################################################################
# Specify two opposite corners of the rectangle as a single, two-element
# SkyCoord object.

# sphinx_gallery_defer_figures

coords = SkyCoord(
    Tx=(100, 500) * u.arcsec,
    Ty=(200, 500) * u.arcsec,
    frame=aia_map.coordinate_frame,
)
aia_map.draw_quadrangle(
    coords,
    axes=ax,
    edgecolor="blue",
    linestyle="-",
    linewidth=2,
    label='2-element SkyCoord'
)

################################################################################
# Specify two opposite corners of the rectangle as separate SkyCoord objects.

# sphinx_gallery_defer_figures

bottom_left = SkyCoord(-500 * u.arcsec, 200 * u.arcsec, frame=aia_map.coordinate_frame)
top_right = SkyCoord(-100 * u.arcsec, 500 * u.arcsec, frame=aia_map.coordinate_frame)
aia_map.draw_quadrangle(
    bottom_left,
    axes=ax,
    top_right=top_right,
    edgecolor="green",
    linestyle="--",
    linewidth=2,
    label='two SkyCoords'
)

################################################################################
# Specify one corner of the rectangle and the rectangle's width and height.

# sphinx_gallery_defer_figures

bottom_left = SkyCoord(-500 * u.arcsec, -500 * u.arcsec, frame=aia_map.coordinate_frame)
width = 400 * u.arcsec
height = 300 * u.arcsec
aia_map.draw_quadrangle(
    bottom_left,
    axes=ax,
    width=width,
    height=height,
    edgecolor="yellow",
    linestyle="-.",
    linewidth=2,
    label='width/height'
)

################################################################################
# Draw a desired rectangle in pixel coordinates by first converting to SkyCoord objects.

bottom_left = aia_map.pixel_to_world(600 * u.pixel, 350 * u.pixel)
top_right = aia_map.pixel_to_world(800 * u.pixel, 450 * u.pixel)
aia_map.draw_quadrangle(
    bottom_left,
    axes=ax,
    top_right=top_right,
    edgecolor="red",
    linestyle=":",
    linewidth=2,
    label='pixel_to_world()'
)
ax.legend()

plt.show()