Filled Contour Plot Error

3 次查看(过去 30 天)
I have some functions with asymptotes and I want to get filled contour plot.
For example;
When I plot the function 3D and rotate it, I get:
First Method using fcontour
figure (1)
func=@(x,t) (16/3).*(12.*((-4)+4.*exp(1).^(8.*((-1/16)+(-2).*t+x))).^(-1)+(-4)*((-1/16)+(-2).*t+x));
fsurf(@(x,t) func(x,t),[-1 1 -1 1],'ShowContours','on');
zlim([-50,50]);
caxis([-50,50]);
colorbar;
colormap jet;
xlabel('x');
ylabel('t');
Second Method using contourf
figure (2)
[X, T] = meshgrid(linspace(-1,1), linspace(-1,1));
F=func(X,T);
contourf(X,T,F);
zlim([-50,50]);
caxis([-50,50]);
colorbar;
colormap jet;
xlabel('x');
ylabel('t');
As you see the colors in the last two figures are different from the first figure.
How to get the correct filled contour plots by automatically selecting appropriate contour levels for any functions?

采纳的回答

Star Strider
Star Strider 2022-1-3
编辑:Star Strider 2022-1-3
Request more contour levels (here I requested 150) —
func=@(x,t) (16/3).*(12.*((-4)+4.*exp(1).^(8.*((-1/16)+(-2).*t+x))).^(-1)+(-4)*((-1/16)+(-2).*t+x));
figure (2)
[X, T] = meshgrid(linspace(-1,1), linspace(-1,1));
F=func(X,T);
contourf(X,T,F,150);
zlim([-50,50]);
caxis([-50,50]);
colorbar;
colormap jet;
xlabel('x');
ylabel('t');
EDIT — (3 Jan 2022 at 14:16)
The surf plot (without rotating it) is —
figure
surf(X,T,F);
zlim([-50,50]);
caxis([-50,50]);
colorbar;
colormap jet;
xlabel('x');
ylabel('t');
so ‘automatically selecting the contour levels’ is going to be a challenge if it is necessary to see all of them. Another option of course is to select them with a specific vector of levels, perhaps 25 so this can be adapted to other functions —
lims = [-50 50]; % Limits To Be Used In The Plot
N = 15; % Number Of Contour Levels Desired
clvls = linspace(lims(1), lims(2), N); % Define Contour Levels
figure
contourf(X,T,F,clvls);
zlim(lims);
caxis(lims);
colorbar;
colormap jet;
xlabel('x');
ylabel('t');
The ‘clvls’ vector obviously must be defined before the contour call, so one way of making everything adaptive is to define the limits first as a separate 2-element vector, and then let zlim, caxis and ‘clvls’ use them as arguments. I did that in the last figure. Defining the limits is something you must provide, depending on how the plots are supposed to describe the data.
.
  3 个评论
Walter Roberson
Walter Roberson 2022-1-3
How to get the correct filled contour plots by automatically selecting appropriate contour levels for any functions?
You cannot. "Appropriate" contour level depends upon the interpretation of interest at the time, not upon the data itself.
... Especially, as in cases like this, when you have a discontinuity.
Star Strider
Star Strider 2022-1-3
1) I am not certain what ‘the first figure in the quesiton’ refers to. If it is the rotated surface plot, the two are essentially the same. If it refers to the first contour plot, that has to do with the number of contours plotted. Plotting fewer contours includes more levels in each contour, so the values in the blank area in the lower right get included in that contour.
2) I am not certain what the problem is. Deciding on the contours for a specific function depends on the values of the function. Since I have no way of determining what the desired result is, I have no way of determining how best to define them.
How to get the correct filled contour plots by automatically selecting appropriate contour levels for any functions?
It is possible to define the contour levels as I did here with the ‘clvls’ vector. The contours do not have to be regularly-spaced, so it would also be appropriate to use the logspace function or simply defining them manually or with respect to a function that would produce a vector of monotonically-increasing values (or one value if the value is a (1x2) vector with both elements being the same).
There is no one specific method of defining the contours. That depends on the data plotted and what the desired result is.

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2022-1-3
func=@(x,t) (16/3).*(12.*((-4)+4.*exp(8.*((-1/16)+(-2).*t+x))).^(-1)+(-4)*((-1/16)+(-2).*t+x));
syms x t
fun(x,t) = simplify(func(x,t))
fun(x, t) = 
figure
fsurf(fun, [-1 1 -1 1]);
view([0 90]); caxis([-50 50]); colorbar(); colormap(jet)
figure
fsurf(fun, [-1 1 -1 1], 'showcontours', true)
view([0 90]); caxis([-50 50]); colorbar(); colormap(jet)
figure
fsurf(func, [-1 1 -1 1],'showcontours', true)
view([0 90]); caxis([-50 50]); colorbar(); colormap(jet)
figure
[X, T] = meshgrid(linspace(-1,1), linspace(-1,1));
F=func(X,T);
contourf(X, T, F, -50:10:50);
caxis([-50 50]); colorbar(); colormap(jet)
The white in the bottom right corner is a section with F less than the lowest contour.

类别

Help CenterFile Exchange 中查找有关 Contour Plots 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by