gpt4 book ai didi

python - 在 Python 中将 4D 数据绘制为分层热图

转载 作者:太空宇宙 更新时间:2023-11-03 21:23:09 24 4
gpt4 key购买 nike

我想使用 (x,y,z) 坐标和基于颜色的第四维创建分层热图,以与强度相关联。

每个与图层相关的数据都位于一个文本文件中,其中包含 x、y、z 和 G 列。分隔符是空格。如果未能正确呈现,敬请原谅。

XA20060012001800240030002006001200180024003000

YA000000600600600600600600

ZA600600600600600600600600600600600600

遗传算法1.271.541.491.341.271.251.281.961.121.061.061.06

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

data = np.load(filename)

x = np.linspace(0,2400,num=6)
y = np.linspace(0,2400,num=11)
X,Y=np.meshgrid(x,y)
Z = data[:,:,0] * 1e-3

plt.contourf(X,Y,Z)
plt.colorbar()

如何读取文本文件、创建并沿 Z 轴叠加热图?

最佳答案

假设你有两个 txt 文件,即 data-z600.txtdata-z1200.txt,与你的 python 脚本位于同一文件夹中,其内容完全相同

data-z600.txt(您的)

XA YA ZA GA
200 0 600 1.27
600 0 600 1.54
1200 0 600 1.49
1800 0 600 1.34
2400 0 600 1.27
3000 0 600 1.25
200 600 600 1.28
600 600 600 1.96
1200 600 600 1.12
1800 600 600 1.06
2400 600 600 1.06
3000 600 600 1.06

data-z1200.txt(故意发明的)

XA YA ZA GA
200 0 1200 1.31
600 0 1200 2
1200 0 1200 1.63
1800 0 1200 1.36
2400 0 1200 1.31
3000 0 1200 1.35
200 600 1200 1.38
600 600 1200 1.36
1200 600 1200 1.2
1800 600 1200 1.1
2400 600 1200 1.1
3000 600 1200 1.11
<小时/>

让我们导入所有必需的库

# libraries
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import scipy.interpolate as si
from matplotlib import cm
import pandas as pd
import numpy as np

并定义grids_maker,该函数负责准备给定文件中包含的数据,此处通过filepath参数定位。

def grids_maker(filepath):
# Get the data
df = pd.read_csv(filepath, sep=' ')

# Make things more legible
xy = df[['XA', 'YA']]
x = xy.XA
y = xy.YA
z = df.ZA
g = df.GA
reso_x = reso_y = 50
interp = 'cubic' # or 'nearest' or 'linear'

# Convert the 4d-space's dimensions into grids
grid_x, grid_y = np.mgrid[
x.min():x.max():1j*reso_x,
y.min():y.max():1j*reso_y
]

grid_z = si.griddata(
xy, z.values,
(grid_x, grid_y),
method=interp
)

grid_g = si.griddata(
xy, g.values,
(grid_x, grid_y),
method=interp
)

return {
'x' : grid_x,
'y' : grid_y,
'z' : grid_z,
'g' : grid_g,
}

让我们对文件列表使用 grids_maker 并获取每个文件第四维的极值。

# Let's retrieve all files' contents
fgrids = dict.fromkeys([
'data-z600.txt',
'data-z1200.txt'
])
g_mins = []
g_maxs = []

for fpath in fgrids.keys():
fgrids[fpath] = grids = grids_maker(fpath)
g_mins.append(grids['g'].min())
g_maxs.append(grids['g'].max())

让我们创建我们的(所有文件统一的)色标

# Create the 4th color-rendered dimension
scam = plt.cm.ScalarMappable(
norm=cm.colors.Normalize(min(g_mins), max(g_maxs)),
cmap='jet' # see https://matplotlib.org/examples/color/colormaps_reference.html
)

...最后制作/显示情节

# Make the plot
fig = plt.figure()
ax = fig.gca(projection='3d')
for grids in fgrids.values():
scam.set_array([])
ax.plot_surface(
grids['x'], grids['y'], grids['z'],
facecolors = scam.to_rgba(grids['g']),
antialiased = True,
rstride=1, cstride=1, alpha=None
)
plt.show()

enter image description here

关于python - 在 Python 中将 4D 数据绘制为分层热图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54072839/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com