Page MenuHomec4science

ark_idealized_dwarfs_pMagvsColor
No OneTemporary

File Metadata

Created
Tue, Sep 17, 07:45

ark_idealized_dwarfs_pMagvsColor

#!/usr/bin/python3
import os
import numpy as np
import argparse
import pickle
import matplotlib.pyplot as plt
####################################################################
# option parser
####################################################################
description="plot magnitude vs color"
epilog ="""
Examples:
--------
ark_idealized_dwarfs_pMagvsColor output.pkl
"""
parser = argparse.ArgumentParser(description=description,epilog=epilog,formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(action="store",
dest="files",
metavar='FILE',
type=str,
default=None,
nargs='*',
help='a list of files')
parser.add_argument("-o",
action="store",
type=str,
dest="outputfilename",
default=None,
help="Name of the output file")
parser.add_argument("--data",
action="store",
type=str,
dest="data",
default=None,
help="data file")
parser.add_argument('--xmin',
action="store",
dest="xmin",
metavar='FLOAT',
type=float,
default=0.5,
help='x min')
parser.add_argument('--xmax',
action="store",
dest="xmax",
metavar='FLOAT',
type=float,
default=1.5,
help='x max')
parser.add_argument('--ymin',
action="store",
dest="ymin",
metavar='FLOAT',
type=float,
default=-6,
help='y min')
parser.add_argument('--ymax',
action="store",
dest="ymax",
metavar='FLOAT',
type=float,
default=-20,
help='y max')
parser.add_argument('--log',
action="store",
dest="log",
metavar='STR',
type=str,
default=None,
help='log scale (None,x,y,xy)')
def SetAxis(ax,xmin, xmax, ymin, ymax, log=None):
"""
Set ticks for the axis
"""
#####################################
# first : slightly extend the limits
#####################################
if log is not None:
if str.find(log, 'x') != -1:
if xmin is not None:
xmin = np.log10(xmin)
if xmax is not None:
xmax = np.log10(xmax)
if log is not None:
if str.find(log, 'y') != -1:
if ymin is not None:
ymin = np.log10(ymin)
if ymax is not None:
ymax = np.log10(ymax)
if xmin is not None and xmax is not None:
if xmin == xmax:
xmin = xmin - 0.05 * xmin
xmax = xmax + 0.05 * xmax
else:
xmin = xmin - 0.05 * (xmax - xmin)
xmax = xmax + 0.05 * (xmax - xmin)
if ymin is not None and ymax is not None:
if ymin == ymax:
ymin = ymin - 0.05 * ymin
ymax = ymax + 0.05 * ymax
else:
ymin = ymin - 0.05 * (ymax - ymin)
ymax = ymax + 0.05 * (ymax - ymin)
if log is not None:
if str.find(log, 'x') != -1:
xmin = 10**xmin
xmax = 10**xmax
if log is not None:
if str.find(log, 'y') != -1:
ymin = 10**ymin
ymax = 10**ymax
#####################################
# second : set log log or lin log
#####################################
if log is not None:
if str.find(log, 'x') != -1 and str.find(log, 'y') != -1:
ax.loglog()
elif str.find(log, 'x') != -1:
ax.semilogx()
else:
ax.semilogy()
plt.axis([xmin, xmax, ymin, ymax])
if log is None:
log = 'None'
#####################################
# third : adapt ticks
#####################################
if str.find(log, 'x') == -1:
ax.xaxis.set_major_locator(plt.AutoLocator())
x_major = ax.xaxis.get_majorticklocs()
dx_minor = (x_major[-1] - x_major[0]) / (len(x_major) - 1) / 5.
ax.xaxis.set_minor_locator(plt.MultipleLocator(dx_minor))
if str.find(log, 'y') == -1:
ax.yaxis.set_major_locator(plt.AutoLocator())
y_major = ax.yaxis.get_majorticklocs()
dy_minor = (y_major[-1] - y_major[0]) / (len(y_major) - 1) / 5.
ax.yaxis.set_minor_locator(plt.MultipleLocator(dy_minor))
####################################################################
# main
####################################################################
if __name__ == '__main__':
opt = parser.parse_args()
params = {
"axes.labelsize": 14,
"axes.titlesize": 18,
"font.size": 12,
"legend.fontsize": 12,
"xtick.labelsize": 14,
"ytick.labelsize": 14,
"text.usetex": True,
"figure.subplot.left": 0.15,
"figure.subplot.right": 0.95,
"figure.subplot.bottom": 0.15,
"figure.subplot.top": 0.95,
"figure.subplot.wspace": 0.02,
"figure.subplot.hspace": 0.02,
"figure.figsize" : (8, 6),
"lines.markersize": 6,
"lines.linewidth": 2.0,
}
plt.rcParams.update(params)
cm = plt.cm.get_cmap('jet')
# create the plot
fig = plt.gcf()
fig.set_size_inches(8,6)
ax = plt.gca()
# loop over files
for filename in opt.files:
with open(filename, 'rb') as file:
database = pickle.load(file)
xs = []
ys = []
zs = []
for key in database.keys():
vis1 = database[key]["Mag_BPASS230_ARK_VIS1"]
nir1 = database[key]["Mag_BPASS230_ARK_NIR1"]
z = database[key]["Mass"]
x = vis1-nir1
y = nir1
xs.append(x)
ys.append(y)
zs.append(z)
sc =ax.scatter(xs,ys,c=np.log10(zs),s=10,cmap=cm,alpha=1)
############################################################################
# add data
############################################################################
if opt.data is not None:
from astropy.io import ascii
data = ascii.read(opt.data)
ID = data['ID']
LUM = data['Lum'] *1e6 # Lsol
MASS = data['Mass']*1e6 # Msol
NIR1 = data['BPASS230_ARK_NIR1']
VIS1 = data['BPASS230_ARK_VIS1']
VIS1mNIR1 = VIS1-NIR1
ax.scatter(VIS1mNIR1,NIR1,c=np.log10(MASS),s=100,cmap=cm,alpha=0.5)
# set the axis
#SetAxis(ax,opt.xmin,opt.xmax,opt.ymin,opt.ymax,log=opt.log)
# labels
ax.set_xlabel(r"VIS1-NIR1")
ax.set_ylabel(r"NIR1")
# limits
ax.set_xlim(opt.xmin,opt.xmax)
ax.set_ylim(opt.ymax,opt.ymin)
# invert axis
ax.invert_yaxis()
# colormap
plt.colorbar(sc,label='log10(Mass)')
# save or display
if opt.outputfilename:
plt.savefig(opt.outputfilename)
else:
plt.show()

Event Timeline