简体   繁体   English

如何使用set_cmap为yt.SlicePlot设置colorbar限制?

[英]How can I set the colorbar limits for a yt.SlicePlot using set_cmap?

I am totally new to Python and I am totally lost. 我是Python的新手,我完全迷失了。 My supervisor helped me to generate a script to see some slices of a 3D velocity model: 我的主管帮我生成了一个脚本来查看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()

With this script, I would like to fix the colorbar, because for each image this one change, and it's not easy to interpret like this. 使用这个脚本,我想修复颜色条,因为对于每个图像,这个更改,并且这样解释起来并不容易。

I tried to add limits like this: 我试图添加这样的限制:

h=colorbar
h.Limits = [5 9]
cut.set_cmap(field='velocity',cmap='jet_r', h)

But it's not the good way. 但这不是好方法。 Does someone have an idea? 有人有想法吗? I saw lot of things but not for cmap. 我看到很多东西,但不是cmap。

You're looking for the set_zlim function: 你正在寻找set_zlim函数:

http://yt-project.org/doc/reference/api/generated/yt.visualization.plot_window.AxisAlignedSlicePlot.set_zlim.html http://yt-project.org/doc/reference/api/generated/yt.visualization.plot_window.AxisAlignedSlicePlot.set_zlim.html

The set_cmap function just allows you to choose which colormap you want, it does not allow you to set the colormap range. set_cmap功能只是让你选择你想要颜色表,它不会让你设置的颜色表范围。 You need to use set_zlim for that. 你需要使用set_zlim Here's an example, using one of the sample datasets from http://yt-project.org/data : 以下是一个示例,使用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)

This produces the following image: 这会产生以下图像:

在此输入图像描述

This is really a question about the yt visualization library rather than matplotlib per se - I've edited the title and tags to reflect this. 这实际上是关于yt可视化库而不是matplotlib本身的问题 - 我编辑了标题和标签以反映这一点。

I have never come across yt before, but based on the official documentation for yt.SlicePlot , it seems that cut will either be an AxisAlignedSlicePlot or an OffAxisSlicePlot object. 我之前从未遇到过,但根据yt.SlicePlot的官方文档,似乎cut将是AxisAlignedSlicePlotOffAxisSlicePlot对象。 Both of these classes have a .set_zlim() method that seems to do what you want: 这两个类都有一个.set_zlim()方法,似乎可以做你想要的:

AxisAlignedSlicePlot.set_zlim(*args, **kwargs)

set the scale of the colormap 设置色彩映射的比例

Parameters: 参数:

  • field : string field:string

    the field to set a colormap scale if field == 'all', applies to all plots. 如果field =='all',则设置色彩映射比例的字段适用于所有绘图。

  • zmin : float zmin:浮动

    the new minimum of the colormap scale. 色彩图比例的新最小值。 If 'min', will set to the minimum value in the current view. 如果为'min',则将设置为当前视图中的最小值。

  • zmax : float zmax:浮动

    the new maximum of the colormap scale. 色彩图比例的新最大值。 If 'max', will set to the maximum value in the current view. 如果'max',将设置为当前视图中的最大值。

Other Parameters: 其他参数:

  • dynamic_range : float (default: None) dynamic_range:float(默认值:无)

    The dynamic range of the image. 图像的动态范围。 If zmin == None, will set zmin = zmax / dynamic_range If zmax == None, will set zmax = zmin * dynamic_range. 如果zmin == None,则设置zmin = zmax / dynamic_range如果zmax == None,则设置zmax = zmin * dynamic_range。 When dynamic_range is specified, defaults to setting zmin = zmax / dynamic_range. 指定dynamic_range时,默认为设置zmin = zmax / dynamic_range。

In other words, you could probably use: 换句话说,您可以使用:

cut.set_zlim(field='velocity', zmin=5, zmax=9)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM