HiveBrain v1.2.0
Get Started
← Back to all entries
snippetpythonMinor

Generate graph in Matlab

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
generategraphmatlab

Problem

I use this Matlab code:

load ~/emailAnalysis/results.txt
results=results(size(results,1)-400:size(results,1),:)
temp = results(:,3)-1238370000;
h=plot(temp,smooth(results(:,1)),':b')
set(h,'LineWidth',1)
ylim([0 80])
xlim([max(temp)-(86400*7),max(temp)-1])
set(gca,'XGrid','on')
ylabel('Emails')
hold on
i=plot(temp,smooth(results(:,4)),'r')
j=plot(temp,smooth(results(:,5)),'-g')
k=plot(temp,smooth(results(:,6)),'m')
xlim([max(temp)-(86400*7),max(temp)-1])
set(gca,'XTick',[1:86400:(86400*max(temp))+1])
set(gca,'XTickLabel',['Mon';'Tue';'Wed';'Thu';'Fri';'Sat';'Sun'])
set(j,'LineWidth',2)
set(h,'LineWidth',1)
set(i,'LineWidth',2)
set(k,'LineWidth',2)
xlabel('Time')
title('Size of inbox over time (seven days)')
print -r3000 -djpeg /XXXX/inbox7day.jpeg
hold off


to generate this graph:

from data like:

34 2012-01-21 1327152611 5 16 10
32 2012-01-21 1327154411 5 14 9
32 2012-01-21 1327156209 5 14 9
34 2012-01-21 1327158012 5 14 9
34 2012-01-21 1327159808 5 15 9
34 2012-01-21 1327161611 5 15 9
34 2012-01-21 1327163406 5 15 9
33 2012-01-21 1327165211 5 13 9
34 2012-01-21 1327167011 5 13 9
31 2012-01-21 1327168810 4 12 8


but it's a little slow, and also probably not very elegant (it's my first use of Matlab) I'm interested in any tips for best practice, tips for speeding it up, or examples of how I might do the same things in R...

Solution

clear all;
load('results.txt');
time = results(:,3) - 1238370000;


There's no need to plot all of the points if you're going to restrict the range to the past week. So I'd suggest you find the index where the time is less than a week ago and limit your plotting to that period to reduce complexity

firstTimeIndex = find(time >= (max(time) - 86400*7), 1);


Also note that you can use 'end' instead of 'size(results,1)'

results = results(max(1,firstTimeIndex-1):end, :);
clf;
h = plot(time,smooth(results(:,1)),':b');
hold on;


Line properties like linewidth can be specified at the time of plotting

plot(time, smooth(results(:,4)), 'r', 'linewidth', 2);
plot(time, smooth(results(:,5)), 'g', 'linewidth', 2);
plot(time, smooth(results(:,6)), 'm', 'linewidth', 2);
xlabel('Time');
ylabel('Emails');
xlim([max(time)-(86400*7) max(time)-1]);
ylim([0 80]);
set(gca,'XGrid','on');
set(gca,'XTickLabel',['Mon';'Tue';'Wed';'Thu';'Fri';'Sat';'Sun']);
title('Size of inbox over time (seven days)');


I'm not sure why you are setting the resolution to 3000 dpi? Maybe you could elaborate on what you're using this for, but if your goal is high resolution and low storage size then you might like to use -dmeta for a metafile on windows, or -deps for an encapsulated postscript on unix

print -djpeg inbox7day.jpeg;


I'm not sure what you are trying to achieve with the 'XTick' property, but the vector you're passing in is extremely large (~100 million elements) which is why the code is running so slow. I'm guessing that you don't want 100 million ticks?

Code Snippets

clear all;
load('results.txt');
time = results(:,3) - 1238370000;
firstTimeIndex = find(time >= (max(time) - 86400*7), 1);
results = results(max(1,firstTimeIndex-1):end, :);
clf;
h = plot(time,smooth(results(:,1)),':b');
hold on;
plot(time, smooth(results(:,4)), 'r', 'linewidth', 2);
plot(time, smooth(results(:,5)), 'g', 'linewidth', 2);
plot(time, smooth(results(:,6)), 'm', 'linewidth', 2);
xlabel('Time');
ylabel('Emails');
xlim([max(time)-(86400*7) max(time)-1]);
ylim([0 80]);
set(gca,'XGrid','on');
set(gca,'XTickLabel',['Mon';'Tue';'Wed';'Thu';'Fri';'Sat';'Sun']);
title('Size of inbox over time (seven days)');
print -djpeg inbox7day.jpeg;

Context

StackExchange Code Review Q#8165, answer score: 4

Revisions (0)

No revisions yet.