2015-10-05 64 views
2

我正在使用yt-Project库来显示数据并创建图。 现在,我想创建一个包含两个子图的情节。看来这不是直接可能的,你必须使用matplotlib进一步定制(描述here)。 暂时不使用matplotlib(和一般蟒蛇)我想是这样的:在yt-Project plot中添加子图

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() 

但不是添加的第一个下插曲另一个,它增加了它在里面。 enter image description here

我想实现的是另一个来自不同数据集的曲线图,它们具有相同的颜色条和正好在第一个下面的相同的x和y轴。

谢谢你的帮助。

+0

感谢您使用yt!如果遇到更多问题,如果您向我们的邮件列表发送消息,您将获得更多的开发者关注。也就是说,我一定会在StackOverflow上关注未来的问题。 – ngoldbaum

回答

3

现在最简单的方法是使用AxesGrid,如in this yt cookbook example以及this one

下面是一个使用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 multiplot

你可以做到这一点(使用fig.add_subplots而不是AxesGrid)的方式,但你需要手动将轴定位,也调整身材。

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

+0

没关系,这工作得很好,但现在我遇到了一个不同的问题。 我正在使用yt通过自适应网格细化来可视化eulerian网格代码模拟。我想在这些地块中展示的是特定时间的电网结构。我发现yt主要是因为它的.annotate_grid()函数,它在像'yt.SlicePlot(ds,'x',“density”)。annotate_grids()。save()''这样的代码中工作起来相当方便。然而,在你的解决方案中,'p = yt.ProjectionPlot(ds,'z','density')。annotate_grids()'对绘图没有任何影响。有任何想法吗? –

+0

今天我会试着看看这个。 – ngoldbaum