Multiple subplots having same legend

  1. I have 3 subplots in the format (3,1,1),(3,1,2),(3,1,3) and I want to add a common legend on top of these plots. How do I do that?
  2. How to add a title where I have a string and 3 numeric value I would like to take as input. It prints multiple times with my current code
A = 25
B = 0.02
C = 0.09
D = plaintext(E)% E might contain _ values
title(sprintf('D: %g A=%.2f, B=%.3f, C=%.3f',plaintext(E),A,B,C))
function s = plaintext( s )
s = regexprep( s, '[\\\^\_]','\\$0');
end

回答 (1 件)

VBBV
VBBV 2023 年 3 月 15 日

0 投票

Read this page https://in.mathworks.com/help/matlab/ref/sgtitle.html

9 件のコメント

VBBV
VBBV 2023 年 3 月 15 日
sgtitle is needed for making common title for all subplots
VBBV
VBBV 2023 年 3 月 15 日
A = 25
A = 25
B = 0.02
B = 0.0200
C = 0.09
C = 0.0900
E = '_Hello $World'
E = '_Hello $World'
D = plaintext(E)% E might contain _ values
D = '0Hello $World'
title(sprintf('A=%0.2f, B=%.3f, C=%.3f\n D: %s',A,B,C,plaintext(E)))
function s = plaintext( s )
s = strrep( s, '_','0');
end
VBBV
VBBV 2023 年 3 月 15 日
Use %s format specifier for printing strings. strrep for replaicng required text/character patttern from given string
MattC
MattC 2023 年 3 月 15 日
Okay gotcha, and how to add a common legend for multiple subplots?
VBBV
VBBV 2023 年 3 月 15 日
To add common legend you can use sgtitle function. This link will provide more info about how to add a common legend for multiple subplots
MattC
MattC 2023 年 3 月 15 日
I think this link talks about adding a common title where as I am looking for a way to add common legend
VBBV
VBBV 2023 年 3 月 15 日
編集済み: VBBV 2023 年 3 月 15 日
Sorry, i misread first part of your question, To have a common legend, its better to use this custom function in File Exchnage .
I dont know if Matlab has such standard functions to represent a common legend for all subplots.
MattC
MattC 2023 年 3 月 16 日
Basically, I am trying to add a legend on top of the first subplot which will be generic like has shapes and the corresponding names with some color coding. Not trying to pull anything specific from the subplots. Is it possible then?
VBBV
VBBV 2023 年 3 月 16 日
編集済み: VBBV 2023 年 3 月 16 日
Legend positioning is possible for subplots using that custom function. Have a look at it. Otherwise you need an extra subplot to display legend something like what @starstrider demonstrated.

サインインしてコメントする。

カテゴリ

製品

リリース

R2020a

タグ

質問済み:

2023 年 3 月 15 日

編集済み:

2023 年 3 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by