You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to create nested legend
6 views (last 30 days)
Show older comments
I have 20 entries in the plot, 5 under 4 heading, I want to categorise those 5 under their respective heading. I have attached the image. So, here I want to simply write h = 525 mm and its sub headings as w = 180, 150, 120, 90, 60 and then similarly for h = 450, so that my legend entry will look a little bit less clumsy.
Accepted Answer
Star Strider
on 21 Aug 2023
One option is to put each ‘h’ in a separate column —
h = 300 : 75 : 525;
w = 60 : 30 : 180;
[H,W] = ndgrid(h,w);
figure
hold on
for k1 = 1:size(H,1)
for k2 = 1:size(W,1)
plot(H(:,k1), H(1,k1)+W(:,k2), 'DisplayName',["h = "+H(k1,1)+", w = "+W(k2,1)])
end
end
hold off
legend('Location','best', 'NumColumns',numel(h))
.
6 Comments
PRASHANT JHA
on 21 Aug 2023
Moved: Star Strider
on 21 Aug 2023
Thanks for your reply.
Sorry for silly question, but I am new to matlab and so I apologize in advance. Actually, I have a table named as data3 whose data I have plotted. So, what I wanted was to categorise w = 180, 150, 120, 90, 60 mm under the headings namely 525, 450, 375, & 300. So, I wrote this code.So, could you please help me out with this.
plot(data3.Var3(1:5),data3.Var5(1:5)); hold on;
plot(data3.Var3(6:10),data3.Var5(6:10)); hold on;
plot(data3.Var3(11:15),data3.Var5(11:15)); hold on;
plot(data3.Var3(16:20),data3.Var5(16:20)); hold on;
plot(data3.Var3(1:5),data3.Var10(1:5)); hold on;
plot(data3.Var3(6:10),data3.Var10(6:10)); hold on;
plot(data3.Var3(11:15),data3.Var10(11:15)); hold on;
plot(data3.Var3(16:20),data3.Var10(16:20)); hold on;
plot(data3.Var3(1:5),data3.Var15(1:5)); hold on;
plot(data3.Var3(6:10),data3.Var15(6:10)); hold on;
plot(data3.Var3(11:15),data3.Var15(11:15)); hold on;
plot(data3.Var3(16:20),data3.Var15(16:20)); hold on;
plot(data3.Var3(1:5),data3.Var20(1:5)); hold on;
plot(data3.Var3(6:10),data3.Var20(6:10)); hold on;
plot(data3.Var3(11:15),data3.Var20(11:15)); hold on;
plot(data3.Var3(16:20),data3.Var20(16:20)); hold off;
xlabel('w (in mm)');
ylabel('Cl');
legend('f = 1050 mm, h = 525 mm', 'f = 1050 mm, h = 450 mm', 'f = 1050 mm, h = 375 mm', 'f = 1050 mm, h = 300 mm', 'f = 900 mm, h = 525 mm', 'f = 900 mm, h = 450 mm', 'f = 900 mm, h = 375 mm', 'f = 900 mm, h = 300 mm', 'f = 750 mm, h = 525 mm', 'f = 750 mm, h = 450 mm', 'f = 750 mm, h = 375 mm', 'f = 750 mm, h = 300 mm', 'f = 600 mm, h = 525 mm', 'f = 600 mm, h = 450 mm', 'f = 600 mm, h = 375 mm', 'f = 600 mm, h = 300 mm', 'Location', 'Best','EdgeColor', 'none','FontSize',8);lineHandles = findobj(gca, 'Type', 'Line'); set(lineHandles, 'LineWidth', 1);
Star Strider
on 21 Aug 2023
I doubt that a ‘nested’ legend is possible. I presented the only option that I know to exist.
The only other option I am aware of would be:
h = 300 : 75 : 525;
w = 60 : 30 : 180;
[H,W] = ndgrid(h,w);
figure
hold on
for k1 = 1:size(H,1)
for k2 = 1:size(W,1)
plot(H(:,k1), W(:,k2)./H(:,k1)+rand/10, 'DisplayName',["w = "+W(k2,1)])
end
end
hold off
hlgd = legend('Location','best', 'NumColumns',numel(h));
sspc = " ";
title(hlgd, [string(h(1))+sspc+string(h(2))+sspc+string(h(3))+sspc+string(h(4))])
That is the best I can come up with.
I do not have your data, so I have to improvise.
.
PRASHANT JHA
on 21 Aug 2023
Thanks for you time. I have attached the matlab file. When you will load this data, you will get three tables named as data1, data2 and data3. I hope this will work.
Star Strider
on 21 Aug 2023
I have no idea what to do with these —
LD = load('matlab.mat')
LD = struct with fields:
data1: [20×20 table]
data2: [20×20 table]
data3: [20×20 table]
data5: [9×6 table]
x: [4×1 double]
data1 = LD.data1
data1 = 20×20 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 Var12 Var13 Var14 Var15 Var16 Var17 Var18 Var19 Var20
____ ____ ____ _______ ______ ____ ____ ____ _______ ______ _____ _____ _____ _______ ______ _____ _____ _____ _______ ______
525 1050 180 0.61066 1.6549 450 1050 180 0.61083 1.6792 375 1050 180 0.61114 1.6296 300 1050 180 0.61163 1.6257
525 900 180 0.61075 1.6594 450 900 180 0.611 1.6873 375 900 180 0.61114 1.6303 300 900 180 0.61156 1.6269
525 750 180 0.61074 1.6677 450 750 180 0.61141 1.6808 375 750 180 0.61137 1.6329 300 750 180 0.64118 1.6456
525 600 180 0.70535 1.6973 450 600 180 0.68547 1.6454 375 600 180 0.70427 1.6976 300 600 180 0.68976 1.6751
525 1050 150 0.61079 1.6523 450 1050 150 0.61101 1.6911 375 1050 150 0.61128 1.6543 300 1050 150 0.61157 1.6284
525 900 150 0.61144 1.6539 450 900 150 0.611 1.7013 375 900 150 0.61113 1.6751 300 900 150 0.61155 1.6329
525 750 150 0.61084 1.6626 450 750 150 0.61107 1.6979 375 750 150 0.61145 1.6755 300 750 150 0.64898 1.6577
525 600 150 0.65395 1.684 450 600 150 0.66703 1.652 375 600 150 0.71511 1.6018 300 600 150 0.69957 1.6805
525 1050 120 0.61093 1.6724 450 1050 120 0.61117 1.6998 375 1050 120 0.61137 1.6653 300 1050 120 0.61177 1.6258
525 900 120 0.61105 1.6734 450 900 120 0.61111 1.7103 375 900 120 0.61092 1.688 300 900 120 0.61157 1.6268
525 750 120 0.61092 1.673 450 750 120 0.61108 1.6897 375 750 120 0.61153 1.679 300 750 120 0.64705 1.6313
525 600 120 0.66347 1.663 450 600 120 0.66722 1.6352 375 600 120 0.72595 1.6202 300 600 120 0.71677 1.6505
525 1050 90 0.61112 1.6701 450 1050 90 0.61122 1.6598 375 1050 90 0.61149 1.6269 300 1050 90 0.61192 1.6293
525 900 90 0.61094 1.6706 450 900 90 0.61129 1.6576 375 900 90 0.61151 1.6258 300 900 90 0.61156 1.6292
525 750 90 0.61118 1.6697 450 750 90 0.61124 1.6588 375 750 90 0.61162 1.6217 300 750 90 0.64726 1.6535
525 600 90 0.66941 1.6545 450 600 90 0.67325 1.6825 375 600 90 0.73679 1.6385 300 600 90 0.72638 1.679
data2 = LD.data2
data2 = 20×20 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 Var12 Var13 Var14 Var15 Var16 Var17 Var18 Var19 Var20
____ ____ ____ _______ ______ ____ ____ ____ _______ ______ _____ _____ _____ _______ ______ _____ _____ _____ _______ ______
1050 180 525 0.61105 1.6606 900 180 525 0.61157 1.6644 750 180 525 0.61074 1.6677 600 180 525 0.70195 1.6497
1050 180 450 0.61103 1.6579 900 180 450 0.61156 1.6687 750 180 450 0.61141 1.6728 600 180 450 0.70169 1.6454
1050 180 375 0.61121 1.6453 900 180 375 0.61154 1.6563 750 180 375 0.61137 1.6615 600 180 375 0.70427 1.652
1050 180 300 0.61163 1.6226 900 180 300 0.61126 1.6269 750 180 300 0.64118 1.6456 600 180 300 0.68976 1.6751
1050 150 525 0.61108 1.6578 900 150 525 0.61132 1.6754 750 150 525 0.61084 1.6763 600 150 525 0.69954 1.6518
1050 150 450 0.6111 1.6606 900 150 450 0.61134 1.6819 750 150 450 0.61107 1.6798 600 150 450 0.6967 1.652
1050 150 375 0.61128 1.6504 900 150 375 0.61142 1.6751 750 150 375 0.61145 1.6698 600 150 375 0.70022 1.662
1050 150 300 0.61157 1.6284 900 150 300 0.61155 1.6329 750 150 300 0.64898 1.6496 600 150 300 0.68496 1.6805
1050 120 525 0.61119 1.6626 900 120 525 0.6113 1.6785 750 120 525 0.61092 1.6753 600 120 525 0.70917 1.6596
1050 120 450 0.61117 1.663 900 120 450 0.61129 1.6832 750 120 450 0.61108 1.679 600 120 450 0.71207 1.6603
1050 120 375 0.61137 1.657 900 120 375 0.61131 1.6782 750 120 375 0.61153 1.6679 600 120 375 0.71259 1.6602
1050 120 300 0.61177 1.6258 900 120 300 0.61157 1.6268 750 120 300 0.64705 1.6413 600 120 300 0.71677 1.6505
1050 90 525 0.61112 1.6607 900 90 525 0.61131 1.6706 750 90 525 0.61118 1.663 600 90 525 0.71693 1.6545
1050 90 450 0.61122 1.6598 900 90 450 0.61129 1.6726 750 90 450 0.61124 1.6646 600 90 450 0.71527 1.6567
1050 90 375 0.61149 1.6527 900 90 375 0.61133 1.6626 750 90 375 0.61162 1.6622 600 90 375 0.71692 1.6694
1050 90 300 0.61192 1.6293 900 90 300 0.61156 1.6292 750 90 300 0.64726 1.6535 600 90 300 0.70926 1.679
data3 = LD.data3
data3 = 20×20 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 Var12 Var13 Var14 Var15 Var16 Var17 Var18 Var19 Var20
____ ____ ____ _______ ______ ____ ____ ____ _______ ______ _____ _____ _____ _______ ______ _____ _____ _____ _______ ______
1050 525 180 0.61111 1.6598 900 525 180 0.61108 1.6554 750 525 180 0.64107 1.6618 600 525 180 0.66171 1.651
1050 525 150 0.6111 1.6615 900 525 150 0.61104 1.6554 750 525 150 0.64108 1.6626 600 525 150 0.65947 1.6495
1050 525 120 0.61109 1.6607 900 525 120 0.61105 1.6567 750 525 120 0.64109 1.6617 600 525 120 0.66147 1.6463
1050 525 90 0.61112 1.6596 900 525 90 0.61109 1.657 750 525 90 0.64112 1.6697 600 525 90 0.66941 1.6495
1050 525 60 0.61131 1.671 900 525 60 0.61125 1.6721 750 525 60 0.64151 1.6685 600 525 60 0.68774 1.6615
1050 450 180 0.61097 1.6709 900 450 180 0.611 1.6719 750 450 180 0.64114 1.6918 600 450 180 0.66855 1.6544
1050 450 150 0.61111 1.6691 900 450 150 0.611 1.672 750 450 150 0.64111 1.6979 600 450 150 0.66703 1.652
1050 450 120 0.61117 1.668 900 450 120 0.61111 1.672 750 450 120 0.64111 1.6897 600 450 120 0.66722 1.6494
1050 450 90 0.61122 1.6611 900 450 90 0.61129 1.666 750 450 90 0.64112 1.6659 600 450 90 0.67325 1.6503
1050 450 60 0.61113 1.6453 900 450 60 0.61157 1.6473 750 450 60 0.64113 1.6516 600 450 60 0.70116 1.6658
1050 375 180 0.61121 1.667 900 375 180 0.61111 1.6693 750 375 180 0.64114 1.6833 600 375 180 0.71043 1.642
1050 375 150 0.61128 1.667 900 375 150 0.61111 1.6702 750 375 150 0.64115 1.6876 600 375 150 0.71151 1.641
1050 375 120 0.61137 1.6653 900 375 120 0.61115 1.6688 750 375 120 0.64115 1.679 600 375 120 0.71595 1.642
1050 375 90 0.61149 1.6515 900 375 90 0.61148 1.6598 750 375 90 0.64116 1.6542 600 375 90 0.72368 1.6438
1050 375 60 0.61189 1.6309 900 375 60 0.61178 1.6281 750 375 60 0.64117 1.6275 600 375 60 0.74763 1.6568
1050 300 180 0.6115 1.658 900 300 180 0.61156 1.6513 750 300 180 0.64103 1.6595 600 300 180 0.7119 1.6498
data5 = LD.data5
data5 = 9×6 table
Var1 Var2 Var3 Var4 Var5 Var6
____ ________ ________ ____ _______ _______
0.05 0.079527 0.072315 0.05 1.6124 1.4754
0.1 0.065503 0.060581 0.1 1.7162 1.6127
0.15 0.056319 0.051083 0.15 1.4266 1.2966
0.2 0.044239 0.045688 0.2 1.2754 1.1724
0.25 0.04054 0.042303 0.25 1.1698 1.053
0.3 0.03749 0.039807 0.3 1.0805 0.97533
0.35 0.034922 0.037874 0.35 1.0332 0.95852
0.4 0.033318 0.035683 0.4 0.98215 0.91858
0.45 0.031876 0.034601 0.45 0.94208 0.85585
x = LD.x
x = 4×1
0.6107
0.6108
0.6107
0.7054
.
PRASHANT JHA
on 22 Aug 2023
yeah, i got it
i just tweaked the code a little bit here are there and got it perfectly
Star Strider
on 22 Aug 2023
Thank you!
More Answers (0)
See Also
Categories
Find more on Legend in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)