Path: blob/main/doc/source/cookbook/multi_plot_slice_and_proj.py
928 views
import numpy as np1from matplotlib.colors import LogNorm23import yt4from yt.visualization.base_plot_types import get_multi_plot56fn = "GasSloshing/sloshing_nomag2_hdf5_plt_cnt_0150" # dataset to load7orient = "horizontal"89ds = yt.load(fn) # load data1011# There's a lot in here:12# From this we get a containing figure, a list-of-lists of axes into which we13# can place plots, and some axes that we'll put colorbars.14# We feed it:15# Number of plots on the x-axis, number of plots on the y-axis, and how we16# want our colorbars oriented. (This governs where they will go, too.17# bw is the base-width in inches, but 4 is about right for most cases.18fig, axes, colorbars = get_multi_plot(3, 2, colorbar=orient, bw=4)1920slc = yt.SlicePlot(21ds,22"z",23fields=[("gas", "density"), ("gas", "temperature"), ("gas", "velocity_magnitude")],24)25proj = yt.ProjectionPlot(ds, "z", ("gas", "density"), weight_field=("gas", "density"))2627slc_frb = slc.data_source.to_frb((1.0, "Mpc"), 512)28proj_frb = proj.data_source.to_frb((1.0, "Mpc"), 512)2930dens_axes = [axes[0][0], axes[1][0]]31temp_axes = [axes[0][1], axes[1][1]]32vels_axes = [axes[0][2], axes[1][2]]3334for dax, tax, vax in zip(dens_axes, temp_axes, vels_axes):35dax.xaxis.set_visible(False)36dax.yaxis.set_visible(False)37tax.xaxis.set_visible(False)38tax.yaxis.set_visible(False)39vax.xaxis.set_visible(False)40vax.yaxis.set_visible(False)4142# Converting our Fixed Resolution Buffers to numpy arrays so that matplotlib43# can render them4445slc_dens = np.array(slc_frb["gas", "density"])46proj_dens = np.array(proj_frb["gas", "density"])47slc_temp = np.array(slc_frb["gas", "temperature"])48proj_temp = np.array(proj_frb["gas", "temperature"])49slc_vel = np.array(slc_frb["gas", "velocity_magnitude"])50proj_vel = np.array(proj_frb["gas", "velocity_magnitude"])5152plots = [53dens_axes[0].imshow(slc_dens, origin="lower", norm=LogNorm()),54dens_axes[1].imshow(proj_dens, origin="lower", norm=LogNorm()),55temp_axes[0].imshow(slc_temp, origin="lower"),56temp_axes[1].imshow(proj_temp, origin="lower"),57vels_axes[0].imshow(slc_vel, origin="lower", norm=LogNorm()),58vels_axes[1].imshow(proj_vel, origin="lower", norm=LogNorm()),59]6061plots[0].set_clim((1.0e-27, 1.0e-25))62plots[0].set_cmap("bds_highcontrast")63plots[1].set_clim((1.0e-27, 1.0e-25))64plots[1].set_cmap("bds_highcontrast")65plots[2].set_clim((1.0e7, 1.0e8))66plots[2].set_cmap("hot")67plots[3].set_clim((1.0e7, 1.0e8))68plots[3].set_cmap("hot")69plots[4].set_clim((1e6, 1e8))70plots[4].set_cmap("gist_rainbow")71plots[5].set_clim((1e6, 1e8))72plots[5].set_cmap("gist_rainbow")7374titles = [75r"$\mathrm{Density}\ (\mathrm{g\ cm^{-3}})$",76r"$\mathrm{Temperature}\ (\mathrm{K})$",77r"$\mathrm{Velocity Magnitude}\ (\mathrm{cm\ s^{-1}})$",78]7980for p, cax, t in zip(plots[0:6:2], colorbars, titles):81cbar = fig.colorbar(p, cax=cax, orientation=orient)82cbar.set_label(t)8384# And now we're done!85fig.savefig(f"{ds}_3x2")868788