python - Getting a contour plot to use data from a CSV file -
hi experienced python community. collect magnetic data part of job have wait until field process data check quality. data comes in format:-
time,f,ef,fp,easting,northing,height 21:51:02,53169.31,-14.3,-17.79,386330.362,7371876.155,540.939
this can outputted in different formats such txt, xls or in case csv. aim able plot on laptop , check there no contamination data. using google lead me stackoverflow , looking through various posts have come script below. thank these posts. problems can read csv file can't understand how data plotting section , removing unwanted numbers line 27 onwards. sure find rather simple have been going around in circles last 2 weeks due lack of experience. thank reply.
import numpy np import matplotlib.pyplot plt import matplotlib.mlab ml f = open("filename.csv") data = np.genfromtxt('filename.csv', dtype=[('time',float),('f',float),('ef',float), ('fp',float),('e',float),('n',float),('h',float)], comments='"', delimiter=',') #only here can see file being #read remove later line in f: print line #below copied elsewhere in stackoverflow , trying adapt #to needs @ moment duplicate point warning. #so need call above below. ndata = 100 ny, nx = 100, 200 xmin, xmax = 1, 50 ymin, ymax = 1, 50 x = np.random.randint(xmin, xmax, ndata) y = np.random.randint(ymin, ymax, ndata) z = np.random.random(ndata) xi = np.linspace(xmin, xmax, nx) # yi = np.linspace(ymin, ymax, ny) # zi = ml.griddata(x, y, z, xi, yi) # 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 = 5, zorder = 10) plt.xlim(xmin, xmax) plt.ylim(ymin, ymax) plt.show()
two things. first, you're doing deal more work necessary when reading in data. long have 1 header line, should simple like
data = np.genfromtxt('filename.csv', skip_header=1, delimiter=',')
here, skip_header=1
says skip first line. note data have nan in first column. that's fine; says numpy doesn't recognize time string. assume don't need plotting. note don't need f = open("filename.csv")
@ all, , if ever want to, sure use f.close()
once you're done it.
second, plot, need reshape data. plt.contour
function takes 3 main arguments. first , second specify x , y coordinates, while third specifies z values. if there n_x
, n_y
coordinate values, z has have n_x
* n_y
values.
i'll have assume csv file in particular order. here, i'll assume first goes through values of easting, repeats values of easting different values of northing. data like
x = data[:n_x,4] y = data[::n_x,5] z = data[:,6].reshape(n_y,n_x)
here, data[:n_x,4]
takes first n_x
values 5th column (which number 4 when start 0), should give different x values. then, data[::n_x,5]
takes numbers 6th column, skips n_x @ time, gets different y values. finally, reshape
command takes height data, , makes rectangular array plotting. if want other height, use value other 6.
then, plot data like
plt.contour(x,y,z) plt.show()
everything else in lower section in code either construct random sample data, or add other bells , whistles plot. it's best play around after basic working.
Comments
Post a Comment