Friday, January 15, 2016

Plot Surface 2D

Kode Python berikut mendemonstrasikan bagaimana men-load data 2D XYZ lalu menampilkannnya sebagai surface.


#!/usr/bin/python
import numpy as np
from matplotlib.mlab import griddata
import matplotlib.mlab as ml
import matplotlib.pyplot as plt

data = np.genfromtxt('input2d.txt')
x1 = data[:,0]
y1 = data[:,1]
z1 = data[:,2]

numcols, numrows = 500, 500
xi = np.linspace(min(x1), max(x1), numcols)
yi = np.linspace(min(y1), max(y1), numrows)

xi, yi = np.meshgrid(xi, yi)

x, y, z = x1, y1, z1
zi = ml.griddata(x, y, z, xi, yi, interp='nn')

print zi.shape

#-- Display the results
#plt.contour(xi, yi, zi, 15, linewidths = 0.5, colors = 'k')
plt.pcolormesh(xi, yi, zi, cmap = plt.get_cmap('rainbow'))
plt.colorbar()
#plt.scatter(x, y, marker = 'o', c = 'b', s = 10, zorder = 10)
plt.show()

No comments: