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

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

HI Ameer
It works!
Thank you!
Glad to be of help.
Hi again Ameer :-)
I tried to apply the procedure to another script as well, but the maximum on the y-axis no longer adjusts to fit the data in the last subplot, Panel D.
The maximum value in this subplot is 1.60 in meanReturns, so it should not have an upper limit of a bit more than 2. Do you have any idea how to adjust it?
Code looks like this now:
f =figure;
ax = gobjects(1,4);
for i = 1:4
ax(i) = subplot(2,2,i);
axis([min(fittedValues(:,i))-0.20 max(fittedValues(:,i))+0.15 min(meanReturns(:,i))-0.20 max(meanReturns(:,i))+0.15]);
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(ewMAPE(i))], ...
'HorizontalAlignment', 'left', 'VerticalAlignment', 'top', 'Interpreter', 'latex', 'FontSize', 6);
text(xLeft+0.05*xDiff, yTop-0.15*yDiff, ['RMSE = ' num2str(ewRMSE(i))], ...
'HorizontalAlignment', 'left', 'VerticalAlignment', 'top', 'Interpreter', 'latex', 'FontSize', 6);
text(xLeft+0.05*xDiff, yTop-0.25*yDiff, ['$R^2$ = ' num2str(ewR2(i))], ...
'HorizontalAlignment', 'left', 'VerticalAlignment', 'top', 'Interpreter', 'latex', 'FontSize', 6);
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,:));
hold off
end
All the best,
Christoffer
Christoffer, where is the command to draw these points? Are you using scatter() to plot those points? These lines should be moved after you have already plotted everything (e.g., after refline)
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(ewMAPE(i))], ...
'HorizontalAlignment', 'left', 'VerticalAlignment', 'top', 'Interpreter', 'latex', 'FontSize', 6);
text(xLeft+0.05*xDiff, yTop-0.15*yDiff, ['RMSE = ' num2str(ewRMSE(i))], ...
'HorizontalAlignment', 'left', 'VerticalAlignment', 'top', 'Interpreter', 'latex', 'FontSize', 6);
text(xLeft+0.05*xDiff, yTop-0.25*yDiff, ['$R^2$ = ' num2str(ewR2(i))], ...
'HorizontalAlignment', 'left', 'VerticalAlignment', 'top', 'Interpreter', 'latex', 'FontSize', 6);
set(gca,'FontSize',6)
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!
Glad to be of help.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!