paradox : updateing 'Data' property for histogram()

I am trying to set the 'Data' property of a histogram once I plot it as follows:
figure(10);
h = histogram(4+randn(1000,1)); % plot histogram for first time for data~N(4,1)
h.Data = randn(1000,1); % update plot with new data ~ N(0,1)
The above approach updates the data, however does not change other related properties, as a result only a part of the samples are shown.
Any ideas how to update the histogram without having to plot it from scratch?
Cheers, M

4 件のコメント

Geoff Hayes
Geoff Hayes 2016 年 8 月 5 日
M - which properties aren't being updated?
N/A
N/A 2016 年 8 月 5 日
編集済み: N/A 2016 年 8 月 5 日
Hi Geoff,
There are many properties that are the same. I compare the example discussed above, and display only the ones that have changed. I attach the code at the bottom for reproducibility.
Data' is the same ? NO
Values' is the same ? NO
Parent' is the same ? NO
Annotation' is the same ? NO
clc;clear;figure(10);rng('default'); % for reproducibility
N = 1000; x = 4+randn(N,1); % initial data are N samples from x~N(4,1)
% create a histogram with 'x' as data
subplot(1,2,1); h1 = histogram(x);
% same initial plot and update with N new samples x ~ N(0,1)
subplot(1,2,2); h2 = histogram(x); h2.Data = randn(1000,1);
% compare properties of 2 histograms
propertyNames = fields(h1);
for i = 1:length(propertyNames)
property = propertyNames{i};
if ~all(h1.(property)==h2.(property))
display(['',property,''' is the same ? NO']);
end
end
Steven Lord
Steven Lord 2016 年 8 月 5 日
And which release are you using?
N/A
N/A 2016 年 8 月 5 日
編集済み: N/A 2016 年 8 月 5 日
Hi Steven, I am using , Matlab 2015a. I have also replicated the same with Matlab 2016a as well
Cheers, M

サインインしてコメントする。

回答 (2 件)

N/A
N/A 2016 年 8 月 5 日
編集済み: N/A 2016 年 8 月 5 日

1 投票

clc;clear;figure(10);
% for reproducibility
rng('default');
N = 1000; x = 4+randn(N,1); % samples from x~N(4,1)
h = histogram(x);
% updating histogram()
xnew = randn(1000,1); % samples from x~N(0,1)
[~,BinEdges] = histcounts(xnew,h.NumBins);
BinLimits = [min(BinEdges),max(BinEdges)];
h.Data= xnew;
h.BinEdges= BinEdges;
h.BinLimits= BinLimits;
Steven Lord
Steven Lord 2016 年 8 月 5 日

0 投票

  • The Data property contains the actual data used to generate the histogram. You explicitly changed this, so it's not surprising that it is different.
  • The Values property is dependent on the Data and the locations of the bins, so it's not surprising that it is different. [As an extreme example, if I binned points based on their distance from Boston, Massachusetts in bins of 0-10, 10-20, 20-30, and 30-40 miles from Boston, I'd get VERY different results if I specified points in Massachusetts than if I specified points in California for those same bins.]
  • The Parent property is based on the axes that contains the histogram. The two histograms are in different subplot axes, so it's not surprising that it is different.
  • The Annotation property is a handle object, and the meaning of == for handle objects is slightly more restrictive than == for non-handle objects. Instead of asking "do each of your properties have the same values?" == for handles asks "do you refer to the same object?" The two histogram objects don't share the same object for their Annotation properties, so == returns false. But if you call isequal on the two handle objects from the Annotation properties (which asks the "do each of your properties have the same values?" question) you'll find that they are equal.
Based on your picture, I think I see what you want to do. You want the histogram object to recompute the bins based on the extent of the new data, right? If you change the BinMethod property of the second histogram to 'auto' (even though the value is already 'auto') "touching" that property will trigger the histogram object to recalculate its bin edges.

1 件のコメント

Guillaume
Guillaume 2016 年 8 月 5 日
Sounds like a Recalculate method would be a useful addition to the Histogram class, then.
More obvious than trying to find which property would trigger a recalculation.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeData Distribution Plots についてさらに検索

質問済み:

N/A
2016 年 8 月 5 日

コメント済み:

2016 年 8 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by