Problem with subplot size and annotation of each plot

Hey all,
I read the documentation very carefully and make this subplot, however, you can see it is very messed up. I have annotation (textbox) on each plot but after subplot, all of them show on each other.
Here is my subplot: ( You can see just 1 annotation showing and size of plots are very bad
I wish I can correct it so it is something like this figure below. Please tell me what option I have to make it right.
Thank you

 채택된 답변

Image Analyst
Image Analyst 2020년 4월 4일
Use xlabel() and ylabel() with a small enough font size instead of annotation() or text():
fontSize = 8;
xlabel('Observed Preciptation [mm]', 'FontSize', fontSize);
ylabel('Whatever you want', 'FontSize', fontSize);

댓글 수: 11

BN
BN 2020년 4월 4일
편집: BN 2020년 4월 4일
Dear Image Analyst,
Thank you, But I'm already done that.
Here is my first subplot, I have done the exact same thing for all others.
subplot(7,3,1)
figure(1);
hold on;
class1 = scatter(X,Y,12,'k','filled');
plot(1:max(X),'k'); %45 degree line
[pp,s] = polyfit(X,Y,1);
r_squared = 1 - s.normr^2 / norm(Y-mean(Y))^2;
R2 = r_squared;
plot(1:max(X),pp(1)*(1:max(X))+pp(2)); %trend line
str = {sprintf('y = %.2fx+%.2f',pp(1),pp(2)),sprintf('R^2 = %.2f',r_squared)};
annotation('textbox', [0.2, 0.75, .1, .1], 'String',str ,'FitBoxToText',...
'on','fontname','Cambria Math','HorizontalAlignment', 'center',...
'FontSize',8,'BackgroundColor', 'white');
xlabel('Observed precipitation (mm)','FontSize',8)
ylabel('Modeled Precipitation','FontSize',8)
set(gca,'fontname','Times New Roman','FontSize',8) % Set it to times
box on;
grid on
Please looking at the second picture that I posted, All subplots have a box that includes an equation and R2. when I plot separately my plots I have this annotation box too but when I use subplot I have just one box.
Thanks again
Try getting the handle of each subplot:
h1 = subplot(7,3,1)
annotation(h1, .......
BN
BN 2020년 4월 4일
편집: BN 2020년 4월 4일
As you said I used this:
h1 = subplot(7,3,1)
annotation(h1,'textbox', [0.2, 0.75, .1, .1], 'String',str ,'FitBoxToText',...
'on','fontname','Cambria Math','HorizontalAlignment', 'center',...
'FontSize',8,'BackgroundColor', 'white');
But this error appears:
Error using annotation (line 85)
First argument must be a valid annotation type or a handle to a figure,
uipanel, or uitab.
OK, usually passing in an axes handle works. Try using text() instead of annotation. You should be able to get most of those attributes (not sure about the box edge though).
Thank you
Any chance to plot all figures separately and then merge all figures in one figure?
Not really, or at least it would be fairly hard/complicated. Your best bet is to use subplot. I can look at it if you attach all data and code needed to replicate your figure window.
Dear Image Analyst
Here are my x and y.
and here is the code:
figure(1);
hold on;
scatter(x,y,12,'k','filled');
[pp,s] = polyfit(x,y,1);
r_squared = 1 - s.normr^2 / norm(x-mean(y))^2;
str = {sprintf('y = %.2fx+%.2f',pp(1),pp(2)),sprintf('R^2 = %.2f',r_squared)};
annotation('textbox', [0.2, 0.75, .1, .1], 'String',str ,'FitBoxToText',...
'on','fontname','Cambria Math','HorizontalAlignment', 'center',...
'FontSize',8,'BackgroundColor', 'white');
xlabel('Observed precipitation (mm)','FontSize',8)
ylabel('Modeled precipitation (mm)','FontSize',8)
set(gca,'fontname','Times New Roman','FontSize',8) % Set it to times
box on;
grid on
hTrend = refline(pp(1), pp(2)); % Trend line
hrefline = refline([1 0]); % 45 degree refline
hrefline.Color = 'k';
axis normal
I want to make a 7x3x1 subplot (repeat this x and y plot i neach plot) in the acceptable space and view like this picture that I download from google:
In order to simplify I attach just one x and y but I have 21 x and y that really similar to each other. and 21 te4xt boxes for each plot. in fact I repeated this code 21 times in order to achieve subplot.
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 8;
fprintf('Beginning to run %s.m ...\n', mfilename);
s = load('x.mat')
x = s.x;
s = load('y.mat')
y = s.y;
hFig = figure;
hFig.WindowState = 'maximized'
for k = 1 : 21
hPlot = subplot(7, 3, k);
hold on;
scatter(x,y,12,'k','filled');
[pp,s] = polyfit(x,y,1);
r_squared = 1 - s.normr^2 / norm(x-mean(y))^2;
str = {sprintf('y = %.2fx+%.2f',pp(1),pp(2)),sprintf('R^2 = %.2f',r_squared)};
thisPlotsPosition = hPlot.Position;
% Get the position of this plot as [x, y, width, height].
% Tweak as needed to get the position where you want it.
annotation('textbox', thisPlotsPosition, 'String',str ,'FitBoxToText',...
'on','fontname','Cambria Math','HorizontalAlignment', 'center',...
'FontSize',8,'BackgroundColor', 'white');
xlabel('Observed (mm)', 'FontSize', fontSize)
ylabel('Modeled (mm)', 'FontSize', fontSize)
title('Modeled vs. Observed Precipitation', 'FontSize', fontSize)
set(gca,'fontname','Times New Roman','FontSize',8) % Set it to times
box on;
grid on
hTrend = refline(pp(1), pp(2)); % Trend line
hrefline = refline([1 0]); % 45 degree refline
hrefline.Color = 'k';
axis normal
end
fprintf('Done running %s.m ...\n', mfilename);
Thank you ?
Hey, Image Analyst please I need your help again. I have this question and i will be grateful if you can help me out
Thanks

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

제품

태그

질문:

BN
2020년 4월 4일

댓글:

2020년 4월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by