Yonbantai
Yonbantai

Reputation: 107

How to stack one heatmap on top of another

I created 2 2-D heatmap separately. Is there anyway I can display them together in a 3-D plot? More specifically, heatmaps will be put on x-y plane, while one will be at z = 10, the other one will be at z =20.

-----------------------------------------------------

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

from matplotlib.pyplot import figure

fig1 = plt.figure()
sns.heatmap(np.random.rand(10,10), xticklabels=False, yticklabels=False, cbar=False)

fig2 = plt.figure()
sns.heatmap(np.random.rand(10,10), xticklabels=False, yticklabels=False, cbar=False)

-----------------------------------------------------

I want the result to be similar to this one. The red and blue surfaces should be replaced by heatmaps.

enter image description here

Upvotes: 2

Views: 417

Answers (1)

Sheldore
Sheldore

Reputation: 39052

If I understood your question correctly, you want two flat surfaces. Here is an example of doing it in 3d. You can replace the x, y data with whatever values you want. The trick here is to have a constant value of z (10 and 20 here).

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

fig = plt.figure(figsize=(8, 4))
ax = fig.gca(projection='3d')

x = np.random.rand(50)
y = np.random.rand(50)
X, Y = np.meshgrid(x, y)

Z1 = 0*X + 10
Z2 = 0*X + 20

color_values = np.random.rand(len(y), len(x))
colors = plt.cm.jet(color_values)

ax.plot_surface(X, Y, Z1, facecolors=colors, antialiased=True)
ax.plot_surface(X, Y, Z2, facecolors=colors, antialiased=True)
ax.set_zlim(8, 21)

enter image description here

Upvotes: 1

Related Questions