adding MAPE/RSME to subplot

1 view (last 30 days)
Hi MathWorks
I have a subplot consisting of four figures. For each figure I would like to add a name (MAPE) and the calculated value (perhaps in the top left corner of within the axes).
I have tried to work with text, legend or annotation, but without success.
At the moment the plots looks as one the attached .png-file. The value does not fit in the top left corner perfectly each time, it's too big in fontsize and does not look nice.
Is there any way to improve it and even add two more variables to the "text box" as I would also like RMSE and R^2?
The script for the plot looks as follows and I have attached a copy of the script and data:
f=figure;
for i = 1:4
subplot(2,2,i);
axis([min(fittedValues(:,i)) max(fittedValues(:,i)) min(meanReturns(:,i)) max(meanReturns(:,i))]);
set(gca,'FontSize',6)
ref = refline(1,0);
ref.Color = plotColors(3,:);
ref.LineWidth = 1.2;
title(titles(1,i),'FontSIze', 7);
xlabel('Model implied returns (in %)', 'FontSize', 6);
ylabel('Avg. monthly realized returns (in %)', 'FontSize', 6);
box on
hold on
labels = {'11','12','13','14','15','21','22','23','24','25',...
'31','32','33','34','35','41','42','43','44','45','51','52','53','54','55'};
text(fittedValues(:,i),meanReturns(:,i), labels,'HorizontalAlignment','center','FontSize',6,'Color',plotColors(1,:));
text(min(fittedValues(:,i)), max(meanReturns(:,i))-0.05, sprintf('MAPE = %.4f', vwMAPE(i,1)))
hold off
end
All the best,
Christoffer

Accepted Answer

Ameer Hamza
Ameer Hamza on 24 Mar 2020
Try something like this
f = figure();
% random values
MAPE = [1.234 2.123 1.034 0.234];
MSE = [2.20 1.20 2.23 1.10];
R2 = [0.9 0.7 0.8 0.9];
% make random graphs
ax = gobjects(1,4);
for i=1:4
ax(i) = subplot(2,2,i);
plot((1:10)+2*rand(1,10));
xLeft = ax(i).XLim(1);
xDiff = diff(ax(i).XLim);
yTop = ax(i).YLim(2);
yDiff = diff(ax(i).YLim);
text(xLeft+0.05*xDiff, yTop-0.05*yDiff, ['MAPE=' num2str(MAPE(i))], ...
'HorizontalAlignment', 'left', 'VerticalAlignment', 'top', 'Interpreter', 'latex');
text(xLeft+0.05*xDiff, yTop-0.15*yDiff, ['MSE=' num2str(MSE(i))], ...
'HorizontalAlignment', 'left', 'VerticalAlignment', 'top', 'Interpreter', 'latex');
text(xLeft+0.05*xDiff, yTop-0.25*yDiff, ['$R^2$=' num2str(R2(i))], ...
'HorizontalAlignment', 'left', 'VerticalAlignment', 'top', 'Interpreter', 'latex');
end
Result:
  6 Comments
Christoffer Benneballe
Christoffer Benneballe on 26 Mar 2020
That was it - after moving your code behind the refline, it works like a charm!
I'm trying to set the limits following the minimum and maximum values of the respective returns (fittedValues and meanReturns), but they are not plotted until the last few lines, where struct label is set equal to these values and plotted.
But it works now - thank you for getting back to quick!
Have a nice day, Ameer!
Ameer Hamza
Ameer Hamza on 26 Mar 2020
Glad to be of help.

Sign in to comment.

More Answers (0)

Categories

Find more on Formatting and Annotation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!