I have a big matrix (Supply) with size of 21 X 1000,000 and I want to plot each column against a fixed time vector as
stairs(time, supply)
I have tried to do it with for loop and using the Apps tool box but it is taking forever to run. Is there a fast way to do it?
This is my for loop:
if true
for k = 1:size(supply,2)
stairs(time ,Supply(:,k)); hold on
disp([' ',num2str(k)])
end
Thanks,

2 Comments

Adam
Adam on 13 Sep 2018
Are you sure you need to plot every value of those 1000000? There is no way that you can distinguish that many values on a plot. Indeed, at that level there aren't enough pixels on the screen to plot them all either. I would recommend sub-sampling your data and plotting that instead. I don't know off the top of my head what is a realistic number of samples to plot that you can visually distinguish, but it would be orders of magnitude less than that.
jonas
jonas on 13 Sep 2018
Edited: jonas on 13 Sep 2018

It could possibly be faster to store the output instead of plotting in each loop. The stairs function will not plot if two output arguments are specified

[x,y] = stairs([1 2 1],[1 2 1])

then you can just plot everything in one go

plot(x,y)

this way, no objects are created until you plot, so the loop should be much faster.

Sign in to comment.

 Accepted Answer

Jan
Jan on 13 Sep 2018
Omit the disp command, because updating the command window 1e6 times wastes a lot of time. Setting hold on repeatedly is not efficient also. Then do not create 1 Millions stairs calls, but call stairs with a matrix:
stairs(time, Supply);
But most of all consider Adam's comment: It is very unlikely that this plot is useful, because it has a huge pixel density.

9 Comments

Is there away to show the horizontal histogram of every year in one plot.
Is there a better way to show this graph?
jonas
jonas on 13 Sep 2018
What do you mean better way? It depends on what you want to convey to the reader. If you want to make the graph more visually appealing, then I'd check out the tons of options which are available for boxplot. For starters, I would probably not show all the outliers and use calender years on the x-axis (if that's what you have).
Thanks Jonas for your help. I'm looking for away to get the outliers. Any recommendations. This is my code:
if true
subplot(2,1,1)
plot(time ,Daily_Demand,'r--'); hold on
plot(time ,Daily_Demand*1.2,'b--'); hold on
xlim([0 22])
ylim([0 10000])
title('Supply vs Demand with NPV')
xlabel('Times (Years)')
ylabel('Daily Production Rate (MMSCFD)')
% saveas(h,sprintf('Daily Supply Demand.fig'))
hold on
boxplot(Daily_Supply_C,YearGroup_V)
subplot(2,1,2)
plot(time ,Daily_Demand,'r--'); hold on
plot(time ,Daily_Demand*1.2,'b--'); hold on
xlim([0 22])
ylim([0 10000])
title('Supply vs Demand with NPV')
xlabel('Times (Years)')
ylabel('Daily Production Rate (MMSCFD)')
% saveas(h,sprintf('Daily Supply Demand.fig'))
hold on
boxplot(Daily_Supply_C,YearGroup_V,'PlotStyle','compact')
end
jonas
jonas on 14 Sep 2018
Edited: jonas on 14 Sep 2018
First you need to decide what an outlier is. The whiskers in your boxplot extend to what boxplot considers to be extreme values and even more extreme values are considered as outliers. However, this is not necessarily true for all cases.
You can turn the outliers off by
boxplot(y,'OutlierSize',0)
However, if this is for a scientific paper it will be difficult to explain what those whiskers describe and why you chose to omit data outside of that range. According to the doc they extend to what the algorithm considers to be extremes. Another way would be to increase the size of the whiskers so that they include all data, or to extend the box to cover a broader range (default is 50%).
I'm not certain looking at 21 million points on a screen is the best way to find outliers, particularly if an outlier in one of your lines falls into the normal range of another line. Finding that brief spike could be challenging.
Consider using the isoutlier function to detect outliers using one of a couple different methods for defining what an outlier is.
I have used the following and I still see outlier
if true
supply_Daily_Mat = supply_Mat/365;
supply_C = supply_Daily_Mat';
A = [];
A_Group = [];
STR = 1; %Starting
STP = 0; %Stopping
for i = 1:size(Suppl_C,2)
TF = isoutlier(Supply_C(:,i),'mean');
B = Supply_C(TF==0);
D = length(B);
GG = i*ones(D,1);
STP = STP + D;
A(STR:STP,1) = B(:);
A_Group(STR:STP,1) = GG(:);
STR = STR + D;
end
boxplot(A,A_Group)
end
[Yaser Khojah wrote the following in a flag on Jonah's first comment on this answer. If you want to respond to a comment please add your own comment rather than adding a flag. The intent of flags is to notify contributors with sufficient reputation of potential unclear, off-topic, or spam messages. Thanks.]
I was able to create the plot without the outliers. However, I get this error "Value should be a finite number greater than 0". I think the outliers should be larger than zero.
Sorry that was bad advice from my part. I just noticed that the outliers were not plotted while missing the error message. This should work:
delete(findobj(gca,'Tag','Outliers'))

Sign in to comment.

More Answers (2)

Yaser Khojah
Yaser Khojah on 13 Sep 2018

0 votes

You are all right but I just wanted to see the whole dense lines. I'm using now boxplot instead since I could not show all the lines
Yaser Khojah
Yaser Khojah on 14 Sep 2018
Edited: Yaser Khojah on 14 Sep 2018
If you do not want to include outliers then use this:
if true
boxplot(y,'symbol','')
end

Tags

Asked:

on 13 Sep 2018

Edited:

on 14 Sep 2018

Community Treasure Hunt

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

Start Hunting!