Why contourf connect far apart points
Show older comments
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 Comments
Mathieu NOE
on 29 Sep 2025
hello
can you share a working code and some data ?
Mathieu NOE
on 29 Sep 2025
I don't know how your data has been generated and why it is stored as thousands of cells of 10 x 10 matrices
there is maybe a way to reduce the amount of data needed to perform the contour plot
is there really a need to have so much data being plotted ?
Dimitrios
on 29 Sep 2025
Mathieu NOE
on 29 Sep 2025
Edited: Mathieu NOE
on 29 Sep 2025
ok
" A1,A2,A3 cell arrays with 3000 4000 or more cells with usually 10x10 matrices "
just wondering how the data is organized .... A1MAT size is then 10 x 10 or do you combine several cells to get the final A1MAT array ?
Dimitrios
on 29 Sep 2025
abc = randi(10,10);
def = {abc};
whos abc def
920 - 800 = 120 bytes of overhead, which is 15% overhead in this case, not 50%
It's not that the cell array version is double the size, Walter, it's that @Dimitrios kept both variables in memory so he has 800 + 920 = 1720 total bytes.
The suggestion was to reuse the same variable in the conversion rather than create the copy; there is no reason to keep both at the same time.
A=mat2cell(rand(10),10,10);
A1=cell2mat(A);
whos A*
is OP's original syntax.
My suggestion was
clear A*
A=mat2cell(rand(10),10,10);
A=cell2mat(A);
whos A*
which keeps only the one variable instead of two and the comparative memory is 800/1720 < 0.5
It is possible MATLAB may make a temporary copy behind the scenes, that I don't know about for sure...
No, there's no confusion about the problem, @Dimitrios, Walter and I just got off onto a sidebar discussion in that he didn't catch the suggestion I had made. As noted, you can help with the memory in several ways, the simplest being to just quit making copies of the same data in different format by reusing the same variable for the assignment to the array from the cell array.
As for the display problem, those are exactly the symptoms I illustrate the reason behind in my Answer; the problem is your construction of the surface from the tilings creates sections of the whole surface that are not sequential in their coordinates and so when they are pasted together in memory storage order, the coordinates aren't in geometric order and so the surface isn't smooth when the contouring function goes to draw the isolines.
As noted, the only way to solve this is one of
- Reorder the pieces in the tiles in true geometric order over x and y into one set of arrays covering the whole surface or
- Add a NaN at the boundary of each tiling so contour will not connect the separate pieces or
- Don't try to draw the isolines at all.
Also as noted, the only way anybody here can help further will be for you to upload a small(ish) portion of a dataset in order to be able to see what the actual construction is; IF (the proverbial "big if") the x,y coordinates are specified in meshgrid fashion, it may be possible to sort the array in x and y and rearrange z to match to reconstruct the full image; whether this is possilble or not will depend upon just what the geometry actually is and how the tilings were constructed, details of which you have not shared.
Dimitrios
on 29 Sep 2025
"...your suggestion with NaN can work"
It will work to not create the isolines across the discontinuities you've introduced by the tilings; however, if you will look closely at the example plots in the Answer, you'll notice that while the shape of the distinct contours matches that of the full image, the color maps aren't identical for each adjacent tiling because the data ranges aren't going to map to the same overall range. Thus, you're still going to get discontinuities; they'll show up as horizontal and vertical lines of color visual discontinuity between adjacent tiles.
You might be able to work around this by finding the overall range of the data and then setting the color maps within each tiling based on it; I've not looked into that, but it may get messy.
The second issue is that you may have issues in creating consistent isolines across the pieces; there are subtle differences between the example and the original full image that I didn't pursue further. It may be that because I took the expedient of replacing the last row/column instead of inserting the break with the small number of points the missing values caused the internals to be different and that would go away if did insert extra rows/columns. Specifically in the example, note there is no line finishing the lobe to the lower left around the [-2,2] region in the fourth image as compared to first. Close inspection will reveal other differences as well.
BTW, I experimented some and it isn't possible to not have contourf attempt to draw the isolines whether they are displayed or not; henc, prior suggestion 3. is out of the running and you're left with only the two alternatives.
Again, the better solution would be to either
- Don't create the data in this fashion to begin with, or
- Rearrange it in proper order before plotting.
And, also yet again, if you would upload a sample dataset for folks to poke at, the latter just might be possible to figure out.
Dimitrios
on 29 Sep 2025
dpb
on 30 Sep 2025
"...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.
Accepted Answer
More Answers (0)
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!