如何使用 set_cmap 设置 yt.SlicePlot 的颜色栏限制?
How can I set the colorbar limits for a yt.SlicePlot using set_cmap?
我对 Python 完全陌生,我完全迷路了。
我的主管帮助我生成了一个脚本来查看 3D 速度模型的一些切片:
import numpy as np
import matplotlib.pyplot as plt
import yt
from yt.units import km
#Import et reshape data
d = np.genfromtxt('velocity_model.txt', delimiter=' ')
nd=22
nx=131
vel = d[:,3].reshape(nd,nx,nx)
lat = d[:,0].reshape(nd,nx,nx)
lon = d[:,1].reshape(nd,nx,nx)
dep = d[:,2].reshape(nd,nx,nx)
# When this is read into YT, depth increases along x axis, longitude increases along y axis and latitude increases along z axis, need to swap x and z and then flip z
dep=dep.swapaxes(0,2) # swap first and third dimensions: gives lon (x), lat (y), depth (z)
vel=vel.swapaxes(0,2) # swap first and third dimensions:
lat=lat.swapaxes(0,2) # swap first and third dimensions:
lon=lon.swapaxes(0,2) # swap first and third dimensions:
dep=dep[:,:,::-1] # reverse z direction
vel=vel[:,:,::-1] # swap first and 2nd dimensions:
lat=lat[:,:,::-1] # swap first and 2nd dimensions:
lon=lon[:,:,::-1] # swap first and 2nd dimensions:
xmin=0
xmax=289
ymin=0
ymax=289
zmin=-100
zmax=5
#Entrer dans YT
data=dict(velocity=(vel,'km/s'),latitude=(lat,'deg'),longitude=(lon,'deg'),depth=(dep,'km'))
bbox = np.array([[xmin,xmax], [ymin,ymax], [zmin,zmax]])
ds=yt.load_uniform_grid(data,vel.shape, length_unit='km', bbox=bbox)
#Off-Axis Slice
for key in ['latitude','longitude','depth','velocity'] :
L = [0,0,1] # cutting plane=z
slicepos=-50
c = [(xmax-xmin)/2, (ymax-ymin)/2, slicepos]
cut = yt.SlicePlot(ds, L, key,origin='native',center=c) #, width=(200,90,'km'))
cut.set_log(key, False)
cut.annotate_text([0.5,0.9],'z={:d} km'.format(slicepos),coord_system='axis')
cut.set_cmap(field='velocity',cmap='jet_r')
cut.save()
使用这个脚本,我想修复颜色条,因为对于每张图像,颜色条都会发生变化,而且像这样解释起来并不容易。
我试过像这样添加限制:
h=colorbar
h.Limits = [5 9]
cut.set_cmap(field='velocity',cmap='jet_r', h)
但这不是好方法。有人有想法吗?我看到了很多东西,但没有看到 cmap。
这确实是一个关于 yt visualization library 而不是 matplotlib 本身的问题 - 我已经编辑了标题和标签以反映这一点。
我以前从未遇到过 yt,但根据 yt.SlicePlot
, it seems that cut
will either be an AxisAlignedSlicePlot
or an OffAxisSlicePlot
object. Both of these classes have a .set_zlim()
方法的官方文档,该方法似乎可以满足您的要求:
AxisAlignedSlicePlot.set_zlim(*args, **kwargs)
set the scale of the
colormap
Parameters:
field : string
the field to set a colormap scale if field == ‘all’, applies to all
plots.
zmin : float
the new minimum of the colormap scale. If ‘min’,
will set to the minimum value in the current view.
zmax : float
the new maximum of the colormap scale. If ‘max’, will set to the maximum
value in the current view.
Other Parameters:
dynamic_range : float (default: None)
The dynamic range of the image. If zmin == None, will set zmin = zmax / dynamic_range If zmax == None, will set zmax = zmin * dynamic_range. When dynamic_range is specified, defaults to setting zmin = zmax / dynamic_range.
换句话说,您可能会使用:
cut.set_zlim(field='velocity', zmin=5, zmax=9)
您正在查找 set_zlim
函数:
set_cmap
函数只允许您选择您想要的颜色图,它不允许您设置颜色图范围。为此,您需要使用 set_zlim
。这是一个示例,使用来自 http://yt-project.org/data:
的示例数据集之一
import yt
ds = yt.load('IsolatedGalaxy/galaxy0030/galaxy0030')
plot = yt.SlicePlot(ds, 2, 'density')
plot.set_cmap('density', 'viridis')
plot.set_zlim('density', 1e-28, 1e-25)
这会生成以下图像:
我对 Python 完全陌生,我完全迷路了。 我的主管帮助我生成了一个脚本来查看 3D 速度模型的一些切片:
import numpy as np
import matplotlib.pyplot as plt
import yt
from yt.units import km
#Import et reshape data
d = np.genfromtxt('velocity_model.txt', delimiter=' ')
nd=22
nx=131
vel = d[:,3].reshape(nd,nx,nx)
lat = d[:,0].reshape(nd,nx,nx)
lon = d[:,1].reshape(nd,nx,nx)
dep = d[:,2].reshape(nd,nx,nx)
# When this is read into YT, depth increases along x axis, longitude increases along y axis and latitude increases along z axis, need to swap x and z and then flip z
dep=dep.swapaxes(0,2) # swap first and third dimensions: gives lon (x), lat (y), depth (z)
vel=vel.swapaxes(0,2) # swap first and third dimensions:
lat=lat.swapaxes(0,2) # swap first and third dimensions:
lon=lon.swapaxes(0,2) # swap first and third dimensions:
dep=dep[:,:,::-1] # reverse z direction
vel=vel[:,:,::-1] # swap first and 2nd dimensions:
lat=lat[:,:,::-1] # swap first and 2nd dimensions:
lon=lon[:,:,::-1] # swap first and 2nd dimensions:
xmin=0
xmax=289
ymin=0
ymax=289
zmin=-100
zmax=5
#Entrer dans YT
data=dict(velocity=(vel,'km/s'),latitude=(lat,'deg'),longitude=(lon,'deg'),depth=(dep,'km'))
bbox = np.array([[xmin,xmax], [ymin,ymax], [zmin,zmax]])
ds=yt.load_uniform_grid(data,vel.shape, length_unit='km', bbox=bbox)
#Off-Axis Slice
for key in ['latitude','longitude','depth','velocity'] :
L = [0,0,1] # cutting plane=z
slicepos=-50
c = [(xmax-xmin)/2, (ymax-ymin)/2, slicepos]
cut = yt.SlicePlot(ds, L, key,origin='native',center=c) #, width=(200,90,'km'))
cut.set_log(key, False)
cut.annotate_text([0.5,0.9],'z={:d} km'.format(slicepos),coord_system='axis')
cut.set_cmap(field='velocity',cmap='jet_r')
cut.save()
使用这个脚本,我想修复颜色条,因为对于每张图像,颜色条都会发生变化,而且像这样解释起来并不容易。
我试过像这样添加限制:
h=colorbar
h.Limits = [5 9]
cut.set_cmap(field='velocity',cmap='jet_r', h)
但这不是好方法。有人有想法吗?我看到了很多东西,但没有看到 cmap。
这确实是一个关于 yt visualization library 而不是 matplotlib 本身的问题 - 我已经编辑了标题和标签以反映这一点。
我以前从未遇到过 yt,但根据 yt.SlicePlot
, it seems that cut
will either be an AxisAlignedSlicePlot
or an OffAxisSlicePlot
object. Both of these classes have a .set_zlim()
方法的官方文档,该方法似乎可以满足您的要求:
AxisAlignedSlicePlot.set_zlim(*args, **kwargs)
set the scale of the colormap
Parameters:
field : string
the field to set a colormap scale if field == ‘all’, applies to all plots.
zmin : float
the new minimum of the colormap scale. If ‘min’, will set to the minimum value in the current view.
zmax : float
the new maximum of the colormap scale. If ‘max’, will set to the maximum value in the current view.
Other Parameters:
dynamic_range : float (default: None)
The dynamic range of the image. If zmin == None, will set zmin = zmax / dynamic_range If zmax == None, will set zmax = zmin * dynamic_range. When dynamic_range is specified, defaults to setting zmin = zmax / dynamic_range.
换句话说,您可能会使用:
cut.set_zlim(field='velocity', zmin=5, zmax=9)
您正在查找 set_zlim
函数:
set_cmap
函数只允许您选择您想要的颜色图,它不允许您设置颜色图范围。为此,您需要使用 set_zlim
。这是一个示例,使用来自 http://yt-project.org/data:
import yt
ds = yt.load('IsolatedGalaxy/galaxy0030/galaxy0030')
plot = yt.SlicePlot(ds, 2, 'density')
plot.set_cmap('density', 'viridis')
plot.set_zlim('density', 1e-28, 1e-25)
这会生成以下图像: