I have 3 variables data in xls file. I want to plot contour plot of one variable against other 2 variables on x and y axis. How do I do this?

 Accepted Answer

I assume your variables are vectors. If they are gridded, it is only necessary to use the reshape function to form them into matrices.
If they are not gridded, something like this will work:
x = rand(20, 1);
y = rand(20, 1);
z = rand(20, 1);
xv = linspace(min(x), max(x), 50);
yv = linspace(min(y), max(y), 50);
[X,Y] = ndgrid(xv, yv);
Z = griddata(x, y, z, X, Y);
figure
contourf(X, Y, Z)
Experiment to get the result you want.

4 Comments

Hello,
How do I make the figure screen bigger, without using the fullfig function? Secondly, regarding the contour plot, how do I plot a graph with one color - different shades. And how do I bold the major grid lines?
Here is my code so far:
Data = readmatrix('TestContourfile.txt');
x = Data(:,1);
y = Data(:,2);
z = Data(:,3);
userTitle = input('What is the title of your contour map? ', 's');
xv = linspace(min(x), max(x), 50 );
yv = linspace(min(y), max(y), 50);
[X,Y] = ndgrid(xv, yv);
Z = griddata(x, y, z, X, Y);
figure
contourf(X, Y, Z,'ShowText','on')
grid on
grid minor
xlabel('Speed in RPM');
ylabel('Input Watts');
title(userTitle);
With respect to the figure size, I am not certain what you want. See the Figure Properties documentation section on Position for some of the properties you can change.
See the contourf documentation section on Custom Line Width to change the line widths.
There are several colormap options that offer different shades of the same color. You can also create your own, for example to get varying intensities of green:
x = rand(20, 1);
y = rand(20, 1);
z = rand(20, 1);
xv = linspace(min(x), max(x), 50);
yv = linspace(min(y), max(y), 50);
[X,Y] = ndgrid(xv, yv);
Z = griddata(x, y, z, X, Y);
colorcol = linspace(0.1, 0.9, 10); % Define Number Of Intensities (Here 10)
cmap = zeros(numel(colorcol), 3); % Define ‘colormap’ Here
cmap(:,2) = colorcol(:); % Choose Green ([R G B])
figure
contourf(X, Y, Z)
colormap(cmap)
My contour lines value is showing as 80.34678, how do I round it of to 80 and so on?
If you know the values you want for the contours, the easiest way is to specify them in a vector as the fourth argument to contourf:
x = rand(20, 1);
y = rand(20, 1);
z = rand(20, 1)*100;
xv = linspace(min(x), max(x), 50);
yv = linspace(min(y), max(y), 50);
[X,Y] = ndgrid(xv, yv);
Z = griddata(x, y, z, X, Y);
colorcol = linspace(0.1, 0.9, 10); % Define Number Of Intensities (Here 10)
cmap = zeros(numel(colorcol), 3); % Define ‘colormap’ Here
cmap(:,2) = colorcol(:); % Choose Green ([R G B])
figure
contourf(X, Y, Z, [20 40 60 80], 'ShowText','on')
colormap(cmap)
Here the plot shows only the contours at [20 40 60 80]. Specify whatever values you want.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!