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
采纳的回答
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 个评论
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, .......
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).
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 的更多信息
产品
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
