Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
yt-project
GitHub Repository: yt-project/yt
Path: blob/main/doc/source/cookbook/multiplot_2x2_coordaxes_slice.py
928 views
1
import matplotlib.pyplot as plt
2
from mpl_toolkits.axes_grid1 import AxesGrid
3
4
import yt
5
6
fn = "IsolatedGalaxy/galaxy0030/galaxy0030"
7
ds = yt.load(fn) # load data
8
9
fig = plt.figure()
10
11
# See http://matplotlib.org/mpl_toolkits/axes_grid/api/axes_grid_api.html
12
# These choices of keyword arguments produce two colorbars, both drawn on the
13
# right hand side. This means there are only two colorbar axes, one for Density
14
# and another for temperature. In addition, axes labels will be drawn for all
15
# plots.
16
grid = AxesGrid(
17
fig,
18
(0.075, 0.075, 0.85, 0.85),
19
nrows_ncols=(2, 2),
20
axes_pad=1.0,
21
label_mode="all",
22
share_all=True,
23
cbar_location="right",
24
cbar_mode="edge",
25
cbar_size="5%",
26
cbar_pad="0%",
27
)
28
29
cuts = ["x", "y", "z", "z"]
30
fields = [
31
("gas", "density"),
32
("gas", "density"),
33
("gas", "density"),
34
("gas", "temperature"),
35
]
36
37
for i, (direction, field) in enumerate(zip(cuts, fields)):
38
# Load the data and create a single plot
39
p = yt.SlicePlot(ds, direction, field)
40
p.zoom(40)
41
42
# This forces the ProjectionPlot to redraw itself on the AxesGrid axes.
43
plot = p.plots[field]
44
plot.figure = fig
45
plot.axes = grid[i].axes
46
47
# Since there are only two colorbar axes, we need to make sure we don't try
48
# to set the temperature colorbar to cbar_axes[4], which would if we used i
49
# to index cbar_axes, yielding a plot without a temperature colorbar.
50
# This unnecessarily redraws the Density colorbar three times, but that has
51
# no effect on the final plot.
52
if field == ("gas", "density"):
53
plot.cax = grid.cbar_axes[0]
54
elif field == ("gas", "temperature"):
55
plot.cax = grid.cbar_axes[1]
56
57
# Finally, redraw the plot.
58
p.render()
59
60
plt.savefig("multiplot_2x2_coordaxes_slice.png")
61
62