How do I make a contour plot?Your data must be in a 2D grid in order for it to be contoured. There are two 2D contour graph types: 2D contour and user-defined contour. The standard 2D contour takes a 2D grid for the values and two 1D arrays for X and Y to define the locations of the grid nodes. If you do not specify the X and Y datasets then the contour will just float between the minimum and maximum of the domain which defaults from 1 to nx and 1 to ny. e.g.
I strongly recommend specifying the location of the grid using X Data and Y Data if you can. It is much better to have the contour plot fixed in your co-ordinate space, than floating over the whole domain. The second graph type is the user-defined contour. This graph type is drawn on an irregular grid rather than a regular grid. As the grid is irregular you must also supply a 2D grid of x values and a 2D grid of y values. e.g.
BUT MY DATA IS NOT IN A GRID! If your data is found as X Y Z values then you need to convert it into a grid. If it all possible you want to avoid interpolating your data as whenever you interpolate you lose accuracy. If you are unsure about the structure of your XYZ data, plot it first using a scatter plot:
You should now be able to see if your data is arranged on a regular or irregular grid. If it is then you can use various tricks to reshape your XYZ arrays into a 2D grid. I'll come back to this in a moment. If your XYZ points are completely irregular then you will have to interpolate. Gsharp has two standard interpolation methods bilinear and bivariate. It is not possible to say which method works best for your data. bivariate does make sure that the interpolated grid passes through the original points, but it is limited to 250 input points. I recommend defining the location of the grid that you want to interpolate onto first. Try and choose grid nodes which resemble the locations of the original data points. For example, if you only have a few different x points you can use the function unique to pick these out: gridx = unique(X); or you could use the range function to create an array of values equidistant apart with the same range as your input points: gridx = range(X,40); gridy = range(Z,40); Once you have defined gridx and gridy then interpolate your data: grid = bilinear(X,Y,Z,gridx,gridy); You can also specify regions and barriers in your interpolation - see the manual for more details. If I am using a region - I normally interpolate over the whole region and apply the region as part of the graph definition. This means that I can contour right up to the very edge of the region. You now have your data in a grid and you can plot it as before:
N.B. There are two things worth noting in the above GSL.
Finally we come to the part that I put off earlier concerning reshaping a regular set of XYZ points into a 2D grid. If you could see from the scatter plot that your XYZ forms a regular grid then we can find the locations of the x and y gridnodes using: gridx = unique(X); gridy = unique(Y); If your data is arranged in the correct order:
Then all you need to do is reshape your Z values using reshape: grid = reshape(Z,nx,ny,1); # (where nx = size(gridx) and ny = size(gridy); If your data is organised but not in this order then the simplest thing to do is grid = reshape(Z,nx,ny,1) as above. Plot the grid you have created and compare it to a scatter of the original points (You can use the GSL code above). You can now change your grid as follows:
Finally, if your XYZ is missing values or you cannot work out the order then you can always use the following script. It is slower than reshape, but it should do the job.
If your scatter plots shows that you have an irregular grid then work out the number of rows and columns and then: grid = reshape(Z,nx,ny,1); gridx = reshape(X,nx,ny,1); gridy = reshape(Y,nx,ny,1); If you do need to transpose the grid, remember to transpose the X and Y grid as well to preserve the XYZ correspondence. grid = slicex(grid,nx:1); gridx = slicex(gridx,nx:1); gridy = slicex(gridy,nx:1); |