What can I do to make the pattern in the legend larger?I look up a lot of ways, can't solve, ask for help.Thinks!
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
s1 = scatter(data1(:,2),data1(:,3),900,'o','b','LineWidth',1.5,'MarkerFaceAlpha',.2,'MarkerEdgeAlpha',.2);
scatter(data2(:,2),data2(:,3),900,'d','r','LineWidth',1.5,'MarkerFaceAlpha',.2,'MarkerEdgeAlpha',.2);
%filled
scatter(data1(1:17,2),data1(1:17,3),900,'o','filled','MarkerFaceColor','b','MarkerEdgeColor','b','LineWidth',1.5,'MarkerFaceAlpha',.2,'MarkerEdgeAlpha',.2);
scatter(data2(1:7,2),data2(1:7,3),900,'d','filled','MarkerFaceColor','r','MarkerEdgeColor','r','LineWidth',1.5,'MarkerFaceAlpha',.2,'MarkerEdgeAlpha',.2);
set(gca,'xminortick','on');
set(gca,'yminortick','on');
lgd = legend({'{\itC_{3v}}(8)',...
'{\itC_{2v}}(9)', ...
'{\itC_{3v}}(8)(candidate)',...
'{\itC_{2v}}(9)(candidate)'}, ...
'FontSize', 25,'FontName','Times New Roman','Location','northwest','linewidth',2);%'FontSize', 30,
Réponse acceptée
DGM
le 16 Oct 2024
Modifié(e) : DGM
le 16 Oct 2024
@Star Strider pretty much reviewed what I dug through. There were changes to legend() over the years. A lot of the workarounds seem to rely on using a second output from legend, the use of which has been deprecated over the years and is now no longer documented afaik. The form of the descendant objects changed around R2018b, and then there were more recent changes in the latest versions.
I've tried messing around with the older workarounds based on using the second output from legend(). In R2019b, requesting two outputs from legend() breaks the formatting of the legend icons, namely the alpha -- which is perhaps a complication that simply wasn't revealed in those prior examples.
I chose to solve it using a dirtier approach. This may or may not cause other problems, but it seems stable so far.
% some fake data
n = 100;
x = 1:n;
y = rand(4,n);
% create all the plot objects
marksz = 500;
alpha = 0.5;
marksty = {{'bo'},{'rd'},{'bo','filled'},{'rd','filled'}};
markfa = {0,0,alpha,alpha};
extraparams = {'LineWidth',1.5,'MarkerEdgeAlpha',alpha};
hold on
for k = 1:4
hsc(k) = scatter(x,y(k,:),marksz,marksty{k}{:}, ...
'markerfacealpha',markfa{k},extraparams{:});
end
% create the legend object
legstr = {'{\itC_{3v}}(8)',...
'{\itC_{2v}}(9)', ...
'{\itC_{3v}}(8)(candidate)',...
'{\itC_{2v}}(9)(candidate)'};
hleg = legend(hsc,legstr,'FontSize',25,'FontName','Times New Roman', ...
'Location','northwest','linewidth',2);
% modify properties of undocumented descendant objects
% it may take digging to find the appropriate objects if this changes in the future
legendmarkersz = 15; % new legend icon size
drawnow % make sure the legend is constructed before messing with it
for k = 1:4
hleg.EntryContainer.Children(k).Icon.Transform.Children.Children.Size = legendmarkersz;
end
Is there a more direct path to find the legend marker object? I don't know. I just dug around until I found something that worked.
3 commentaires
Plus de réponses (2)
Star Strider
le 16 Oct 2024
I did a search on this and found How can I change the marker size in legend? and How to set legend marker size among others after my own experiments to do that failed. That does not appear to be possible, although I thought I saw a post a while ago that actually worked, I could not find one that did.
The accepted answer to How to enlarge legend marker size? explains the reason that it is not possible, and offers a work-around.
2 commentaires
Vinay
le 17 Oct 2024
Hello Lin
The Legend feature doesn't support direct customization of marker sizes. However, you can work around this limitation by using dummy plots.
These dummy plots can be tailored to achieve the desired legend properties, such as 'marker size', 'Display name', and 'colour'.
It's important to note that the legend marker size will increase only up to a certain point, beyond which it remains constant. The following code demonstrates this approach.
% Set random seed for reproducibility
rng(0);
% RANDOM data
n1 = 30; % Number of data points for data1
n2 = 20; % Number of data points for data2
% Linear trend with some distortion for data1
x1 = linspace(0, 10, n1)';
y1 = 2 * x1 + 5 + sin(x1) + randn(n1, 1) * 0.5; % Linear trend with sine distortion and noise
data1 = [(1:n1)', x1, y1];
% Linear trend with some distortion for data2
x2 = linspace(5, 15, n2)';
y2 = 1.5 * x2 + 3 + cos(x2) + randn(n2, 1) * 0.5; % Linear trend with cosine distortion and noise
data2 = [(1:n2)', x2, y2];
% Plot the data using the given scatter plot commands
ax = axes();
hold on;
% Scatter plots with default marker size
s(1) = scatter(data1(:,2), data1(:,3), 100, 'o', 'b', 'LineWidth', 1.5, 'MarkerFaceAlpha', .2, 'MarkerEdgeAlpha', .2);
s(2) = scatter(data2(:,2), data2(:,3), 100, 'd', 'r', 'LineWidth', 1.5, 'MarkerFaceAlpha', .2, 'MarkerEdgeAlpha', .2);
% Filled scatter plots (subset)
s(3) = scatter(data1(1:17,2), data1(1:17,3), 100, 'o', 'filled', 'MarkerFaceColor', 'b', 'MarkerEdgeColor', 'b', 'LineWidth', 1.5, 'MarkerFaceAlpha', .2, 'MarkerEdgeAlpha', .2);
s(4)= scatter(data2(1:7,2), data2(1:7,3), 100, 'd', 'filled', 'MarkerFaceColor', 'r', 'MarkerEdgeColor', 'r', 'LineWidth', 1.5, 'MarkerFaceAlpha', .2, 'MarkerEdgeAlpha', .2);
% Dummy plots for legend customization
hCopy(1) = plot(nan,nan,'bo','MarkerSize',200,'DisplayName', '{\itC_{3v}}(8)');
hCopy(2)= plot(nan,nan,'rd','MarkerSize',200,'DisplayName', '{\itC_{2v}}(9)');
hCopy(3) = plot(nan,nan,'bo','MarkerSize',200,'MarkerFaceColor', 'b','DisplayName', '{\itC_{3v}}(8)(candidate)');
hCopy(4) = plot(nan,nan,'rd','MarkerSize',200,'MarkerFaceColor', 'r','DisplayName', '{\itC_{2v}}(9)(candidate)');
legend(hCopy,'FontSize',12,'Location', 'northwest', 'linewidth', 2)
Kindly refer to the below documentation of ‘legend’ for more details:
0 commentaires
Voir également
Catégories
En savoir plus sur Legend dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!