Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
yt-project
GitHub Repository: yt-project/yt
Path: blob/main/doc/source/cookbook/multi_plot_slice_and_proj.py
928 views
1
import numpy as np
2
from matplotlib.colors import LogNorm
3
4
import yt
5
from yt.visualization.base_plot_types import get_multi_plot
6
7
fn = "GasSloshing/sloshing_nomag2_hdf5_plt_cnt_0150" # dataset to load
8
orient = "horizontal"
9
10
ds = yt.load(fn) # load data
11
12
# There's a lot in here:
13
# From this we get a containing figure, a list-of-lists of axes into which we
14
# can place plots, and some axes that we'll put colorbars.
15
# We feed it:
16
# Number of plots on the x-axis, number of plots on the y-axis, and how we
17
# want our colorbars oriented. (This governs where they will go, too.
18
# bw is the base-width in inches, but 4 is about right for most cases.
19
fig, axes, colorbars = get_multi_plot(3, 2, colorbar=orient, bw=4)
20
21
slc = yt.SlicePlot(
22
ds,
23
"z",
24
fields=[("gas", "density"), ("gas", "temperature"), ("gas", "velocity_magnitude")],
25
)
26
proj = yt.ProjectionPlot(ds, "z", ("gas", "density"), weight_field=("gas", "density"))
27
28
slc_frb = slc.data_source.to_frb((1.0, "Mpc"), 512)
29
proj_frb = proj.data_source.to_frb((1.0, "Mpc"), 512)
30
31
dens_axes = [axes[0][0], axes[1][0]]
32
temp_axes = [axes[0][1], axes[1][1]]
33
vels_axes = [axes[0][2], axes[1][2]]
34
35
for dax, tax, vax in zip(dens_axes, temp_axes, vels_axes):
36
dax.xaxis.set_visible(False)
37
dax.yaxis.set_visible(False)
38
tax.xaxis.set_visible(False)
39
tax.yaxis.set_visible(False)
40
vax.xaxis.set_visible(False)
41
vax.yaxis.set_visible(False)
42
43
# Converting our Fixed Resolution Buffers to numpy arrays so that matplotlib
44
# can render them
45
46
slc_dens = np.array(slc_frb["gas", "density"])
47
proj_dens = np.array(proj_frb["gas", "density"])
48
slc_temp = np.array(slc_frb["gas", "temperature"])
49
proj_temp = np.array(proj_frb["gas", "temperature"])
50
slc_vel = np.array(slc_frb["gas", "velocity_magnitude"])
51
proj_vel = np.array(proj_frb["gas", "velocity_magnitude"])
52
53
plots = [
54
dens_axes[0].imshow(slc_dens, origin="lower", norm=LogNorm()),
55
dens_axes[1].imshow(proj_dens, origin="lower", norm=LogNorm()),
56
temp_axes[0].imshow(slc_temp, origin="lower"),
57
temp_axes[1].imshow(proj_temp, origin="lower"),
58
vels_axes[0].imshow(slc_vel, origin="lower", norm=LogNorm()),
59
vels_axes[1].imshow(proj_vel, origin="lower", norm=LogNorm()),
60
]
61
62
plots[0].set_clim((1.0e-27, 1.0e-25))
63
plots[0].set_cmap("bds_highcontrast")
64
plots[1].set_clim((1.0e-27, 1.0e-25))
65
plots[1].set_cmap("bds_highcontrast")
66
plots[2].set_clim((1.0e7, 1.0e8))
67
plots[2].set_cmap("hot")
68
plots[3].set_clim((1.0e7, 1.0e8))
69
plots[3].set_cmap("hot")
70
plots[4].set_clim((1e6, 1e8))
71
plots[4].set_cmap("gist_rainbow")
72
plots[5].set_clim((1e6, 1e8))
73
plots[5].set_cmap("gist_rainbow")
74
75
titles = [
76
r"$\mathrm{Density}\ (\mathrm{g\ cm^{-3}})$",
77
r"$\mathrm{Temperature}\ (\mathrm{K})$",
78
r"$\mathrm{Velocity Magnitude}\ (\mathrm{cm\ s^{-1}})$",
79
]
80
81
for p, cax, t in zip(plots[0:6:2], colorbars, titles):
82
cbar = fig.colorbar(p, cax=cax, orientation=orient)
83
cbar.set_label(t)
84
85
# And now we're done!
86
fig.savefig(f"{ds}_3x2")
87
88