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
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.
Image Analyst
2020 年 4 月 4 日
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).
BN
2020 年 4 月 4 日
Thank you
BN
2020 年 4 月 4 日
Any chance to plot all figures separately and then merge all figures in one figure?
Image Analyst
2020 年 4 月 5 日
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);

BN
2020 年 4 月 6 日
Thank you ?
Fego Etese
2020 年 4 月 6 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
