Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong frame for wide fov plots #107

Closed
ManonMarchand opened this issue Aug 29, 2023 · 1 comment
Closed

Wrong frame for wide fov plots #107

ManonMarchand opened this issue Aug 29, 2023 · 1 comment

Comments

@ManonMarchand
Copy link
Member

In all-sky plots, we draw a rectangular frame instead of an elliptical one, see MNWE:

import matplotlib.pyplot as plt
import astropy.units as u
from astropy.coordinates import SkyCoord, Angle
from mocpy import MOC, WCS

moc = MOC.from_string('2/24')

fig = plt.figure()

with WCS(
    fig,
    fov=270 * u.deg,
    center=SkyCoord(0, 0, unit="deg", frame="galactic"),
    coordsys="galactic",
    rotation=Angle(0, u.degree),
    projection="AIT",
) as wcs:
    ax = fig.add_subplot(1, 1, 1, projection=wcs)


    moc.fill(
        ax=ax,
        wcs=wcs,
        edgecolor="teal",
        facecolor="orange",
        linewidth=1.0,
        fill=True,
        alpha=0.8,
    )

ax.grid(True)

plt.tight_layout()

image

However this is not as trivial as enforcing Elliptical Frame, see:

from astropy.visualization.wcsaxes.frame import EllipticalFrame
import matplotlib.pyplot as plt
import astropy.units as u
from astropy.coordinates import SkyCoord, Angle
from mocpy import MOC, WCS

moc = MOC.from_string('2/24')

fig = plt.figure()

with WCS(
    fig,
    fov=270 * u.deg,
    center=SkyCoord(0, 0, unit="deg", frame="galactic"),
    coordsys="galactic",
    rotation=Angle(0, u.degree),
    projection="AIT",
) as wcs:
    ax = fig.add_subplot(1, 1, 1, projection=wcs, frame_class=EllipticalFrame)


    moc.fill(
        ax=ax,
        wcs=wcs,
        edgecolor="teal",
        facecolor="orange",
        linewidth=1.0,
        fill=True,
        alpha=0.8,
    )

ax.grid(True)

plt.tight_layout()

image

There is something to investigate, don't know if the bug is upstream in astropy or with us.

@ManonMarchand
Copy link
Member Author

See EllipticalFrame implementation here

class EllipticalFrame(BaseFrame):
    """
    An elliptical frame.
    """

    spine_names = "chv"

    def update_spines(self):
        xmin, xmax = self.parent_axes.get_xlim()
        ymin, ymax = self.parent_axes.get_ylim()

        xmid = 0.5 * (xmax + xmin)
        ymid = 0.5 * (ymax + ymin)

        dx = xmid - xmin
        dy = ymid - ymin

        theta = np.linspace(0.0, 2 * np.pi, 1000)
        self["c"].data = np.array(
            [xmid + dx * np.cos(theta), ymid + dy * np.sin(theta)]
        ).transpose()
        self["h"].data = np.array(
            [np.linspace(xmin, xmax, 1000), np.repeat(ymid, 1000)]
        ).transpose()
        self["v"].data = np.array(
            [np.repeat(xmid, 1000), np.linspace(ymin, ymax, 1000)]
        ).transpose()

        super().update_spines()

    def _update_patch_path(self):
        """Override path patch to include only the outer ellipse,
        not the major and minor axes in the middle.
        """
        self.update_spines()
        vertices = self["c"].data

        if self._path is None:
            self._path = Path(vertices)
        else:
            self._path.vertices = vertices

    def draw(self, renderer):
        """Override to draw only the outer ellipse,
        not the major and minor axes in the middle.

        FIXME: we may want to add a general method to give the user control
        over which spines are drawn.
        """
        axis = "c"
        pixel = self[axis]._get_pixel()
        line = Line2D(
            pixel[:, 0],
            pixel[:, 1],
            linewidth=self._linewidth,
            color=self._color,
            zorder=1000,
        )
        line.draw(renderer)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant