How to create Contour plots in Matlab

  • Thread starter Thread starter TFM
  • Start date Start date
  • Tags Tags
    Matlab Plots
AI Thread Summary
To create contour plots in MATLAB, the user is transitioning from scatter plots to contour plots to better visualize data density. The initial attempts using the 'contour' function produced unsatisfactory results, resembling peaks rather than a proper contour plot. A suggested approach involves generating a 2D histogram from the data, which can effectively highlight trends and peaks in the dataset. The discussion emphasizes the importance of representing data with two variables to achieve the desired visualization. Proper implementation of the contour plot can reveal areas of higher concentration, aiding in data analysis.
TFM
Messages
1,016
Reaction score
0

Homework Statement



I need to create a contour plot

Homework Equations



N/A

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,
 

Attachments

  • Example plot.jpg
    Example plot.jpg
    13.2 KB · Views: 790
  • Example Plot 2.jpg
    Example Plot 2.jpg
    33.6 KB · Views: 902
Physics news on Phys.org
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
 
Version 7.8.0.347 (R2009a)
 
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.
 
Thanks
 
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:
%% 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.
 
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
 

Attachments

  • Plots.jpg
    Plots.jpg
    21.5 KB · Views: 910
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).
 
Back
Top