How to create Contour plots in Matlab

  • Thread starter Thread starter TFM
  • Start date Start date
  • Tags Tags
    Matlab Plots
Click For Summary

Discussion Overview

The discussion revolves around creating contour plots in MATLAB, specifically for visualizing data that is currently represented as scatter plots. Participants explore methods for generating contour plots from a dataset and clarify the desired output.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant seeks assistance in creating a contour plot from a dataset currently visualized as a scatter plot, expressing confusion over the output from their initial attempts.
  • Another participant requests clarification on the version of MATLAB being used to provide more tailored assistance.
  • A participant suggests that the desired output may be a 2D histogram, providing example code that generates a contour plot based on a generated dataset.
  • Further clarification is provided by another participant, who emphasizes the need for a contour plot to reveal underlying trends in the data, specifically mentioning the presence of two peaks.
  • One participant explains that the contour plot is a representation of functions of two variables and reiterates that the earlier provided code aims to create a 2D histogram, which may align with the original request.

Areas of Agreement / Disagreement

Participants have not reached a consensus on the exact method to create the desired contour plot, with differing interpretations of the original request and the proposed solutions. Multiple approaches and clarifications are presented without resolution.

Contextual Notes

There is uncertainty regarding the specific requirements for the contour plot, including the nature of the data and the expected visual output. The discussion includes various assumptions about the dataset and the desired representation.

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: 805
  • Example Plot 2.jpg
    Example Plot 2.jpg
    33.6 KB · Views: 913
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: 921
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).
 

Similar threads

Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K