Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: How to create Contour plots in Matlab

  1. Jan 29, 2010 #1

    TFM

    User Avatar

    1. The problem statement, all variables and given/known data

    I need to create a contour plot

    2. Relevant equations

    N/A

    3. The attempt at a solution

    Hi

    Firstly apologies if this is not the correct thread.

    I need to create contour plots using matlab. I currently am creating ordinary scatter plots, but I have been told it is better to make this a contour plot, due to the high density (I have attached a example plot)

    The data is currently in a datastruct and the plot at the moment is like so:

    scatter(datastruct8.logden, datastruct7.gr,'.')

    now there are a lot of variables.

    I have tried to create a contour plot using contour, but the only time I got it even working was when I used the code:

    dencol = [datastruct7.den;datastruct7.ug];

    figure
    contour(dencol);


    However this just makes a weird plot which just appears to be peaks, not a contour plot (example Plot 2).

    Anyone know how too help me create a proper contour plot?

    Thanks in Advanced,
     

    Attached Files:

  2. jcsd
  3. Jan 29, 2010 #2
    Can you tell us which version of matlab... I might be able to help when i get my old textbook if I know the version
     
  4. Jan 29, 2010 #3

    TFM

    User Avatar

    Version 7.8.0.347 (R2009a)
     
  5. Jan 29, 2010 #4
    Ok so that is the version I know... I'll check the textbook when I get a chance because I can't remember right now.
     
  6. Jan 29, 2010 #5

    TFM

    User Avatar

    Thanks
     
  7. Jan 30, 2010 #6

    Päällikkö

    User Avatar
    Homework Helper

    I'm not exactly clear on what you want to plot. Maybe the 2D-histogram of the first attached image (with the density and changes in magnitude as the axis)?

    If so, something like the thing below should work (don't mind the upper part, as I just wanted to create some test data that ought to be more or less similar to yours):

    Code (Text):
    %% Data creation
    [xx, yy] = meshgrid(linspace(.1,pi,100), linspace(-pi,pi,100));
    zz = 1./(sqrt(pi*10*xx)) .* exp(-5*yy.^2.*xx);
    zz = zz/sum(zz(:));
    contour(xx, yy, zz);

    N = 100000;
    x = zeros(1, N);
    y = x;
    for n = 1:N
       r = rand;
       i = find(cumsum(zz(:))/sum(zz(:)) > r, 1, 'first');
       x(n) = xx(i);
       y(n) = yy(i);
    end

    %% Histogram
    xi = linspace(.1, pi, 100);
    yi = linspace(-pi, pi, 100);
    z = repmat(hist(x, xi), 100, 1).*repmat(hist(y, yi), 100, 1)';
    z = z/sum(z(:));
    figure;
    contour(xx, yy, z);
     
    Hopefully you can make some sense of it, as I'm a bit lazy to comment.

    The histogram is just done in equal intervals. I suppose there should exist a method of doing this better, like with Voronoi or something. Anyways, it's a start.
     
  8. Jan 30, 2010 #7

    TFM

    User Avatar

    Hi

    What I wanted was to make a contour plot of the first graph, because I am looking for trends and one part is the fact that there should be two peaks, one as seen 'sticking out', so to speak and the other which is obscured under the data, because there are so many points 0 I wanted a contour plot so that the peak could be seen as a area of higher plot concentration

    It should look something like my attached image below:

    Thanks
     

    Attached Files:

  9. Jan 30, 2010 #8

    Päällikkö

    User Avatar
    Homework Helper

    Contour plot is just a way to represent data with two variables, i.e. functions of the form f(x, y). What you apparently want to do, is plot the 2D histogram of your data. This is what the code I provided earlier does (see the countours it plots. If you do scatter(x,y), you see the input data. The final contour-command plots the output, i.e. the 2D histogram. From what I understood, this is what you wanted).
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook