2

我正在使用 yt-Project 库来可视化数据并创建绘图。现在,我想创建一个包含两个子图的图。似乎这不能直接使用 yt 并且您必须使用 matplotlib 进行进一步的自定义(在此处描述)。不习惯matplotlib(和一般的python)我尝试了这样的事情:

slc = yt.SlicePlot(ds, 'x', 'density')
dens_plot = slc.plots['density']

fig = dens_plot.figure
ax = dens_plot.axes
#colorbar_axes = dens_plot.cax

new_ax2 = fig.add_subplot(212)

slc.save()

但是,它没有在第一个子图下方添加另一个子图,而是将其添加到其中。 在此处输入图像描述

我想要实现的是来自不同数据集的另一个图,该图具有相同的颜色条和相同的 x 和 y 轴,位于第一个下方。

谢谢您的帮助。

4

1 回答 1

3

现在最简单的方法是使用 AxesGrid,就像在这个 yt 食谱示例这个.

这是一个使用 yt 3.2.1 在时间序列中绘制两次气体密度的示例。我使用的示例数据可以从http://yt-project.org/data下载。

import yt
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import AxesGrid

fns = ['enzo_tiny_cosmology/DD0005/DD0005', 'enzo_tiny_cosmology/DD0040/DD0040']

fig = plt.figure()

# See http://matplotlib.org/mpl_toolkits/axes_grid/api/axes_grid_api.html
# These choices of keyword arguments produce a four panel plot with a single
# shared narrow colorbar on the right hand side of the multipanel plot. Axes
# labels are drawn for all plots since we're slicing along different directions
# for each plot.
grid = AxesGrid(fig, (0.075,0.075,0.85,0.85),
                nrows_ncols = (2, 1),
                axes_pad = 0.05,
                label_mode = "L",
                share_all = True,
                cbar_location="right",
                cbar_mode="single",
                cbar_size="3%",
                cbar_pad="0%")

for i, fn in enumerate(fns):
    # Load the data and create a single plot
    ds = yt.load(fn) # load data

    # Make a ProjectionPlot with a width of 34 comoving megaparsecs
    p = yt.ProjectionPlot(ds, 'z', 'density', width=(34, 'Mpccm'))

    # Ensure the colorbar limits match for all plots
    p.set_zlim('density', 1e-4, 1e-2)

    # This forces the ProjectionPlot to redraw itself on the AxesGrid axes.
    plot = p.plots['density']
    plot.figure = fig
    plot.axes = grid[i].axes
    plot.cax = grid.cbar_axes[i]

    # Finally, this actually redraws the plot.
    p._setup_plots()

plt.savefig('multiplot_1x2_time_series.png', bbox_inches='tight')

yt 多图

您也可以按照自己的方式进行操作(使用fig.add_subplots代替AxesGrid),但您需要手动定位轴并调整图形大小。

最后,如果您希望图形更小,您可以通过在创建图形时传递以英寸为单位的图形大小来控制图形的大小plt.figure()。如果您这样做,您可能还需要通过调用p.set_font_size().ProjectionPlot

于 2015-10-13T05:39:42.307 回答