2017-03-15 5 views
1

Deshalb möchte ich, so etwas tun:Generierung Discrete Heatmap von 3D-Daten in Matplotlib

Generating a heat map using 3D data in matplotlib

Aber ich will nicht meine Farbe Achse kontinuierlich sein.


Meine Eingangsdaten ist von der Form:

x = [0, 0, 0, ... , 20, 20, 20, ..., 39, 39, 39] 
y = [0, 1, 2, ..., 0, 1, 2... , 37, 38, 39] 
z = [0, 1, 0, ..., 1, 1, 0, ..., 1, 0 ,0] 

Mit anderen Worten, meine z-Wert ist entweder eine 0 oder eine 1, und geht nie in-between.

Ich versuche:

import numpy 
import matplotlib.pyplot as plt 

#===Load Data===# 
data = numpy.loadtxt("class_map.dat"); 

#===Get Array Sizes===# 
max_x_size = int(numpy.sqrt(len(data[:,0]))); 
max_y_size = int(numpy.sqrt(len(data[:,1]))); 

#===Reshape Into Square Grid===# 
x = data[:,0]; xx = numpy.reshape(x, (max_x_size, max_x_size)); 
y = data[:,1]; yy = numpy.reshape(y, (max_y_size, max_y_size)); 
z = data[:,2]; zz = numpy.reshape(z, (max_x_size, max_y_size)); 

#===Plot===# 
plt.subplot(111) 
plt.contourf(xx,yy,zz) 
plt.colorbar() 
plt.show(); 

Aber was ich bekommen ist:

Result

Es sieht aus wie contourf ist mir eine kontinuierliche Funktion zu geben. Ich möchte, dass es einfach die Werte anzeigt, die sich bereits in der z-Variablen befinden, anstatt eine Funktion dafür anzupassen. Wie kann ich das machen?

Edit: Ich würde auch gerne die diskreten Werte (0 oder 1) auf einer Grauskala, wenn möglich.

Antwort

0

Sie können das levels Argument zu contour, z. B. levels=[0,0.5,1] verwenden.

import numpy as np 
import matplotlib.pyplot as plt 

#===Load Data===# 
x,y = np.meshgrid(np.arange(20),np.arange(20)) 
data = np.random.randint(0,2, size=(20,20)) 

#===Plot===# 
plt.subplot(111) 
plt.contourf(x,y,data, levels=[0,0.5,1]) 
plt.colorbar() 
plt.show() 

enter image description here

Eine weitere Option kann imshow()

import numpy as np 
import matplotlib.pyplot as plt 

#===Load Data===# 
x,y = np.meshgrid(np.arange(20),np.arange(20)) 
data = np.random.randint(0,2, size=(20,20)) 

#===Plot===# 
plt.subplot(111) 
plt.imshow(data, interpolation="None") 
plt.colorbar() 
plt.show() 

enter image description here

zu benutzen,