From 760a7d3864c1d2b076fbe3719ae5c23a06353b2d Mon Sep 17 00:00:00 2001 From: jrob93 Date: Tue, 7 May 2024 14:10:48 +0100 Subject: [PATCH 1/2] phase curve plot func --- notebooks/plotting_utilities.ipynb | 235 ++++++++++++++++++++++ src/adler/utilities/plotting_utilities.py | 75 +++++++ 2 files changed, 310 insertions(+) create mode 100644 notebooks/plotting_utilities.ipynb create mode 100644 src/adler/utilities/plotting_utilities.py diff --git a/notebooks/plotting_utilities.ipynb b/notebooks/plotting_utilities.ipynb new file mode 100644 index 0000000..e96b70b --- /dev/null +++ b/notebooks/plotting_utilities.ipynb @@ -0,0 +1,235 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "10cfab6a", + "metadata": {}, + "outputs": [], + "source": [ + "%matplotlib notebook" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9d3f3aa6", + "metadata": {}, + "outputs": [], + "source": [ + "%matplotlib inline" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9f5f4b7d", + "metadata": {}, + "outputs": [], + "source": [ + "from adler.dataclasses.AdlerPlanetoid import AdlerPlanetoid\n", + "from adler.science.PhaseCurve import PhaseCurve\n", + "from adler.utilities.plotting_utilities import plot_phasecurve\n", + "\n", + "import numpy as np\n", + "import matplotlib.pyplot as plt\n", + "import matplotlib.gridspec as gridspec\n", + "import astropy.units as u" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "28113970", + "metadata": {}, + "outputs": [], + "source": [ + "ssoid = \"8268570668335894776\"\n", + "fname = \"../tests/data/testing_database.db\"\n", + "planetoid = AdlerPlanetoid.construct_from_SQL(ssoid, sql_filename=fname)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f27e1dec", + "metadata": {}, + "outputs": [], + "source": [ + "fig = plot_phasecurve(planetoid, filt_list=[\"r\"])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "72ec3def", + "metadata": {}, + "outputs": [], + "source": [ + "# access the axes object to update attributes\n", + "ax1 = fig.axes[0]\n", + "ax1.set_xlabel(\"phaseAngle (degrees)\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3b751ba2", + "metadata": {}, + "outputs": [], + "source": [ + "# replot the figure\n", + "fig" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2ee0ae9e", + "metadata": {}, + "outputs": [], + "source": [ + "# define a phase curve model\n", + "\n", + "filt = \"r\"\n", + "sso = planetoid.SSObject_in_filter(filt)\n", + "obs = planetoid.observations_in_filter(filt)\n", + "\n", + "H = sso.H\n", + "G12 = sso.G12\n", + "\n", + "pc = PhaseCurve(abs_mag=H * u.mag, phase_param=G12, model_name=\"HG12_Pen16\")\n", + "alpha = np.linspace(0, np.amax(obs.phaseAngle)) * u.deg\n", + "red_mag = pc.ReducedMag(alpha)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c8c4608d", + "metadata": {}, + "outputs": [], + "source": [ + "# add this phase curve to the figure\n", + "ax1.plot(alpha.value, pc.ReducedMag(alpha).value, label=\"{} {}\".format(filt, pc.model_name))\n", + "ax1.legend()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ab0df0b1", + "metadata": {}, + "outputs": [], + "source": [ + "fig" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6cae1b45", + "metadata": {}, + "outputs": [], + "source": [ + "# we can also pass the fig object to the plotting function again to add more data\n", + "fig2 = plot_phasecurve(planetoid, fig=fig, filt_list=[\"u\", \"g\"], label_list=[\"u\", \"g\"])\n", + "\n", + "# update the legend\n", + "ax1 = fig2.axes[0]\n", + "ax1.legend()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3d92c46d", + "metadata": {}, + "outputs": [], + "source": [ + "fig2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "be921f85", + "metadata": {}, + "outputs": [], + "source": [ + "ax1.__dict__" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f356cbf7", + "metadata": {}, + "outputs": [], + "source": [ + "ax1._children" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4bf0f661", + "metadata": {}, + "outputs": [], + "source": [ + "ax1.containers" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c847b938", + "metadata": {}, + "outputs": [], + "source": [ + "# we can access axes properties and update them after the fact\n", + "ax1.containers[0]._label = \"r\"\n", + "ax1.legend()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "07982dd4", + "metadata": {}, + "outputs": [], + "source": [ + "fig2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a56d5711", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "adler-dev", + "language": "python", + "name": "adler-dev" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/src/adler/utilities/plotting_utilities.py b/src/adler/utilities/plotting_utilities.py new file mode 100644 index 0000000..f9b1d29 --- /dev/null +++ b/src/adler/utilities/plotting_utilities.py @@ -0,0 +1,75 @@ +import matplotlib.pyplot as plt +import matplotlib.gridspec as gridspec + + +def plot_phasecurve( + planetoid, + filt_list=["r"], + x_plot="phaseAngle", + y_plot="reduced_mag", + xerr_plot="magErr", + fig=None, + label_list=None, + col_list=None, +): + """Make an errorbar scatter plot of reduced magnitude against phase angle to show the phase curve of an Adler object. + + planetoid: AdlerPlanetoid + AdlerPlanetoid object containing the observational data to be plotted + filt_list: list + List of filters to be plotted + x_plot: str + Name of the AdlerPlanetoid attribute to be plotted on the x axis + y_plot: str + Name of the AdlerPlanetoid attribute to be plotted on the y axis + xerr_plot: str + Name of the AdlerPlanetoid attribute for the x axis uncertainties + fig: matplotlib.figure.Figure + Optional, pass an existing figure object to be added to + label_list: list + Optional, labels for errorbar plot elements + col_list: list + Optional, colors for errorbar scatter points + + Returns + ----------- + fig: matplotlib.figure.Figure + The figure object which can be manipulated further if required + + """ + + if fig: + # use the figure object that was passed + ax1 = fig.axes[0] + else: + # set up a new figure object + fig = plt.figure() + gs = gridspec.GridSpec(1, 1) + ax1 = plt.subplot(gs[0, 0]) + ax1.invert_yaxis() + ax1.set_xlabel(x_plot) + ax1.set_ylabel(y_plot) + + for i, filt in enumerate(filt_list): + # get the object data + obs_filt = planetoid.observations_in_filter(filt) + x = getattr(obs_filt, x_plot) + y = getattr(obs_filt, y_plot) + xerr = getattr(obs_filt, xerr_plot) + + # label the errorbars? + if label_list is not None: + l = label_list[i] + else: + l = None + + # select colours? + if col_list is not None: + c = col_list[i] + else: + c = None + + # plot the errorbars + ax1.errorbar(x, y, xerr, color=c, fmt="o", label=l) + + return fig From 3e098eedc60b49d8fadcadec5cd938ebc229601b Mon Sep 17 00:00:00 2001 From: jrob93 Date: Mon, 13 May 2024 22:28:21 +0100 Subject: [PATCH 2/2] update func name and add savefig --- notebooks/plotting_utilities.ipynb | 45 ++++++++++++++++++++--- src/adler/utilities/plotting_utilities.py | 11 +++++- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/notebooks/plotting_utilities.ipynb b/notebooks/plotting_utilities.ipynb index e96b70b..3b1071c 100644 --- a/notebooks/plotting_utilities.ipynb +++ b/notebooks/plotting_utilities.ipynb @@ -29,7 +29,7 @@ "source": [ "from adler.dataclasses.AdlerPlanetoid import AdlerPlanetoid\n", "from adler.science.PhaseCurve import PhaseCurve\n", - "from adler.utilities.plotting_utilities import plot_phasecurve\n", + "from adler.utilities.plotting_utilities import plot_errorbar\n", "\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", @@ -56,7 +56,19 @@ "metadata": {}, "outputs": [], "source": [ - "fig = plot_phasecurve(planetoid, filt_list=[\"r\"])" + "fig = plot_errorbar(planetoid, filt_list=[\"r\"])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fd41e5bf", + "metadata": {}, + "outputs": [], + "source": [ + "# we can access axes properties and update them after the fact\n", + "\n", + "# ax1.__dict__" ] }, { @@ -133,7 +145,7 @@ "outputs": [], "source": [ "# we can also pass the fig object to the plotting function again to add more data\n", - "fig2 = plot_phasecurve(planetoid, fig=fig, filt_list=[\"u\", \"g\"], label_list=[\"u\", \"g\"])\n", + "fig2 = plot_errorbar(planetoid, fig=fig, filt_list=[\"g\", \"i\"], label_list=[\"g\", \"i\"])\n", "\n", "# update the legend\n", "ax1 = fig2.axes[0]\n", @@ -153,11 +165,11 @@ { "cell_type": "code", "execution_count": null, - "id": "be921f85", + "id": "f5a183bb", "metadata": {}, "outputs": [], "source": [ - "ax1.__dict__" + "# inspect the different items that have been plotted" ] }, { @@ -187,7 +199,7 @@ "metadata": {}, "outputs": [], "source": [ - "# we can access axes properties and update them after the fact\n", + "# add the legend label to the r filter data\n", "ax1.containers[0]._label = \"r\"\n", "ax1.legend()" ] @@ -208,6 +220,27 @@ "id": "a56d5711", "metadata": {}, "outputs": [], + "source": [ + "# we can plot nothing, but save the figure\n", + "fig3 = plot_errorbar(planetoid, fig=fig2, filename=\"phase_curve_{}.png\".format(ssoid))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1c89f107", + "metadata": {}, + "outputs": [], + "source": [ + "fig3" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b76a0b12", + "metadata": {}, + "outputs": [], "source": [] } ], diff --git a/src/adler/utilities/plotting_utilities.py b/src/adler/utilities/plotting_utilities.py index f9b1d29..a77de00 100644 --- a/src/adler/utilities/plotting_utilities.py +++ b/src/adler/utilities/plotting_utilities.py @@ -2,15 +2,16 @@ import matplotlib.gridspec as gridspec -def plot_phasecurve( +def plot_errorbar( planetoid, - filt_list=["r"], + filt_list=[], x_plot="phaseAngle", y_plot="reduced_mag", xerr_plot="magErr", fig=None, label_list=None, col_list=None, + filename=None, ): """Make an errorbar scatter plot of reduced magnitude against phase angle to show the phase curve of an Adler object. @@ -30,6 +31,8 @@ def plot_phasecurve( Optional, labels for errorbar plot elements col_list: list Optional, colors for errorbar scatter points + filename: str + Optional, if provided save the figure with this filename Returns ----------- @@ -72,4 +75,8 @@ def plot_phasecurve( # plot the errorbars ax1.errorbar(x, y, xerr, color=c, fmt="o", label=l) + # save the figure? + if filename: + fig.savefig(filename, facecolor="w", transparent=True, bbox_inches="tight") + return fig