簡體   English   中英

如何使用set_cmap為yt.SlicePlot設置colorbar限制?

[英]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。

你正在尋找set_zlim函數:

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

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)

這會產生以下圖像:

在此輸入圖像描述

這實際上是關於yt可視化庫而不是matplotlib本身的問題 - 我編輯了標題和標簽以反映這一點。

我之前從未遇到過,但根據yt.SlicePlot的官方文檔,似乎cut將是AxisAlignedSlicePlotOffAxisSlicePlot對象。 這兩個類都有一個.set_zlim()方法,似乎可以做你想要的:

AxisAlignedSlicePlot.set_zlim(*args, **kwargs)

設置色彩映射的比例

參數:

  • field:string

    如果field =='all',則設置色彩映射比例的字段適用於所有繪圖。

  • zmin:浮動

    色彩圖比例的新最小值。 如果為'min',則將設置為當前視圖中的最小值。

  • zmax:浮動

    色彩圖比例的新最大值。 如果'max',將設置為當前視圖中的最大值。

其他參數:

  • dynamic_range:float(默認值:無)

    圖像的動態范圍。 如果zmin == None,則設置zmin = zmax / dynamic_range如果zmax == None,則設置zmax = zmin * dynamic_range。 指定dynamic_range時,默認為設置zmin = zmax / dynamic_range。

換句話說,您可以使用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM