phase_plot.py 2.6 KB
Newer Older
Loic Hausammann's avatar
Loic Hausammann committed
1 2 3
#!/usr/bin/env python3

import yt
Loic Hausammann's avatar
Loic Hausammann committed
4
from yt.units import Msun, amu, cm, K, kpc
Loic Hausammann's avatar
Loic Hausammann committed
5 6 7
from matplotlib.offsetbox import AnchoredText
import matplotlib.pyplot as plt

Loic Hausammann's avatar
Loic Hausammann committed
8
width = 5 * kpc
Loic Hausammann's avatar
Loic Hausammann committed
9 10 11 12
limits_temperature = (1e1 * K, 1e7 * K)
limits_density = (1e-7 * amu / cm**3, 1e7 * amu / cm**3)
limits_mass = (1e3 * Msun, 1e7 * Msun)

Loic Hausammann's avatar
Loic Hausammann committed
13 14 15

def save2DPlot(fig):
    fig.savefig("phase_2d.png", bbox_inches="tight",
Loic Hausammann's avatar
Loic Hausammann committed
16 17 18
                pad_inches=0.03, dpi=300)


Loic Hausammann's avatar
Loic Hausammann committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
def save1DPlot(profiles):
    plt.figure(figsize=(8, 8))
    plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=0.5)
    markers = ["s", "o"]
    for i, p in enumerate(profiles[0]):
        rho = p.x.in_units("g/cm**3").d
        mass = p["Masses"].in_units("Msun").d
        plt.plot(rho, mass, linestyle="-", marker=markers[i],
                 markeredgecolor='none', linewidth=1.2, alpha=0.8)
    plt.semilogx()
    plt.semilogy()

    plt.xlabel("$\mathrm{Density\ (g/cm^3)}$", fontsize='large')
    plt.ylabel(r"$\mathrm{Mass,}\/\mathrm{d}M\mathrm{/dlog}\/\mathrm{\rho}\/\mathrm{(M_{\odot})}$", fontsize='large')
    plt.legend(profiles[1], loc=4, frameon=True, ncol=2, fancybox=True)
    leg = plt.gca().get_legend()
    ltext = leg.get_texts()
    plt.setp(ltext, fontsize='small')
    plt.grid(True)

    plt.savefig("phase_1d.png", bbox_inches='tight', pad_inches=0.03, dpi=300)

Loic Hausammann's avatar
Loic Hausammann committed
41

Loic Hausammann's avatar
Loic Hausammann committed
42
def do2DPlot(f, name, i, fig, axes):
Loic Hausammann's avatar
Loic Hausammann committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    p = yt.PhasePlot(
        f, ("PartType0", "density"), ("PartType0", "Temperature"),
        ("PartType0", "mass"),
        weight_field=None, x_bins=300, y_bins=300)

    plt.title("Scale Factor %g" % f.scale_factor)

    # Set the unit system
    p.set_log("density", True)
    p.set_unit("density", "amu / cm**3")
    p.set_xlim(limits_density[0],
               limits_density[1])

    p.set_log("Temperature", True)
    p.set_unit("Temperature", "K")
    p.set_ylim(limits_temperature[0],
               limits_temperature[1])

    p.set_log("mass", True)
    p.set_unit("mass", "Msun")
    p.set_zlim("mass", limits_mass[0],
               limits_mass[1])

    plot = p.plots[("PartType0", "mass")]

    plot.figure = fig
    plot.axes = axes[i].axes

    # plot color bar
    if i != 0:
        plot.cax = axes.cbar_axes[0]
        # p.hide_axes()
    p._setup_plots()

    at = AnchoredText(name, loc=2, prop=dict(size=6), frameon=True)
    plot.axes.add_artist(at)
Loic Hausammann's avatar
Loic Hausammann committed
79 80 81 82 83 84 85 86 87 88


def do1DPlot(f, name, i):
    sp = f.sphere("max", width)
    # Because ParticleProfilePlot doesn't exist, I will do the following trick.
    p = yt.create_profile(sp, ("PartType0", "density"),  ("PartType0", "Masses"),
                          weight_field=None, n_bins=50,
                          accumulation=False)

    return p