import numpy as np12import yt34# Load the dataset. We'll work with a some Gadget data to illustrate all5# the different ways in which the effective resolution can vary. Specifically,6# we'll use the GadgetDiskGalaxy dataset available at7# http://yt-project.org/data/GadgetDiskGalaxy.tar.gz89# load the data with a refinement criteria of 2 particle per cell10# n.b. -- in yt-4.0, n_ref no longer exists as the data is no longer11# deposited only a grid. At present (03/15/2019), there is no way to12# handle non-gas data in Gadget snapshots, though that is work in progress13if int(yt.__version__[0]) < 4:14# increasing n_ref will result in a "lower resolution" (but faster) image,15# while decreasing it will go the opposite way16ds = yt.load("GadgetDiskGalaxy/snapshot_200.hdf5", n_ref=16)17else:18ds = yt.load("GadgetDiskGalaxy/snapshot_200.hdf5")1920# Create projections of the density (max value in each resolution element in the image):21prj = yt.ProjectionPlot(22ds, "x", ("gas", "density"), method="max", center="max", width=(100, "kpc")23)2425# nicen up the plot by using a better interpolation:26plot = prj.plots[list(prj.plots)[0]]27ax = plot.axes28img = ax.images[0]29img.set_interpolation("bicubic")3031# nicen up the plot by setting the background color to the minimum of the colorbar32prj.set_background_color(("gas", "density"))3334# vary the buff_size -- the number of resolution elements in the actual visualization35# set it to 2000x200036buff_size = 200037prj.set_buff_size(buff_size)3839# set the figure size in inches40figure_size = 1041prj.set_figure_size(figure_size)4243# if the image does not fill the plot (as is default, since the axes and44# colorbar contribute as well), then figuring out the proper dpi for a given45# buff_size and figure_size is non-trivial -- it requires finding the bbox46# for the actual image:47bounding_box = ax.get_position()48# we're going to scale to the larger of the two sides49image_size = figure_size * max([bounding_box.width, bounding_box.height])50# now save with a dpi that's scaled to the buff_size:51dpi = np.rint(np.ceil(buff_size / image_size))52prj.save("with_axes_colorbar.png", mpl_kwargs=dict(dpi=dpi))5354# in the case where the image fills the entire plot (i.e. if the axes and colorbar55# are turned off), it's trivial to figure out the correct dpi from the buff_size and56# figure_size (or vice versa):5758# hide the colorbar:59prj.hide_colorbar()6061# hide the axes, while still keeping the background color correct:62prj.hide_axes(draw_frame=True)6364# save with a dpi that makes sense:65dpi = np.rint(np.ceil(buff_size / figure_size))66prj.save("no_axes_colorbar.png", mpl_kwargs=dict(dpi=dpi))676869