1
votes

I want to generate a heat map with my 3D data.

I have been able to plot trisurf using this data.

Can some one help me generate a heat map? I saw the online tutorials but they all seem quite complex for 3D. I found one on this website 'generating heatmap with scatter point in matplotlib but that is having only 2D data.

My code to generate trisurf is

from mpl_toolkits.mplot3d import Axes3D

from matplotlib import cm
import matplotlib.pyplot as plt
import numpy as np

n_angles = 36
n_radii = 8

# An array of radii
# Does not include radius r=0, this is to eliminate duplicate points
radii = np.linspace(0.125, 1.0, n_radii)

# An array of angles
angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False)

# Repeat all angles for each radius
angles = np.repeat(angles[...,np.newaxis], n_radii, axis=1)

# Convert polar (radii, angles) coords to cartesian (x, y) coords
# (0, 0) is added here. There are no duplicate points in the (x, y) plane

x,y,z =np.loadtxt('output/flash_KR_endowment_duration_3D.dat',delimiter='\t',usecols=(0,1,2),unpack=True)
#x,y,z =np.loadtxt('output/disk_KR_endowment_duration_3D.dat',delimiter='\t',usecols=(0,1,2),unpack=True)


fig = plt.figure()
ax = fig.gca(projection='3d')
#fig.suptitle(suptitle, fontsize=12, fontweight='bold')


#ax.set_title("Disk Kryder's Rate-Endowment-Duration Plot",fontsize=12)
ax.set_title("Flash Kryder's Rate-Endowment-Duration Plot",fontsize=12)

ax.set_xlabel("Kryder's rate")
ax.set_ylabel("Duration")
ax.set_zlabel("Endowment")


surf = ax.plot_trisurf(x, y, z, cmap=cm.jet, linewidth=0.2)
fig.colorbar(surf, shrink=.7, aspect=20)

plt.show()

Data is 3 column. say X,Y,Z. I have tried 3D scatter plot with color. But I am looking for heatmap.

1
I would say 3D scatter plot with color, but we cant tell unless what structure your data has. Why don't you post your code so far. - ysakamoto
What kind of heat map are you looking for? Is your data 4-dimensional, and you want the color to be determined by the fourth dimension? It's not clear from the question what you want the plot to look like. - Joan Smith
Data is 3 dimensional. I want to use 3rd dimension for coloring. - user1792899
Are you looking for imshow which is for 2+1D data. - tacaswell

1 Answers

0
votes

If you only "want to use 3rd dimension for coloring", you can do it like this:

import pandas as pd
import numpy as np
import plotly.plotly as plotly
from plotly.graph_objs import Data, Heatmap

plotly.sign_in("username", "api_key") # this is annoying but you can get one after registering - free

# generate tridimentional data
pp = pd.Panel(np.random.rand(20, 20, 20))

# crunch (sum, average...) data along one axis
crunch = pp.sum(axis=0)

# now plot with plot.ly or matplotlib as you wish
data = Data([Heatmap(z=np.array(crunch))])
plotly.image.save_as(data, "filename.pdf")

Result - heatmap with 3rd variable of 3D data as colour: Heatmap with 3rd variable of 3D data as colour Additionally, you can plot for each combination of axis with a loop:

## Plot
# for each axis, sum data along axis, plot heatmap
# dict is axis:[x,y,z], where z is a count of that variable
desc = {0 : ["ax1", "ax2", "ax3"], 1 : ["ax1", "ax2", "ax3"], 2 : ["ax1", "ax2", "ax3"]}
for axis in xrange(3):
    # crunch (sum) data along one axis
    crunch = pp.sum(axis=axis)

    # now let's plot
    data = Data([Heatmap(
        z=np.array(crunch),
        x=crunch.columns,
        y=crunch.index)]
    )
    plotly.image.save_as(data,
        "heatmap_{0}_vs_{1}_count_of_{2}".format(desc[axis][0], desc[axis][1], desc[axis][2])
    )