In [48]:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from astroquery.ned import Ned
import astropy.units as u
from astropy import coordinates
from astropy.coordinates import SkyCoord
import pandas as pd
%matplotlib inline
In [2]:
from matplotlib.transforms import Affine2D
from matplotlib.projections import PolarAxes
from mpl_toolkits.axisartist import angle_helper
from mpl_toolkits.axisartist.grid_finder import MaxNLocator
from mpl_toolkits.axisartist.floating_axes import GridHelperCurveLinear, FloatingSubplot
def fractional_polar_axes(f, thlim=(0, 180), rlim=(0, 1), step=(30, 0.2),
thlabel='theta', rlabel='r', ticklabels=True):
"""Return polar axes that adhere to desired theta (in deg) and r limits. steps for theta
and r are really just hints for the locators. Using negative values for rlim causes
problems for GridHelperCurveLinear for some reason"""
th0, th1 = thlim # deg
r0, r1 = rlim
thstep, rstep = step
# scale degrees to radians:
tr_scale = Affine2D().scale(np.pi/180., 1.)
tr = tr_scale + PolarAxes.PolarTransform()
theta_grid_locator = angle_helper.LocatorDMS((th1-th0) // thstep)
r_grid_locator = MaxNLocator((r1-r0) // rstep)
theta_tick_formatter = angle_helper.FormatterDMS()
grid_helper = GridHelperCurveLinear(tr,
extremes=(th0, th1, r0, r1),
grid_locator1=theta_grid_locator,
grid_locator2=r_grid_locator,
tick_formatter1=theta_tick_formatter,
tick_formatter2=None)
a = FloatingSubplot(f, 111, grid_helper=grid_helper)
f.add_subplot(a)
# adjust x axis (theta):
a.axis["bottom"].set_visible(False)
a.axis["top"].set_axis_direction("bottom") # tick direction
a.axis["top"].toggle(ticklabels=ticklabels, label=bool(thlabel))
a.axis["top"].major_ticklabels.set_axis_direction("top")
a.axis["top"].label.set_axis_direction("top")
# adjust y axis (r):
a.axis["left"].set_axis_direction("bottom") # tick direction
a.axis["right"].set_axis_direction("top") # tick direction
a.axis["left"].toggle(ticklabels=ticklabels, label=bool(rlabel))
# add labels:
a.axis["top"].label.set_text(thlabel)
a.axis["left"].label.set_text(rlabel)
# create a parasite axes whose transData is theta, r:
auxa = a.get_aux_axes(tr)
# make aux_ax to have a clip path as in a?:
auxa.patch = a.patch
# this has a side effect that the patch is drawn twice, and possibly over some other
# artists. So, we decrease the zorder a bit to prevent this:
a.patch.zorder = -2
# add sector lines for both dimensions:
thticks = grid_helper.grid_info['lon_info'][0]
rticks = grid_helper.grid_info['lat_info'][0]
for th in thticks[1:-1]: # all but the first and last
auxa.plot([th, th], [r0, r1], '--', c='grey', zorder=-1)
for ri, r in enumerate(rticks):
# plot first r line as axes border in solid black only if it isn't at r=0
if ri == 0 and r != 0:
ls, lw, color = 'solid', 2, 'black'
else:
ls, lw, color = 'dashed', 1, 'grey'
# From http://stackoverflow.com/a/19828753/2020363
auxa.add_artist(plt.Circle([0, 0], radius=r, ls=ls, lw=lw, color=color, fill=False,
transform=auxa.transData._b, zorder=-1))
return auxa
In [24]:
co = coordinates.SkyCoord(ra=56.38, dec=38.43,
unit=(u.deg, u.deg), frame='fk4')
result_table = Ned.query_region(co, radius=2 * u.deg, equinox='B2000.0')
print(result_table)
In [14]:
dir(result_table['Redshift'])
Out[14]:
In [25]:
np.where(~result_table['Redshift'].data.mask)
Out[25]:
In [38]:
float('+124.424')
Out[38]:
In [41]:
tbl = pd.read_table('/Users/spardy/Downloads/vizier_votable.tsv', delimiter=';',
dtype={'RAJ2000': float, 'DEJ2000': float, 'cz': float})
In [44]:
plt.scatter(tbl.RAJ2000, tbl.DEJ2000, c=tbl.cz)
Out[44]:
In [50]:
plt.hist(tbl[tbl.cz == tbl.cz].cz)
Out[50]:
In [68]:
fig = plt.figure(figsize=(5, 5), dpi=200)
ax = fractional_polar_axes(fig, thlim=(0, 180), rlim=(0, 15000),
step=(30, 5000), thlabel='RA', rlabel='cz')
ax.plot(tbl.RAJ2000, tbl.cz, 'k.', alpha=0.1, markersize=1)
#ax.set_rmax(15000)
Out[68]:
In [5]:
tbl = pd.read_table('/Users/spardy/Data/2df_redshifts.tsv', delimiter=';')
In [7]:
tbl.head()
Out[7]:
In [25]:
coords = SkyCoord(ra=tbl.RAJ2000, dec=tbl.DEJ2000, unit=(u.hourangle, u.deg))
In [64]:
fig = plt.figure(figsize=(7, 5), dpi=200)
ax = fractional_polar_axes(fig, thlim=(150, 220), rlim=(0, 0.25),
step=(30, 0.05), thlabel='RA', rlabel='z')
ax.plot(coords.ra.value, tbl.z, 'k.', alpha=0.1, markersize=1)
#ax.set_rmax(15000)
#fig.tight_layout()
fig.savefig('/Users/spardy/Desktop/2df_plot1.png', dpi=600, bbox_inches='tight')
In [62]:
fig = plt.figure(figsize=(7, 5), dpi=200)
ax = fractional_polar_axes(fig, thlim=(-25, 45), rlim=(0, 0.25),
step=(30, 0.05), thlabel='RA', rlabel='z')
ax.plot(coords.ra.value, tbl.z, 'k.', alpha=0.1, markersize=1)
fig.savefig('/Users/spardy/Desktop/2df_plot2.png', dpi=600, bbox_inches='tight')
In [52]:
ax2.get_position()
Out[52]:
In [32]:
plt.hist(coords.ra.value)
Out[32]:
In [ ]: