Graph prints more lines than it should

조회 수: 2 (최근 30일)
Zajt
Zajt 2017년 5월 7일
편집: Zajt 2017년 5월 9일
Hi!
I have this code:
mu = 0;
sigma = 1;
pd = makedist('Normal',mu,sigma);
fileID1 = fopen('TLSDeliveryTime.txt', 'r');
fileID2 = fopen('X509DeliveryTime.txt', 'r');
fileID3 = fopen('OCSPDeliveryTime.txt', 'r');
formatSpec = '%f';
x1 = fscanf(fileID1,formatSpec);
fclose(fileID1);
nbins=100;
[h, binedges] = hist(x1, nbins);
tlsNormalized = h / sum(h);
cdf = cumsum(tlsNormalized);
plot(cdf)
semilogx(binedges, cdf,'LineWidth',2)
hold on
x2 = fscanf(fileID2,formatSpec);
fclose(fileID2);
[h2, binedges2] = hist(x2,nbins);
x509Normalized = h2 / sum(h2);
cdf2 = cumsum(x509Normalized);
plot(cdf2)
semilogx(binedges2, cdf2,'LineWidth',2)
hold on
x3 = fscanf(fileID3,formatSpec);
[h3, binedges3] = hist(x3,nbins);
ocspNormalized = h3 / sum(h3);
cdf3 = cumsum(ocspNormalized);
plot(cdf3)
semilogx(binedges3,cdf3,'LineWidth',2)
fclose(fileID3);
[h3, binedges3] = hist(x3,nbins);
ocspNormalized = h3 / sum(h3);
cdf3 = cumsum(ocspNormalized);
plot(cdf3)
semilogx(binedges3,cdf3,'LineWidth',2)
hold off
%set(gca,'xscale','log')
xlabel('Delivery time (ms)');
ylabel('CDF (%)');
ylim([0,1]);
legend('TLS','X509','OCSP')
and when I plot the graph, I get this:
It should only plot 3 lines(TLS,X509,OCSP), why does it plot more lines?

채택된 답변

Walter Roberson
Walter Roberson 2017년 5월 8일
Your code has
plot(cdf)
semilogx(binedges, cdf,'LineWidth',2)
hold on
The plot() creates one line, but then when you do the semilogx() the first line is thrown away because by default the "high level plotting routines" erase the current axis. So the plot(cdf) is discarded and the semilogx plot is created. At that point, the "hold on" turns off the default behavior of erasing the previous axes contents, so anything after the "hold on" will add to the plot. At this point the plot has one line.
After that you have
plot(cdf2)
semilogx(binedges2, cdf2,'LineWidth',2)
hold on
The plot(cdf2) will add to the existing plot because "hold on" is in effect. That will create the second line.
The semilogx will add to the existing plot because "hold on" is in effect. That will create a third line.
The "hold on" after that will not change anything because "hold" is already on.
Then the
plot(cdf3)
semilogx(binedges3,cdf3,'LineWidth',2)
will add the fourth and fifth line to the plot.
You then go through a block of code
[h3, binedges3] = hist(x3,nbins);
ocspNormalized = h3 / sum(h3);
cdf3 = cumsum(ocspNormalized);
plot(cdf3)
semilogx(binedges3,cdf3,'LineWidth',2)
but x3 has not changed since the block immediately above that, so this block calculates exactly the same as had been calculated in the code right before that, and plots the sixth and seventh lines, but the sixth line is exactly the same as the fourth line and the seventh line is exactly the same as the fifth line so you cannot tell that the lines have been plotted twice.
It appears to me that you have been thinking that plot() of a variable followed by semilogx() referring to the same variable modifies the plot() that was just done. That is not the case, however: semilogx() does new plotting. You should probably remove the plot() calls (and remove the repeated computation.)
  댓글 수: 3
Walter Roberson
Walter Roberson 2017년 5월 8일
semilogx() plots.
semilogx(X, Y) is the same thing as
plot(X, Y)
set(gca, 'XScale', 'log')
Zajt
Zajt 2017년 5월 9일
편집: Zajt 2017년 5월 9일
Alright thank you, now some lines got removed so it works.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by