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

phase curve plot func #114

Merged
merged 3 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
268 changes: 268 additions & 0 deletions notebooks/plotting_utilities.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
{
"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_errorbar\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_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__"
]
},
{
"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_errorbar(planetoid, fig=fig, filt_list=[\"g\", \"i\"], label_list=[\"g\", \"i\"])\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": "f5a183bb",
"metadata": {},
"outputs": [],
"source": [
"# inspect the different items that have been plotted"
]
},
{
"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": [
"# add the legend label to the r filter data\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": [
"# 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": []
}
],
"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
}
82 changes: 82 additions & 0 deletions src/adler/utilities/plotting_utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec


def plot_errorbar(
planetoid,
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.

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
filename: str
Optional, if provided save the figure with this filename

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)

# save the figure?
if filename:
fig.savefig(filename, facecolor="w", transparent=True, bbox_inches="tight")

return fig
Loading