[yt-users] using show() in ipython/python

Timothy Waters waterst3 at unlv.nevada.edu
Tue Dec 2 16:37:25 PST 2014


Hi Johanna,

I had also wondered about this.  I took to scripting with
FixedResolutionBuffer like Nathan mentioned, and I have had good luck with
Matplotlib's ImageGrid command.  I use it to make both publishable plots
and animations/movies.  Below is a basic version of the type of script I
use in case that is helpful to you.

Tim

---------------------------------------------------------------
import yt
import array as ar
import numpy as np
import pylab as plt
#from pylab import *
import matplotlib.animation as animation
from yt.visualization.fixed_resolution import FixedResolutionBuffer
from mpl_toolkits.axes_grid1 import ImageGrid

################
# Globals
################
GATHER_IMAGES = False
ANIMATE = False
dumps = ar.array('i',(i for i in range(1,101,1)))
imagefolder = 'images'
xpix,ypix = 1024,512 #doesn't have to be resolution of simulation
xmin,xmax = 0.,1.
ymin,ymax = 0.,0.5

################
# Specify files
################
# ...

################
# Gather images
################
if GATHER_IMAGES:
for i in dumps:
ds = yt.load(file)
gs = ds.index.select_grids(ds.index.max_level)
grid = gs[0]
#xaxis = grid['x'][:,0,0] #can access variables if needed
for var in vars:
prj = ds.proj(var,2)
prj_frb = FixedResolutionBuffer(prj, (xmin,xmax,ymin,ymax),(ypix,xpix))
img_prj = prj_frb[var]
#at this step, can apply functions to prj_frb to alter image data, e.g.:
#img_prj = doSomething(img_frb[var])
img = np.array(img_prj)
img_name = var + `i` # (for example)
print "Saving image {}...".format(img_name)
file = open(imagefolder + img_name, 'w')
np.save(file,img)
file.close()

##################
# Setup ImageGrid
##################
N_SUBPLOTS = 2
fig = plt.figure(1, (7., 8.))
grid = ImageGrid(fig, 111, # similar to subplot(111)
                nrows_ncols = (2, 1), # creates 2x1 grid of axes
                axes_pad=0.15, # pad between axes in inch.
                direction='column',
                cbar_location='right',
                cbar_mode='each',
                cbar_pad =0.15,
                cbar_size='3.0%'
                )

def plotImages(iter):
i = dumps[0] + iter
for iplot in range(N_SUBPLOTS):
plt.setp(grid[iplot].get_yticklabels(), visible=False)
plt.setp(grid[iplot].get_xticklabels(), visible=False)
 if iplot == 0: #plot density
img_name = 'density' + `i`
img = np.load(imagefolder + img_name)
vmin = min(map(min,img))
vmax = max(map(max,img))
im = grid[iplot].imshow(img,vmin=vmin, vmax=vmax,
cmap='Spectral',extent=[xmin,xmax,ymin,ymax])
 elif iplot == 1: #plot pressure
img_name = 'pressure' + `i`
img = np.load(imagefolder + img_name)
vmin = min(map(min,img))
vmax = max(map(max,img))
im = grid[iplot].imshow(img,vmin=vmin, vmax=vmax,
cmap='Spectral',extent=[xmin,xmax,ymin,ymax])

  if not(ANIMATE):
for t in grid.cbar_axes[iplot].get_yticklabels():
t.set_visible(False)
t.set_fontsize(14)
t.set_visible(True)
 #show y ticks
plt.setp(grid[iplot].get_yticklabels(), visible=True)
 #set limits
grid[iplot].set_xlim(xmin,xmax)
grid[iplot].set_ylim(ymin,ymax)
 # show x-ticks
plt.setp(grid[2].get_xticklabels(), visible=True)
 # axes labels
grid[0].set_ylabel('density')
grid[1].set_ylabel('pressure')
############
# Make Plot
############
if GATHER_IMAGES:
sys.exit()
elif ANIMATE:
ani = animation.FuncAnimation(fig,plotImages,init_func=None,
    frames=len(dumps),interval=100, blit=False,repeat=False)
else:
idump = 1
plotProfiles(idump)

plt.show()

On Tue, Dec 2, 2014 at 2:45 PM, Nathan Goldbaum <nathan12343 at gmail.com>
wrote:

>
>
> On Tue, Dec 2, 2014 at 1:45 PM, Johanna Malinen <pjmaline at gmail.com>
> wrote:
>
>> Hi,
>>
>> I just installed the yt development version
>> http://hg.yt-project.org/yt/raw/yt/doc/install_script.sh
>> Version = 3.1-dev
>> Changeset = dc2ade033502 (yt) tip
>> into ubuntu 14.04
>> with
>>  INST_ZLIB=0
>>  INST_FTYPE=0
>>
>> yt seemed to install properly and it works otherwise, but it doesn't
>> open any windows when I'm trying to show an image, e.g.,
>> when running the script
>> http://yt-project.org/doc/quickstart/3)_Simple_Visualization.py
>> line by line in ipython:
>> p = yt.ProjectionPlot(ds, "y", "density")
>> p.show()   #doesn't open a window
>> In [35]: p.show()
>> <yt.visualization.plot_window.ProjectionPlot at 0x7fe1897941d0>
>> However, saving the image works:
>> p.save()
>>
>> When I run that script directly with python, I get the following error:
>> Traceback (most recent call last):
>>   File "3)_Simple_Visualization_short.py", line 28, in <module>
>>     p.show()
>>   File
>> "/home/.../yt/yt-x86_64/src/yt-hg/yt/visualization/plot_container.py",
>> line 622, in show
>>     raise YTNotInsideNotebook
>> yt.utilities.exceptions.YTNotInsideNotebook: This function only works
>> from within an IPython Notebook.
>>
>> I'm using
>> ipython --matplotlib
>> from pylab import *
>> and e.g. plot() and figure() open a window normally. Is there a way to
>> open a window with yt's show() and other functions
>> using ipython and python, or does that really require IPython Notebook?
>>
>
> Hi Johanna,
>
> This is a known issue:
>
>
> https://bitbucket.org/yt_analysis/yt/issue/660/support-for-interactive-mpl-backends
>
> We had a recent discussion about this on the dev mailing list:
>
>
> http://lists.spacepope.org/pipermail/yt-dev-spacepope.org/2014-November/013944.html
>
> So it's not impossible, but it will take some work.  I've wanted to get
> this set up for a while, but need to complete my Ph.D. before I can focus
> on this.  If someone else wants to step up and work on this, I think that
> would be an excellent contribution.
>
> For now you do need to work inside of the IPython notebook if you want to
> interactively work with yt plots.
>
> You can also manually create your plots using e.g. FixedResolutionBuffer
> and pyplot.
>
> Cheers,
>> Johanna
>> _______________________________________________
>> yt-users mailing list
>> yt-users at lists.spacepope.org
>> http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org
>>
>
>
> _______________________________________________
> yt-users mailing list
> yt-users at lists.spacepope.org
> http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.spacepope.org/pipermail/yt-users-spacepope.org/attachments/20141202/0f1266e5/attachment.htm>


More information about the yt-users mailing list