2014-02-24 4 views
1

3D 데이터로 히트 맵을 생성하고 싶습니다.matplotlib의 3D 데이터를 사용한 히트 맵

이 데이터를 사용하여 trisurf를 플로팅 할 수있었습니다.

히트 맵을 생성하는 데 도움이 될 수 있습니까? 온라인 자습서를 보았지만 3D 용으로 모두 상당히 복잡해 보입니다. 이 웹 사이트에서 matplotlib에 scatter point가있는 히트 맵을 생성했지만 2D 데이터 만 가지고 있습니다. trisurf를 생성

내 코드

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

3 데이터 열이다. X, Y, Z라고 말하십시오. 색상이있는 3D scatter plot을 시도했습니다. 그러나 히트 맵을 찾고 있습니다. 당신은 단지 "색상에 대한 3 차원을 사용하고자하는"경우

+0

나는 3D 입방경을 색으로 표시 하겠지만, 데이터에 어떤 구조가 없는지 알 수는 없습니다. 지금까지 코드를 게시하지 않는 것이 좋습니다. – ysakamoto

+0

어떤 종류의 열지도가 필요하십니까? 데이터가 4 차원이고 4 차원으로 색상을 결정하고 싶습니까? 음모가 무엇을 원하는지 질문에서 분명하지 않습니다. –

+0

데이터는 3 차원입니다. 나는 채색을 위해 3 차원을 사용하고 싶다. – Pretty

답변

0

, 당신이 이런 식으로 작업을 수행 할 수 있습니다

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

결과 - 히트 맵을 3D 데이터의 3 변수 색상으로 : Heatmap with 3rd variable of 3D data as colour 을 추가로 수행 할 수 있습니다 루프와 함께 각 축의 조합에 대한 플롯 :

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