Created notebook on plotting galaxies

This commit is contained in:
O . O 2019-03-11 15:49:00 -04:00
Родитель f464cbc404
Коммит 624006ac3f
1 изменённых файлов: 295 добавлений и 0 удалений

295
tutorials/galaxies.ipynb Normal file
Просмотреть файл

@ -0,0 +1,295 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\\*\\* *(under construction. If you're using Binder, please note that this tutorial is only fully functional in Firefox at the moment.)* \\*\\*\n",
"\n",
"Adapted from [a notebook](https://nbviewer.jupyter.org/github/marksubbarao/pyWWT_AAS225/blob/master/CosmicFlows.ipynb) by Mark SubbaRao that plots galactic data from [Tully et al. 2013](https://iopscience.iop.org/article/10.1088/0004-6256/146/4/86/meta)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"import numpy as np\n",
"\n",
"from astropy.coordinates import SkyCoord, Distance\n",
"from astropy import units as u\n",
"from astropy.table import Table, Column\n",
"from astropy.time import Time, TimeDelta\n",
"\n",
"from astroquery.vizier import Vizier\n",
"\n",
"from matplotlib import colors\n",
"import matplotlib.pyplot as plt\n",
"\n",
"from pywwt.jupyter import WWTJupyterWidget"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"wwt = WWTJupyterWidget()\n",
"wwt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*WWT works under HTTP, while Binder uses HTTPS. As such, in order to access all features of pywwt, Binder users will need to disable HTTPS for this session. Firefox users can do this with the following steps:*\n",
"- *Click the lock icon to the left of the address bar,*\n",
"- *Select the arrow next to “Connection,”*\n",
"- *Then, press the “Disable protection for now” button. (Protection is disabled for this page and only during this session, not for your whole browser. We are working on upgrading WWT so this step is no longer necessary.)*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Visualizing the *Cosmicflows-2* galaxy catalog"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(In this more simple example, we plot several galaxies in 3-D and color them by their velocities.)\n",
"\n",
"**First, we use a class from the `astroquery` package to query the Vizier catalog and return an `astropy` table that contains redshift-independent distances, peculiar velocities, and coordinates for thousnads of galaxies.** \n",
"\n",
"We begin by slimming down the table to the columns necessary for this example."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"v = Vizier()\n",
"v.ROW_LIMIT = -1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cats = v.get_catalogs('J/AJ/146/86')\n",
"cosmic_flows = cats[1]\n",
"cosmic_flows.keep_columns([\"GLON\", \"GLAT\", \"__Dist_\",\n",
" \"__PV_\", \"__Bmag_\",\"N1\"])\n",
"cosmic_flows.rename_column('__Dist_', 'Dist')\n",
"cosmic_flows.rename_column('__PV_', 'PV')\n",
"cosmic_flows.rename_column('__Bmag_', 'Bmag')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*(Open a new cell above this one and see what the table looks like by entering `cosmic_flows` and running it.)*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"~~**Next, since the latitude and longitude coordinates are in the galactic frame, we use `SkyCoord` from `astropy` to convert them to right ascension and declination, then add those columns to our table.**~~"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#coords = SkyCoord(cosmic_flows['GLON'], cosmic_flows['GLAT'],\n",
"# unit=(u.deg, u.deg),\n",
"# distance=Distance(cosmic_flows['Dist'],u.Mpc),\n",
"# )#frame='galactic')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#cosmic_flows.remove_columns(['RA', 'Dec'])\n",
"\n",
"#cosmic_flows.add_column(Column(coords.icrs.ra, name='ra'))\n",
"#cosmic_flows.add_column(Column(coords.icrs.dec, name='dec'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**`pywwt` can read colormap columns from tables when plotting points, so we'll continue by assigning colors to each galaxy.**\n",
"\n",
"`plt.cm` contains a number of pre-made colormap objects for us to choose from. To illustrate the peculiar velocity (`'PV'`) of these galaxies, I chose `magma`, whose color transition evokes thoughts of red & blueshift and should also be accessible to people with common forms of colorblindness."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# try twilight shifted, but also remember r/b colorblind accessibility\n",
"cmap = plt.cm.magma(cosmic_flows['PV'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#norm = plt.Normalize(min(cosmic_flows['PV']), max(cosmic_flows['PV']))\n",
"#colors2 = plt.cm.magma(norm(cosmic_flows['PV']))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"After getting an array of RGB colors, we convert them to hex codes and add a colormap column to our table."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cosmic_flows['cmap'] = '#000000'\n",
"for i, _ in enumerate(cosmic_flows['PV']):\n",
" cosmic_flows['cmap'][i] = colors.rgb2hex(cmap[i])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Re-run the cell where you examined `cosmic_flows` to see the new columns we've added to the table."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cosmic_flows"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**We're now ready to work in `pywwt`. Move the cell containing the `wwt` viewer down here. Then, switch to the solar system view so we can plot the galaxies in three-dimensional space.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"wwt.set_view('solar system')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Next, since the longitude and latitude columns of the table are in the galactic frame, we turn on `wwt`'s galactic mode.**\n",
"\n",
"To improve loading times, we also turn off the COSMOS galactic survey that normally loads in the background."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"wwt.galactic_mode = True\n",
"wwt.solar_system.cosmos = False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Finally, we're ready to plot a data layer based on the table we've modified.**\n",
"\n",
"We link the appropriate columns for longitude, latitude, altitude, and colormap. Since our \"altitude\" in this scenario is distance of the galaxy from our perspective, we set `alt_type` to `'distance'`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gals = wwt.layers.add_data_layer(table=cosmic_flows, frame='Sky',\n",
" #lon_att='ra', lat_att='dec',\n",
" lon_att='GLON', lat_att='GLAT',\n",
" alt_att='Dist', alt_type='distance',\n",
" cmap_att='cmap', size_scale=40)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Zoom out until you've reached galactic scale and see the thousands of galaxies points we've plotted.**\n",
"\n",
"It's also possible to edit a layer's attributes after its creation. If you would like to see all points even when they're on the far side, run the following cell:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gals.far_side_visible = True"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}