Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
yt-project
GitHub Repository: yt-project/yt
Path: blob/main/doc/source/cookbook/image_resolution.py
928 views
1
import numpy as np
2
3
import yt
4
5
# Load the dataset. We'll work with a some Gadget data to illustrate all
6
# the different ways in which the effective resolution can vary. Specifically,
7
# we'll use the GadgetDiskGalaxy dataset available at
8
# http://yt-project.org/data/GadgetDiskGalaxy.tar.gz
9
10
# load the data with a refinement criteria of 2 particle per cell
11
# n.b. -- in yt-4.0, n_ref no longer exists as the data is no longer
12
# deposited only a grid. At present (03/15/2019), there is no way to
13
# handle non-gas data in Gadget snapshots, though that is work in progress
14
if int(yt.__version__[0]) < 4:
15
# increasing n_ref will result in a "lower resolution" (but faster) image,
16
# while decreasing it will go the opposite way
17
ds = yt.load("GadgetDiskGalaxy/snapshot_200.hdf5", n_ref=16)
18
else:
19
ds = yt.load("GadgetDiskGalaxy/snapshot_200.hdf5")
20
21
# Create projections of the density (max value in each resolution element in the image):
22
prj = yt.ProjectionPlot(
23
ds, "x", ("gas", "density"), method="max", center="max", width=(100, "kpc")
24
)
25
26
# nicen up the plot by using a better interpolation:
27
plot = prj.plots[list(prj.plots)[0]]
28
ax = plot.axes
29
img = ax.images[0]
30
img.set_interpolation("bicubic")
31
32
# nicen up the plot by setting the background color to the minimum of the colorbar
33
prj.set_background_color(("gas", "density"))
34
35
# vary the buff_size -- the number of resolution elements in the actual visualization
36
# set it to 2000x2000
37
buff_size = 2000
38
prj.set_buff_size(buff_size)
39
40
# set the figure size in inches
41
figure_size = 10
42
prj.set_figure_size(figure_size)
43
44
# if the image does not fill the plot (as is default, since the axes and
45
# colorbar contribute as well), then figuring out the proper dpi for a given
46
# buff_size and figure_size is non-trivial -- it requires finding the bbox
47
# for the actual image:
48
bounding_box = ax.get_position()
49
# we're going to scale to the larger of the two sides
50
image_size = figure_size * max([bounding_box.width, bounding_box.height])
51
# now save with a dpi that's scaled to the buff_size:
52
dpi = np.rint(np.ceil(buff_size / image_size))
53
prj.save("with_axes_colorbar.png", mpl_kwargs=dict(dpi=dpi))
54
55
# in the case where the image fills the entire plot (i.e. if the axes and colorbar
56
# are turned off), it's trivial to figure out the correct dpi from the buff_size and
57
# figure_size (or vice versa):
58
59
# hide the colorbar:
60
prj.hide_colorbar()
61
62
# hide the axes, while still keeping the background color correct:
63
prj.hide_axes(draw_frame=True)
64
65
# save with a dpi that makes sense:
66
dpi = np.rint(np.ceil(buff_size / figure_size))
67
prj.save("no_axes_colorbar.png", mpl_kwargs=dict(dpi=dpi))
68
69