Waterfall plot not showing correctly

I think this is an undesirable feature of the waterfall plot function and I'm posting it here so that others don't spend as long as I did trying to figure out why your plot doesn't look right.
Here is an example of a correcly rendered waterfall plot:
figure(1)
[X,Y] = meshgrid(-5:.5:5);
Z = Y.*sin(X) - X.*cos(Y);
p = waterfall(X,Y,Z)
If you happen to have nan values in your data the plot does not raise an error but the white 'area-fill' of each plot line dissappears.
figure(2)
Z(:,5) = nan;
p2 = waterfall(X,Y,Z)
I couldn't find any mention of this in the documentation, and when you don't know that nan values are the cause of this, it's very hard to diagnose. Especially if your data set is large or the nan values are in the first or last column and it's not so obvious to see that there is a gap in the data as it is in these simple examples.

3 Comments

While I can understand possible frustration, what would you have it do, instead?
If there are NaN in the dataset, it's not really waterfall's fault and don't think it (or any of the plotting routines) should be expected to produce data silently to replace such.
All handle graphics objects silently ignore NaN; altho I'll grant it isn't in bold type up front and probably not in all documentation for every graph type/function -- plot contains the following:
Tips
  • Use NaN and Inf values to create breaks in the lines. For example, this code plots the first two elements, skips the third element, and draws another line using the last two elements:plot([1,2,NaN,4,5])
I suppose folks just pick up on this and with experience would do an any(Z,'all') automatically if something didn't look as expected.
A quick perusal showed the above in documentation for plot and plot3 as well as loglog, but nothing else in a non-exhaustive search.
Wouldn't hurt to have an enhancement request for the documentation to make that a global, high-level piece of information at the top-level, agreed.
Bill Tubbs
Bill Tubbs on 18 Jun 2021
Edited: Bill Tubbs on 18 Jun 2021
My issue is not that it hasn't replaced the nan values with something. It's that it has turned off the fill colour for the area under all points when there is at least one nan. So, the statement "Use NaN and Inf values to create breaks in the lines" does not apply to waterfalls behaviour.
I would suggest two possible solutions:
  1. Documentation says: The area under the plot lines will only be filled if the data contains no NaN or Inf values
  2. Modify the area-filling algorithm so that it can handle NaN values (This would involve drawing filling up to the point where a Nan occurs).
I just checked and a regular area plot exhibits the behaviour I was suggesting for solution #2:
figure(3)
subplot(2,1,1)
x = -5:.5:5;
y = sin(x)+2;
area(x, y)
grid on
subplot(2,1,2)
y(x == 0) = nan;
area(x, y)
grid on

Sign in to comment.

Answers (0)

Products

Release

R2019b

Asked:

on 18 Jun 2021

Commented:

on 18 Jun 2021

Community Treasure Hunt

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

Start Hunting!