Hold on changes appearance of histogram2

3 views (last 30 days)
Jonah Pearl
Jonah Pearl on 20 Feb 2019
Commented: Adam on 20 Feb 2019
In the following code block, executing "hold on" before the histogram is created changes the appearance of the histogram in the plot. Specifically, it prevents MATLAB from drawing light grey lines demarcating the empty bins. I cannot figure out how to get the light grey lines to appear using a property. (The view is also different, but that matters less since it can be changed with the view command()). I cannot see any differences in properties between h and h2. I'd like to understand this difference. A work-around is to call hold on after the histogram has been created, but this seems like a poor solution. Thank you!
Version: Matlab 2018b. OS: Mac OS High Sierra
figure
x = rand(200,1)+rand(200,1)*3; % create data
y = rand(200,1)+rand(200,1)*3;
x = x(~(round(x)==2 | round(x)==3)); % create emtpy bins in data
y = y(~(round(x)==2)| round(x)==3);
[vals,xe,ye] = histcounts2(x, y); % set up to plot using histogram2
h = histogram2('XBinEdges',xe,'YBinEdges',ye,'BinCounts',vals, 'FaceColor', 'flat');
figure
hold on % only difference from above code
x = rand(200,1)+rand(200,1)*3;
y = rand(200,1)+rand(200,1)*3;
x = x(~(round(x)==2 | round(x)==3));
y = y(~(round(x)==2)| round(x)==3);
[vals,xe,ye] = histcounts2(x, y);
h2 = histogram2('XBinEdges',xe,'YBinEdges',ye,'BinCounts',vals, 'FaceColor', 'flat');
h
h2
  1 Comment
Adam
Adam on 20 Feb 2019
Calling hold on before you have plotted anything does tend to often have unwanted effects. Certain plotting functions reset some axes properties. I can't remember off-hand, exact situations in which this happens, but calling hold on when you already have something on the axes is the usual way to do it.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!