Why contourf connect far apart points
121 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Dimitrios
il 29 Set 2025 alle 7:04
Commentato: dpb
il 30 Set 2025 alle 13:54
I have a problem with the contour it connects far apart points and appear colored lines ( I already set 'LineStyle', 'none') that they shouldn't how can I avoid this problem? My data are in a cell arrays and I use cell2mat to create matrices and then I do the contour If I do it from the cell arrays I don't have this problem but is too slow and use all the memory so I don't like this approach.
14 Commenti
dpb
il 30 Set 2025 alle 13:54
"...I can't have less than 2000 with 8x8 matrices and ... less data can't represent the ... contour..."
A typical HD monitor has only 1920x1080 pixels so more unique points than that are immaterial for display; closer than that will just be more than one point at the same display pixel location. That would allow an 8X decimation over the range of the x data. Even if you have 4K, it's only twice that so that would still allow roughly a 4X reduction in size with no discernible change in the plotting/visualiztion of the contour.
Risposta accettata
dpb
il 29 Set 2025 alle 13:23
Modificato: dpb
il 29 Set 2025 alle 15:43
A1=cell2mat(A1);
A2=cell2mat(A2);
A3=cell2mat(A3);
would reduce the memory consumption by over half because the cell array has an overhead associated with each cell over the actual data storage and so is even larger than the double array.
You could also save half the memory by using single instead of default double since that will be more than adequate for plotting accuracy.
Back to the original Q?, I expect the problem is the data structure of having the cell arrays and plotting them as the overall matrices is that the coordinates are based on the individual pieces and not as if were one single representation of the same surface and that is why it appears ok when plot each cell array by itself as opposed to all together.
We would need to have the actual data for a small example that would produce the problem to verify, but I'd expect the actual data structure is where the problem lies.
As for speeding it up, not sure there would be much that could be done there unless can rearrange the data structure to represent the overall surface as a single x,y,z array rather than by patchwork. Doing that would then allow for decimation by a significant factor before markedly changing the visual representation. Reducing the number of contours would also help; 100, is quite large number.
As an aside on speed, which release of MATLAB are you using; the newer releases (since R2024) aren't yet up to the performance level of earlier releases, but the data size and organization are going to be the key issues to solve methinks.
ADDENDUM
Example of my hypothesis
[X,Y,Z]=peaks(40); % small, divisible by 10 surface
x=mat2cell(X,10*ones(4,1),10*ones(4,1)); % split up into 10x10 cells
y=mat2cell(Y,10*ones(4,1),10*ones(4,1));
z=mat2cell(Z,10*ones(4,1),10*ones(4,1));
ix=randperm(4); iy=randperm(4); % rearrange the cells to not be contiguous
x=x(ix,iy); y=y(ix,iy); z=z(ix,iy); % keeping same order within subsections
tiledlayout('flow')
nexttile
contourf(X,Y,Z,10)
title('Original Full Surface')
nexttile
x=cell2mat(x); y=cell2mat(y); z=cell2mat(z);
contourf(x,y,z,2,'ShowText','on')
title('Tiled Surface, Two Levels')
nexttile
contourf(x,y,z,10)
title('Tiled Surface, Ten Levels')
nexttile
x(10:10:end,:)=nan; x(:,10:10:end)=nan;
y(10:10:end,:)=nan; y(:,10:10:end)=nan;
z(10:10:end,:)=nan; z(:,10:10:end)=nan;
contourf(x,y,z,10)
title('Full Tiled Surface with Breaks')
The second shows the contour lines drawn between distant points because the coordinates in the second case aren't all sequential so the surface looks all jumbled to the contour tracing function internally. Here I set it to have only two lines and to label them so can follow which went where; with a much larger number of unlabelled contours, it would just look like scribbles as the third shows..
To avoid this if cannot reconstruct the whole surface, insert a NaN between sections to stop the connecting; the above code just replaces the right and bottom edge data with NaN instead of inserting a row/column to illustrate the difference in the resulting contour plot. Now each individual tile looks ok and, in fact, reproduces the original overall because the coordinates are still consistent in the tiles; they just aren't trying to be connected. If the dimensions are large as in OP's case, the breaks may not even be visible.
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Surface and Mesh Plots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!