Skip to content Skip to sidebar Skip to footer

Plot 4d Data As Layered Heatmaps In Python

I would like to create layered heatmaps using (x,y,z) coordinates and a fourth dimension, color-based, to correlate to intensity. Each layer-related data sits in a text file with

Solution 1:

Say you have two txt files, namely data-z600.txt and data-z1200.txt, in the same folder as your python script, whose contents are exactly

data-z600.txt (yours)

XAYAZAGA20006001.2760006001.541200 06001.491800 06001.342400 06001.273000 06001.252006006001.286006006001.961200 6006001.121800 6006001.062400 6006001.063000 6006001.06

and data-z1200.txt (invented on purpose)

XAYAZAGA20001200 1.3160001200 21200 01200 1.631800 01200 1.362400 01200 1.313000 01200 1.352006001200 1.386006001200 1.361200 6001200 1.21800 6001200 1.12400 6001200 1.13000 6001200 1.11

Let's import all the required libraries

# librariesfrom 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

and define grids_maker, a function that does the job of preparing data contained in a given file, here targeted via the filepath argument.

defgrids_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,
    }

Let's use grids_maker over our list of files and get the extrema of each file's 4th dimension.

# 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())

Let's create our (all-file unifying) color-scale

# 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
)

... and finally make/show the plot

# 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

Post a Comment for "Plot 4d Data As Layered Heatmaps In Python"