How to create Contour plots in Matlab

In summary, the conversation is about creating a contour plot in MATLAB, with the original intention of creating a 2D-histogram to represent the data. The conversation includes discussions about different versions of MATLAB and code examples for creating the desired plot.
  • #1
TFM
1,026
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: 735
  • Example Plot 2.jpg
    Example Plot 2.jpg
    33.6 KB · Views: 861
Physics news on Phys.org
  • #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
 
  • #3
Version 7.8.0.347 (R2009a)
 
  • #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.
 
  • #5
Thanks
 
  • #6
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.
 
  • #7
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: 855
  • #8
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).
 

1. How do I create a contour plot in Matlab?

To create a contour plot in Matlab, you can use the contour function. This function takes in the x and y coordinates as well as the corresponding z values and plots the contour lines.

2. Can I customize the appearance of my contour plot in Matlab?

Yes, you can customize various aspects of your contour plot such as the contour line colors, line widths, and labels. You can also add a colorbar to indicate the corresponding z values.

3. How do I add a title and axis labels to my contour plot in Matlab?

To add a title and axis labels, you can use the title, xlabel, and ylabel functions. These functions take in a string as an argument to specify the title or label.

4. Is it possible to create a 3D contour plot in Matlab?

Yes, you can create a 3D contour plot using the contour3 function. This function takes in the same arguments as the contour function but also allows you to specify the viewing angle.

5. Can I save my contour plot as an image or export it to other file formats?

Yes, you can save your contour plot as an image or export it to other file formats such as PDF or EPS. You can use the saveas function and specify the desired file type.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
830
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top