Ecco come farlo da un file CSV:
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import griddata
# Load data from CSV
dat = np.genfromtxt('dat.xyz', delimiter=' ',skip_header=0)
X_dat = dat[:,0]
Y_dat = dat[:,1]
Z_dat = dat[:,2]
# Convert from pandas dataframes to numpy arrays
X, Y, Z, = np.array([]), np.array([]), np.array([])
for i in range(len(X_dat)):
X = np.append(X,X_dat[i])
Y = np.append(Y,Y_dat[i])
Z = np.append(Z,Z_dat[i])
# create x-y points to be used in heatmap
xi = np.linspace(X.min(),X.max(),1000)
yi = np.linspace(Y.min(),Y.max(),1000)
# Z is a matrix of x-y values
zi = griddata((X, Y), Z, (xi[None,:], yi[:,None]), method='cubic')
# I control the range of my colorbar by removing data
# outside of my range of interest
zmin = 3
zmax = 12
zi[(zi<zmin) | (zi>zmax)] = None
# Create the contour plot
CS = plt.contourf(xi, yi, zi, 15, cmap=plt.cm.rainbow,
vmax=zmax, vmin=zmin)
plt.colorbar()
plt.show()
dove dat.xyz
è in forma
x1 y1 z1
x2 y2 z2
...
Eventuali duplicati di [tracciare una matrice 2D in Python , codice e visualizzazione più utile] (http://stackoverflow.com/questions/5552641/plotting-a-2d -matrix-in-python-codice-e-più-utile-visualizzazione). Inoltre, Google avrebbe aiutato qui. Hai controllato l'esteso matplotlib [esempi] (http://matplotlib.org/examples/index.html) e [gallery] (http://matplotlib.org/gallery.html)? –
hai visto la [matplotlib' gallery] (http://matplotlib.org/gallery.html#images_contours_and_fields) prima di postare? Ci sono alcuni buoni esempi usando 'imshow',' pcolor' e 'pcolormesh' che fanno quello che vuoi – tom
Possibile duplicato di [multi color Heat error error Python] (http://stackoverflow.com/questions/30068049/multi-colored -heat-map-error-python) – jkalden