Issue with contour plot due to different size
3 views (last 30 days)
Show older comments
Hello,
As cited above I want to have a contour plot, but the problem is the size of X and Z are different. as given below:
x0 = x;
t0 = Time(1:9:100);
[t1, x1] = meshgrid(t0, x0);
contourf(t1,x1,H0I0_Turbulence_cen2,1000,'edgecolor','none')
colormap(jet);
colorbar;
X = min(t0):(max(t0)-min(t0))/9:max(t0);
set(gca, 'xlim', [min(t0) max(t0)]);
set(gca, 'xtick', X);
xtickformat('%.2f');
xlabel('t','FontSize',24,'fontweight','bold');
Y = min(x0):(max(x0)-min(x0))/9:max(x0);
set(gca, 'ylim', [min(x0) max(x0)]);
set(gca, 'ytick', Y);
ytickformat('%.1f');
ytickformat('%.2f');
ylabel('x','FontSize',24,'fontweight','bold')
set(gca,'ycolor','k');
set(gcf,'color','w');
hold on
contour(t1,x1,H0I0_Turbulence_cen2,'k','ShowText','on')
grid on
hold off
ct = 0:(max(max(H0I0_Turbulence_cen2))-0)/10:max(max(H0I0_Turbulence_cen2));
c=colorbar('Ticks',ct);
clim([0 max(max(H0I0_Turbulence_cen2))]);
c.TickLabels = compose('%.2f',ct);
ylabel(c,'\itI_{cen}','FontSize',24, 'fontweight', 'bold');
Here, size(t1) = 100 x 12, size(x1) = 100x12,
and size(H0I0_Turbulence_cen2)=6x12
So size is differing by a large amount.
Thus I get the error as "Error using contourf
The size of X must match the size of Z or the number of columns of Z."
Is there any way to solve this issue and plot the contour?
3 Comments
DGM
on 29 Apr 2024
Edited: DGM
on 29 Apr 2024
That's what I'm asking you. Your X,Y grids are based off of:
x0 = x; % this creates 100 elements of YData
t0 = Time(1:9:100); % this creates 12 elements of XData
So the question is why the variable called "x" (which corresponds to y) has 100 elements when the Z data has 6. If there is some sort of interpolation or translation that needs to be done, where in the space of YData do the upper and lower rows of the Z data lie?
Accepted Answer
Joshua Levin Kurniawan
on 29 Apr 2024
Here, the issue is that the size for using "contour" function is MATLAB must be comply as follows:
t0 must be equal to size(H0I0_Turbulence_cen2,2) and x0 must be a vector with length equal to size(H0I0_Turbulence_cen2,1). Or in other words, you can check
length(t0) == size(H0I0_Turbulence_cen2,2) %must return true
length(x0) == size(H0I0_Turbulence_cen2,1) %must return true
In this particular case, if you have x0 with length of 100 and t0 with length of 12, then contourf(t1,x1,H0I0_Turbulence_cen2,1000,'edgecolor','none') can be plotted if and only if H0I0_Turbulence_cen2 is 100x12 matrix
1 Comment
DGM
on 29 Apr 2024
That's not how length() or contour() work. You're presuming that the inputs are vectors, when they clearly aren't. The X,Y inputs to contour() may either be vectors, or 2D grids. Length() returns the size of the largest dimension. Unless you know what you're doing with it, don't use it.
% these are valid inputs
[X Y] = meshgrid(linspace(0,1,10),linspace(0,1,100));
Z = X.^2 + Y.^2;
contour(X,Y,Z) % they work
% your tests will incorrectly declare that the inputs are invalid
length(X)
length(Y)
size(Z)
The given X,Y inputs are 2D. The Z data is also 2D, but not the same size, and the sizes differ by a non-integer factor.
More Answers (0)
See Also
Categories
Find more on Contour Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!