Path: blob/main/doc/source/cookbook/multiplot_2x2_coordaxes_slice.py
928 views
import matplotlib.pyplot as plt1from mpl_toolkits.axes_grid1 import AxesGrid23import yt45fn = "IsolatedGalaxy/galaxy0030/galaxy0030"6ds = yt.load(fn) # load data78fig = plt.figure()910# See http://matplotlib.org/mpl_toolkits/axes_grid/api/axes_grid_api.html11# These choices of keyword arguments produce two colorbars, both drawn on the12# right hand side. This means there are only two colorbar axes, one for Density13# and another for temperature. In addition, axes labels will be drawn for all14# plots.15grid = AxesGrid(16fig,17(0.075, 0.075, 0.85, 0.85),18nrows_ncols=(2, 2),19axes_pad=1.0,20label_mode="all",21share_all=True,22cbar_location="right",23cbar_mode="edge",24cbar_size="5%",25cbar_pad="0%",26)2728cuts = ["x", "y", "z", "z"]29fields = [30("gas", "density"),31("gas", "density"),32("gas", "density"),33("gas", "temperature"),34]3536for i, (direction, field) in enumerate(zip(cuts, fields)):37# Load the data and create a single plot38p = yt.SlicePlot(ds, direction, field)39p.zoom(40)4041# This forces the ProjectionPlot to redraw itself on the AxesGrid axes.42plot = p.plots[field]43plot.figure = fig44plot.axes = grid[i].axes4546# Since there are only two colorbar axes, we need to make sure we don't try47# to set the temperature colorbar to cbar_axes[4], which would if we used i48# to index cbar_axes, yielding a plot without a temperature colorbar.49# This unnecessarily redraws the Density colorbar three times, but that has50# no effect on the final plot.51if field == ("gas", "density"):52plot.cax = grid.cbar_axes[0]53elif field == ("gas", "temperature"):54plot.cax = grid.cbar_axes[1]5556# Finally, redraw the plot.57p.render()5859plt.savefig("multiplot_2x2_coordaxes_slice.png")606162