Problem with subplot size and annotation of each plot
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
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
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).
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에 대해 자세히 알아보기
제품
참고 항목
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)
