2015-10-05 60 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