Skip to content

Commit

Permalink
reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
JoranAngevaare committed May 4, 2022
1 parent 0efd1de commit 05f7548
Showing 1 changed file with 38 additions and 41 deletions.
79 changes: 38 additions & 41 deletions thesis_plots/rotation_curve/rotation_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,57 +12,45 @@
import os
import thesis_plots

# %matplotlib inline
plt.rc('font', size=22)
plt.rcParams['figure.figsize'] = (10.0, 7.0)
plt.rc('text', usetex=True)
# # %matplotlib inline
# plt.rc('font', size=22)
# plt.rcParams['figure.figsize'] = (10.0, 7.0)
# plt.rc('text', usetex=True)


def stringformatter(s):
s = str(s)
s = s.replace(';', '')
s = s.replace(',', '.')
s = s.replace("b'", '')
s = s.replace("'", '')
return s

def read_curve(fn):
d = np.loadtxt(fn, converters={i: stringformatter for i in (0,1)})
return d[:, 0], d[:, 1]

class RotationCurve:
def __init__(self):
base = os.path.dirname(os.path.realpath(thesis_plots.__file__))
d = {}
data = {}
fns = ['fit', 'pts', 'pts+', 'pts-', 'halo', 'gas', 'stellar_disk', 'luminous+', 'luminous-']
for fn in fns:
d[fn + '_x'], d[fn] = read_curve(os.path.join(base, '..', 'data', 'rotation_curve', f'{fn}.txt'))
data[fn + '_x'], data[fn] = _read_curve(os.path.join(base, '..', 'data', 'rotation_curve', f'{fn}.txt'))

d['dpts'] = (d['pts+'] - d['pts-']) * 0.5
data['dpts'] = (data['pts+'] - data['pts-']) * 0.5

for fn in ['gas', 'stellar_disk', 'halo', 'fit', 'luminous+', 'luminous-']:
d[fn + '_x'] = np.concatenate([[0], d[fn + '_x']])
d[fn] = np.concatenate([[0], d[fn]])
data[fn + '_x'] = np.concatenate([[0], data[fn + '_x']])
data[fn] = np.concatenate([[0], data[fn]])

xp = np.linspace(0, 16, 200)
d['x'] = xp
d['baryons'] = np.sqrt(np.interp(xp, d['gas_x'], d['gas'])**2 + np.interp(xp, d['stellar_disk_x'], d['stellar_disk'])**2)
d['all'] = np.sqrt(
np.interp(xp, d['gas_x'], d['gas'])**2 +
np.interp(xp, d['stellar_disk_x'], d['stellar_disk'])**2 +
np.interp(xp, d['halo_x'], d['halo'])**2)
data['x'] = xp
data['baryons'] = np.sqrt(np.interp(xp, data['gas_x'], data['gas'])**2 + np.interp(xp, data['stellar_disk_x'], data['stellar_disk'])**2)
data['all'] = np.sqrt(
np.interp(xp, data['gas_x'], data['gas'])**2 +
np.interp(xp, data['stellar_disk_x'], data['stellar_disk'])**2 +
np.interp(xp, data['halo_x'], data['halo'])**2)

for pol in ('+', '-'):
d['lum%s' % pol] = np.interp(xp, d['luminous%s_x' % pol], d['luminous%s' % pol])
data['lum%s' % pol] = np.interp(xp, data['luminous%s_x' % pol], data['luminous%s' % pol])

self.d = d
self.data = data

def plot_rotation_curve(self):
d = self.d
plt.errorbar(d['pts_x'], d['pts'], d['dpts'], marker='.', capsize=5, ls='None', label='Measured')
plt.plot(d['stellar_disk_x'], d['stellar_disk'], label='Stars')
plt.plot(d['x'], d['baryons'], label='Stars and gas')
plt.plot(d['fit_x'], d['fit'], label='Stars, gas and dark matter')
data = self.data
plt.errorbar(data['pts_x'], data['pts'], data['dpts'], marker='.', capsize=5, ls='None', label='Measured')
plt.plot(data['stellar_disk_x'], data['stellar_disk'], label='Stars')
plt.plot(data['x'], data['baryons'], label='Stars and gas')
plt.plot(data['fit_x'], data['fit'], label='Stars, gas and dark matter')

# plt.plot(d['x'], d['all'], label='All')
# plt.plot(d['gas_x'], d['gas'], label='Gas')
Expand All @@ -72,11 +60,11 @@ def plot_rotation_curve(self):
plt.legend()

def plot_rotation_curve_fancy(self):
d = self.d
plt.errorbar(d['pts_x'], d['pts'], d['dpts'], marker='.', capsize=5, ls='None', label='Measured')
plt.plot(d['stellar_disk_x'], d['stellar_disk'], label='Stars', ls='--', lw=2.5)
plt.plot(d['x'], d['baryons'], label='Stars and gas', ls='-.', lw=2.5)
plt.plot(d['fit_x'], d['fit'], label='Stars, gas and dark matter', lw=2.5)
data = self.data
plt.errorbar(data['pts_x'], data['pts'], data['dpts'], marker='.', capsize=5, ls='None', label='Measured')
plt.plot(data['stellar_disk_x'], data['stellar_disk'], label='Stars', ls='--', lw=2.5)
plt.plot(data['x'], data['baryons'], label='Stars and gas', ls='-.', lw=2.5)
plt.plot(data['fit_x'], data['fit'], label='Stars, gas and dark matter', lw=2.5)

plt.xlabel(r'Radius (kpc)')
plt.ylabel(r'v$_c$ (km/s)')
Expand All @@ -86,12 +74,21 @@ def plot_rotation_curve_fancy(self):
plt.text(8.3, 62.5, 'Expected from stars and gas', color='C2', rotation = -8)
plt.text(8.75, 112, 'Including dark matter halo', color='C3', rotation = 8)

plt.fill_between(d['x'], d['lum-'], d['lum+'], color='C2', alpha=0.2)
plt.fill_between(data['x'], data['lum-'], data['lum+'], color='C2', alpha=0.2)

xp = np.linspace(0.001, 15, 200)
for start in np.arange(20, 701, 20):
plt.plot(xp, start * np.sqrt(1/xp), color='k', alpha=0.2, lw=1.5)

# plt.savefig('plots/rotationcurve.pdf', bbox_inches='tight')

def _string_fmt(s):
s = str(s)
s = s.replace(';', '')
s = s.replace(',', '.')
s = s.replace("b'", '')
s = s.replace("'", '')
return s

def _read_curve(fn):
d = np.loadtxt(fn, converters={i: _string_fmt for i in (0, 1)})
return d[:, 0], d[:, 1]

0 comments on commit 05f7548

Please sign in to comment.