Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
yt-project
GitHub Repository: yt-project/yt
Path: blob/main/doc/source/cookbook/multiplot_2x2.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 a four panel plot that includes
13
# four narrow colorbars, one for each plot. Axes labels are only drawn on the
14
# bottom left hand plot to avoid repeating information and make the plot less
15
# cluttered.
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="1",
22
share_all=True,
23
cbar_location="right",
24
cbar_mode="each",
25
cbar_size="3%",
26
cbar_pad="0%",
27
)
28
29
fields = [
30
("gas", "density"),
31
("gas", "velocity_x"),
32
("gas", "velocity_y"),
33
("gas", "velocity_magnitude"),
34
]
35
36
# Create the plot. Since SlicePlot accepts a list of fields, we need only
37
# do this once.
38
p = yt.SlicePlot(ds, "z", fields)
39
40
# Velocity is going to be both positive and negative, so let's make these
41
# slices use a linear colorbar scale
42
p.set_log(("gas", "velocity_x"), False)
43
p.set_log(("gas", "velocity_y"), False)
44
45
p.zoom(2)
46
47
# For each plotted field, force the SlicePlot to redraw itself onto the AxesGrid
48
# axes.
49
for i, field in enumerate(fields):
50
plot = p.plots[field]
51
plot.figure = fig
52
plot.axes = grid[i].axes
53
plot.cax = grid.cbar_axes[i]
54
55
# Finally, redraw the plot on the AxesGrid axes.
56
p.render()
57
58
plt.savefig("multiplot_2x2.png")
59
60