How may i select and omit legend elements recognized by Matlab on a problem?
1 view (last 30 days)
Show older comments
Hi Everyone, i'd like to understand how to select legend elements from many legend elements that sequentially show on a scatter plot problem. The legend needs to show only these 4 elements, a red circle for the red scatter plot under 2 curves, a blue circle for a blue scatter plot above 2 curves, a black line for the 2 curves y(x) and d(c), and a black dot size 5 for y(x) = d(c) (an intersection). iow:
a 'r' for the string 'below both'; for elements 'm' and 'n' a 'b' for the string 'above either'; for elements 'o' and 'p' a 'k-' for the string 'f(x), g(x)'; for elements 'y(x)' and 'd(c)' a 'ko' for the string 'x_0', 'markerfacecolor', 'k', 'markeredgecolor', 'k', 'markersize', 5; for element 'd(q)'
A photo of desired result is attached and my Matlab program i wrote is:
problem 3c
clear all
rng(100);
N = 10^4;
x = 2*rand(1,N);
y = rand(1,N);
f = @(x) exp(-x.^2) - x./2;
x0 = 0.5;
q = fzero(f, x0);
[r,s] = find(f(q));
rng(100);
N = 10^4;
c = 2*rand(1,N);
d = rand(1,N);
i = find(y < exp(-x.^2) & y < x./2);
j = find(y >= exp(-x.^2));
l = find(d < c./2);
k = find(d > c./2 & d < exp(-x.^2));
figure;
m = scatter(x(i), y(i), 1, 'r');
hold on;
n = scatter(c(l), d(l), 1, 'r');
o = scatter(x(j), y(j), 1, 'b');
p = scatter(c(k), d(k), 1, 'b');
x = 0:0.00020001:2;
y = @(x) exp(-x.^2);
c = 0:0.00020001:2;
d = @(c) c./2;
q;
d(q);
plot(x,y(x), 'k-', c,d(c), 'k-', q,d(q), 'ko', 'markeredgecolor',...
'k', 'markerfacecolor', 'k', 'markersize', 5);
yaxis(0, 1, 0:1);
xaxis(0, 2, 0:1:2);
title('area under both curves');
ylabel('y');
xlabel('x');
legend('under both', 'above either', 'f(x), g(x)', 'x_0',...
'location', 'n');
line([q,q], [d(q),0], 'linestyle', '--', 'color', 'k');
hold off;
How may i properly show the plot?
0 Comments
Answers (0)
See Also
Categories
Find more on Legend in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!