Skip to content

Instantly share code, notes, and snippets.

@brittonsmith
Created March 1, 2022 13:01
Show Gist options
  • Save brittonsmith/2e7e52081bf3ddb2fbfd4b17045565aa to your computer and use it in GitHub Desktop.
Save brittonsmith/2e7e52081bf3ddb2fbfd4b17045565aa to your computer and use it in GitHub Desktop.
An example of plotting time on the bottom axis and redshift on the top.
from matplotlib import pyplot
from matplotlib.ticker import FuncFormatter
import numpy as np
from yt.utilities.cosmology import Cosmology
import matplotlib as mpl
mpl.rcParams['axes.unicode_minus'] = False
def _z_from_t(t, pos):
global co
return "%d" % np.round(co.z_from_t(co.quan(t, "Gyr")))
if __name__ == "__main__":
co = Cosmology(omega_matter=0.266,
omega_lambda=0.734,
hubble_constant=0.71)
my_fig, my_axes = pyplot.subplots(figsize=(10, 6))
xlim = (14, 0)
tx = my_axes.twiny()
tx.xaxis.tick_top()
# Set a list of redshifts to be included on the redshift axis
z_ticks_in_t = co.t_from_z(np.array([8, 5, 4, 3, 2, 1, 0])).in_units("Gyr")
tx.xaxis.set_ticks(z_ticks_in_t.d)
tx.xaxis.set_major_formatter(FuncFormatter(_z_from_t))
tx.set_xlim(xlim)
tx.xaxis.set_label_text("z")
my_axes.xaxis.set_ticks(np.arange(xlim[0], xlim[1]+1, 5), minor=True)
my_axes.xaxis.set_label_text("t [Gyr]")
my_axes.set_xlim(xlim[0], xlim[1])
pyplot.savefig('cosmo.pdf')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment